python菜鸟学习: 13. excel文件编辑openpyxl使用
#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)
本文来自博客园,作者:鲤鱼洲畔,转载请注明原文链接:https://www.cnblogs.com/liyuzhoupan/p/16671932.html