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)
python菜鸟学习: 13. excel文件编辑openpyxl使用的更多相关文章
- python入门学习:9.文件和异常
python入门学习:9.文件和异常 关键点:文件.异常 9.1 从文件中读取数据9.2 写入文件9.3 异常9.4 存储数据 9.1 从文件中读取数据 9.1.1 读取整个文件 首先创建一个pi_ ...
- python 作业 批量读取excel文件并合并为一张excel
1 #!/usr/bin/env python 2 # coding: utf-8 3 4 def concat_file(a,b): 5 #如何批量读取并快速合并文件夹中的excel文件 6 imp ...
- python xlrd,xlwt 读写excel文件
python 读excel文件,需要xlrd库.下载地址:https://pypi.python.org/pypi/xlrd python 写excel文件,需要xlwt库.下载地址:https:// ...
- python利用xlrd读取excel文件始终报错原因
1.代码按照网上百度的格式进行书写如下: 但运行后,始终报错如下: 百度了xlrd网页: 分明支持xls和xlsx两种格式的文件,但运行始终报错. 最后找到原因是因为我所读取的文件虽然是以.xls命名 ...
- python xlwt模块生成excel文件并写入数据 xlrd读取数据
python中一般使用 xlwt (excel write)来生成Excel文件(可以控制单元格格式),用 xlrd 来读取Excel文件,用xlrd读取excel是不能对其进行操作的. 1.xlrd ...
- Python基础学习七 Excel操作
python操作excel,python操作excel使用xlrd.xlwt和xlutils模块, xlrd模块是读取excel的,xlwt模块是写excel的,xlutils是用来修改excel的. ...
- 用python的pandas读取excel文件中的数据
一.读取Excel文件 使用pandas的read_excel()方法,可通过文件路径直接读取.注意到,在一个excel文件中有多个sheet,因此,对excel文件的读取实际上是读取指定文件.并 ...
- python操作csv和excel文件
1.操作csv文件 1).读取文件 import csv f=open("test.csv",'r') t_text=csv.reader(f) for t,i in t_text ...
- linux学习之vi文件编辑命令
如果文件为只读则无法使用普通用户编辑,需要切换到root用户,具体名称可参考: https://www.cnblogs.com/huangwei1992/p/9493443.html vi文件编辑命令 ...
- Selenium(Python) ddt读取Excel文件数据驱动
首先, 引入xlrd模块: ExcelDDT.py: import unittestfrom time import sleep from ddt import ddt, datafrom selen ...
随机推荐
- Hive删除分区名称中含有特殊字符
先说方案:通过show partitions和hdfs url看到的都不是真正的分区名称,都是经过URI重新编码的,访问这些分区应该使用分区名称的原始字符串. 场景描述 当我们在SQL语句中使用变量时 ...
- W04 散文周刊 : 重回Emacs
W04 散文周刊 : 重回Emacs 邮箱:ppbhoy@qq.com 博客地址: www.pipihao.com 公纵号:南湖小皮 公告:后面会开始稳定更新 使用Emacs还是有些沉重,我一直将Em ...
- log4j 完美版配置
百度的LOG4J配置记录一下 目前无时间阅读,反正能用就行了 # priority :debug<info<warn<error #you cannot specify every ...
- Vue.config.js配置 最新可用版本
最近 在学前端,然后,学了这个vue-cli脚手架,虽然,我这个vue-cli还不算入门,后我会把这个笔记补上 下面是我的Vue.config.js的配置,我感觉这个复用的程度高,所以记下 了这个随笔 ...
- 亲测有效! Studio One 6 V6.0.1 音乐编曲工具 含win/mac版
亲测有效! Studio One 6 V6.0.1 音乐编曲工具 含win/mac版 记录.生产.混合.掌握和执行所有操作.从工作室到舞台,Studio One6以易用为核心,是您的创意合作伙伴.当 ...
- DrCush_082020_血清阴性RA确诊延迟
转自Dr Jack Cush的Twitter (2020-08-20) 梅奥诊所:血清阴性RA的诊断存在延迟-血清阴性的首次关节肿胀距RA确诊为187天,而血清阳性者则为11天(P <.001) ...
- CKeditor页面公式不显示
<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax. ...
- PostgreSQL Repmgr集群
一.概述 repmgr是一套开源工具,用于管理PostgreSQL服务器群集内的复制和故障转移.它支持并增强了PostgreSQL的内置流复制,该复制流提供了一个读/写主服务器以及一个或多个只读备用数 ...
- Collection集合类(Java)
Collection集合体系 1,Collection集合分为两大类List集合和Set集合 List系列集合特点: 有序,可重复,有索引 ArrayList:有序,可重复,有索引. LinkedLi ...
- 小白学python必选一本书籍《Python学习手册》(第4版)
<Python学习手册>(第4版)pdf高清版免费下载地址: 提取码:ihpl 内容简介 · · · · · · Google和YouTube由于Python的高可适应性.易于维护以及适 ...