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 ...
随机推荐
- Flink程序打包
在基于 Flink DataStreamAPI 进行流式数据处理应用时,我们可能希望将依赖和应用程序分别打包,如此便于发布和问题定位.在较新版本的 Flink版本中推出了application模式,这 ...
- 线程基础知识 03 synchronized锁(对象在内存的布局和加上锁后对象在内存中的变化)
1 线程不安全演示 public class ThreadAndLockTest1 { private static int a = 0; public static void main(String ...
- JAVA虚拟机09---垃圾回收---经典垃圾回收器
1.Serial收集器 1.1简介 Serial收集器是最基础.历史最悠久的收集器,曾经(在JDK 1.3.1之前)是HotSpot虚拟机新生代收集器的唯一选择 1.2使用算法 标记-复制算法 1 ...
- Layui 表单元素考到页面样式不生效
表单元素必须要标记在表单里面(calss="layui-form") 例如: <div class="layui-form"> <input ...
- Spark Streaming实时计算
spark批处理模式: receiver模式:接收数据流,负责数据的存储维护,缺点:数据维护复杂(可靠性,数据积压等),占用计算资源(core,memory被挤占) direct模式:数据源由三方组件 ...
- vue - 环境变量和模式
1.在项目根目录中创建.env 或者 .env.xxx 的文件来指定环境变量 .env # 在所有的环境中被载入 .env.local # 在所有的环境中被载入,但会被 git 忽略 .env.[mo ...
- user-agent反反爬
title: user-agent反反爬 author: 杨晓东 permalink: user-agent反反爬 date: 2021-10-02 11:27:04 categories: - 投篮 ...
- 常见的git操作
git branch 查看本地所有分支 git status 查看当前状态 git commit 提交 git branch -a查看所有的分支 git branch -r 查看远程所有分支 git ...
- Cookie之记录用户上次访问时间
Cookie之记录用户访问时间 需求: 需求: 1:用户访问服务器(Servlet),判断用户是否是第一次访问,如果是第一次访问,则在页面响应内容为:"您好,欢迎您首次访问"; 2 ...
- conda创建和启动python虚拟环境
conda与python虚拟环境 创建环境 conda create -n ENV_NAME python=3.9 激活环境 conda activate ENV_NAME 给虚拟环境安装包: con ...