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 ...
随机推荐
- v-html渲染页面的时候 css样式无效
感谢: https://www.cnblogs.com/niuxiaoxian/p/9443873.html 当我们用v-html渲染页面的时候会发现样式并没有添加上,如下 复制代码 <temp ...
- java数据库连接池笔记
(课程笔记来源于跟着老师敲,老师是黑马程序b站白嫖课程~) #数据库连接池: 1.概念:就是一个容器(集合),存放数据连接的容器 当容器初始化好后,容器会被创建,容器中会申请一些连接对象,当用户来 ...
- HGAME_2023_WEB_WP_WEEK3
Ping to the host 很明显的rce,简单测试一下发现空格,cat,:被办,且执行无回显,空格用${IFS},%09,$IFS$9等等来绕过,我们利用dnslog将执行结果外带出来,这里使 ...
- appium连接手机 adb调试 app自动化
一. 工具准备 jdk,java环境必备. android sdk,要使用内置的Android debug bridge,简称adb,调试手机用. appium,提供自动化服务,app自动化的核心库. ...
- 自己从零写操作系统GrapeOS——1.GrapeOS介绍
为了学习操作系统原理我自己写了一个简单的操作系统,取名叫GrapeOS. GrapeOS是一个x86多任务桌面操作系统,但非常简单,代码只有4千行. 下面我来简单介绍一下GrapeOS的功能. 1.桌 ...
- Can not use keyword ‘await’ outside an async function
- IIS 负载均衡(ARR)
Application Request Route:应用程序请求路由 1.下载安装web平台安装程序 微软官网搜索 "web平台安装程序" 只能找到 "web平台安装程序 ...
- JAVA排序的方法
//冒泡排序法: package fuxi;public class Bubble { public static void main(String[] args) { int a ...
- 获取小程序toast控件
Toast 含义 为了给当前视图显示一个浮动的显示块,与dialog不同它永远不会获得焦点 显示时间有限,根据用户设置的显示时间后自动消失 本身是个系统级别的控件,它归属系统settings,当一个a ...
- ERROR StatusLogger No Log4j 2 configuration file found
ERROR StatusLogger No Log4j 2 configuration file found. Using default configuration (logging only er ...