#1. xls文件转换成xlsx文件,需要使用到 win32com

from win32com import client as wc

filepath = "D:\\python\\liyuzhuopan\\s14\\20220531"
# xls 转化成xlsx
def convert_Xsl_To_xlsx(filepath, filename, new_filename):
if new_filename.split(".")[1] == "xlsx":
e = wc.Dispatch('Excel.application')
pro = e.Workbooks.Open(filepath + "\\\\" + filename) #
# 另存为新的文件
new_excel_path = filepath + "\\" + new_filename
print(new_excel_path)
pro.SaveAs(new_excel_path, FileFormat=51) #FileFormat=51为xlsx文件格式
        pro.Close()
e.Application.Quit()
# 格式转换
return "转换成功{_filename}".format(_filename=new_filename)
else:
print("请输入新文件名称为.xlsx结尾")
return "请输入新文件名称为.xlsx结尾"
# xlsx转化成xls
# convert_Xsl_To_xlsx(filepath, "merge_setup20210807.xls", "merge_setup20210807.xlsx")

#2. xlsx文件转换成xls文件,需要使用到 win32com
def convert_xlsx_To_xls(filepath, filename, new_filename):
if new_filename.split(".")[1] == "xls":
# 格式转换
e = wc.Dispatch('Excel.application')
pro = e.Workbooks.Open(filepath + "\\\\" + filename) #
# 另存为新的文件
new_excel_path = filepath + "\\" + new_filename
pro.SaveAs(new_excel_path, FileFormat=56) #FileFormat=56为xls文件格式
        pro.Close()
e.Application.Quit()
else:
print("请输入新文件名称为.xls结尾")
return "请输入新文件名称为.xls结尾"

# convert_xlsx_To_xls(filepath, "merge_setup20210807_new.xlsx", "merge_setup20210807.xls")

#3.读取excel文件内容 需要用到 openyxl load_workbook
from openpyxl import  load_workbook, styles
def readExcel(filepath, filename):
facilityDict = {}
# print(filepath + filename)
readfile = load_workbook(filepath + "\\\\" + filename)
# 获取excel中的页签
# print(readfile.sheetnames)
for i in readfile:
# 读取excel中页签的值i.title
if i.title == "facility":
# 循环读取sheet里面的值,存放在字典表中
for index1 in range(2, i.max_row + 1):
key1 = str(readfile[i.title].cell(index1, 1).value)
value1 = str(readfile[i.title].cell(index1, 12).value)
value2 = str(readfile[i.title].cell(index1, 13).value)
facilityDict[key1] = value1 + "," + value2
elif i.title == "config":
for index2 in range(2, i.max_row + 1):
key2 = str(readfile[i.title].cell(index2, 4).value)
value3 = str(readfile[i.title].cell(index2, 6).value)
# print(key2, value3)
facilityDict[key2] = value3
else:
pass
readfile.close()
return facilityDict
# print(readExcel(filepath, "merge_setup20210807_new.xlsx"))

4. 写入excel文件 openyxl load_workbook
def wirteExcel(filepath, new_filename, dict1={}):
wirteFlie = load_workbook(filepath + "\\\\" + new_filename)
for i in wirteFlie:
if i.title == "facility":
for index1 in range(2, i.max_row + 1):
# 如果文件的第一行存在字典中则切割后赋值给excel的单元格
if wirteFlie[i.title].cell(index1, 1).value in dict1.keys():
value1 = dict1[wirteFlie[i.title].cell(index1, 1).value]
if str(value1).split(",")[1] != "None":
# print(value1)
column12 = str(value1).split(",")[0]
column13 = str(value1).split(",")[1]
wirteFlie[i.title].cell(index1, 12, column12)
wirteFlie[i.title].cell(index1, 13, column13)
else:
pass
else:
# 如果不存在字典中,则将该单元格变成黄色
wirteFlie[i.title].cell(row=index1, column=12).fill = yellow_fill
wirteFlie[i.title].cell(row=index1, column=13).fill = yellow_fill
# print(i.title + str(index1) + "set yellow")
elif i.title == "config":
for index2 in range(2, i.max_row + 1):
# print(wirteFlie[i.title].cell(index2, 4).value)
if wirteFlie[i.title].cell(index2, 4).value in dict1.keys():
value3 = dict1[wirteFlie[i.title].cell(index2, 4).value]
# print(value3)
if value3 != "None":
# print(value1)
wirteFlie[i.title].cell(index2, 6, value3)
else:
pass
else:
# 如果不存在字典中,则将该单元格变成黄色
wirteFlie[i.title].cell(row=index2, column=6).fill = yellow_fill
# print(i.title + str(index2) + "set yellow")
else:
pass
wirteFlie.save(new_filename)
wirteFlie.close()
# wirteExcel(filepath, "merge_setup20210807.xlsx", dict1)


python菜鸟学习: 13. excel文件编辑openpyxl使用的更多相关文章

  1. python入门学习:9.文件和异常

    python入门学习:9.文件和异常 关键点:文件.异常 9.1 从文件中读取数据9.2 写入文件9.3 异常9.4 存储数据 9.1 从文件中读取数据 9.1.1 读取整个文件  首先创建一个pi_ ...

  2. python 作业 批量读取excel文件并合并为一张excel

    1 #!/usr/bin/env python 2 # coding: utf-8 3 4 def concat_file(a,b): 5 #如何批量读取并快速合并文件夹中的excel文件 6 imp ...

  3. python xlrd,xlwt 读写excel文件

    python 读excel文件,需要xlrd库.下载地址:https://pypi.python.org/pypi/xlrd python 写excel文件,需要xlwt库.下载地址:https:// ...

  4. python利用xlrd读取excel文件始终报错原因

    1.代码按照网上百度的格式进行书写如下: 但运行后,始终报错如下: 百度了xlrd网页: 分明支持xls和xlsx两种格式的文件,但运行始终报错. 最后找到原因是因为我所读取的文件虽然是以.xls命名 ...

  5. python xlwt模块生成excel文件并写入数据 xlrd读取数据

    python中一般使用 xlwt (excel write)来生成Excel文件(可以控制单元格格式),用 xlrd 来读取Excel文件,用xlrd读取excel是不能对其进行操作的. 1.xlrd ...

  6. Python基础学习七 Excel操作

    python操作excel,python操作excel使用xlrd.xlwt和xlutils模块, xlrd模块是读取excel的,xlwt模块是写excel的,xlutils是用来修改excel的. ...

  7. 用python的pandas读取excel文件中的数据

    一.读取Excel文件   使用pandas的read_excel()方法,可通过文件路径直接读取.注意到,在一个excel文件中有多个sheet,因此,对excel文件的读取实际上是读取指定文件.并 ...

  8. python操作csv和excel文件

    1.操作csv文件 1).读取文件 import csv f=open("test.csv",'r') t_text=csv.reader(f) for t,i in t_text ...

  9. linux学习之vi文件编辑命令

    如果文件为只读则无法使用普通用户编辑,需要切换到root用户,具体名称可参考: https://www.cnblogs.com/huangwei1992/p/9493443.html vi文件编辑命令 ...

  10. Selenium(Python) ddt读取Excel文件数据驱动

    首先, 引入xlrd模块: ExcelDDT.py: import unittestfrom time import sleep from ddt import ddt, datafrom selen ...

随机推荐

  1. MRS_外部库相关问题汇总

    解决问题如下: CH32V307使用IQMath库时 使用MRS时,当使用到math数学库 CH32V307使用IQMath库时 CH32V307是支持硬件浮点的,可以是math.h中的sinf.IQ ...

  2. 12月5日内容总结——JS基础知识及变量常量、基本数据类型、运算符、流程控制、函数、内置对象

    目录 一.JS简介 简介 ECMAScript的历史 二.JS基础 1.注释语法 2.引入js的多种方式 3.结束符号 三.变量与常量 编写和运行js代码的两种方式 变量声明 四.基本数据类型 1.数 ...

  3. allure环境搭建

    allure环境搭建 在搭建之前你应该有python.pycharm jdk也需要(文中忽略,可以参考网上文档安装,可以用jdk1.8) 以windows为例,mac.linux你用到这些操作系统,这 ...

  4. Spring Cloud 2022.0.1 Spring Cloud Zookeeper4.0

    官网: https://spring.io/ 左侧菜单 向下找到 spring Cloud Zookeeper 所有我们希望看到的都在 Reference Doc 中,点击进入 连接zookeeper ...

  5. spring in action day-06 JMS -ActiveMQ Artemi

    JMS -ActiveMQ Artemi JMS:它是一个规范,类似于jdbctemplate Spring提供了jmstemplate来发送和接收消息. 搭建JMS环境 1.引入依赖 我们要使用的消 ...

  6. P12_小程序API的3大分类

    小程序 API 概述 小程序中的 API 是由宿主环境提供的,通过这些丰富的小程序 API,开发者可以方便的调用微信提供的能力,例如:获取用户信息.本地存储.支付功能等. 小程序 API 的 3 大分 ...

  7. ROS多机通讯

    1.主机: sudo /etc/hosts 写入从机的地址和用户名: 192.168.31.14    raspi2 2.从机 sudo /etc/hosts 写入主机的地址和用户名 192.168. ...

  8. Appium自动化(一)-window环境搭建详细教程

    一.软件环境所需要运用的工具 1.JAVA1.8.1以上环境 2.AndroidSDK 3.Appium Desktop(appium servers) 4.Appium Client 5.Appiu ...

  9. GeoServer发布Oracle空间数据

    1. 概述 Oracle是常用的数据库,Oracle数据库包含空间数据库,可以在Oracle中进行空间数据的存储,更详细的信息可参考: 空间数据库 | Oracle 中国 GeoServer是常用的开 ...

  10. 利用自定义ref实现防抖

    1. debounce.js import { customRef } from 'vue'; export function debounceRef(value, delay = 1000) { l ...