怎么在Python3中利用openpyxl读写Excel文件?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
前言
Python中常用的操作Excel的三方包有xlrd, xlwt和openpyxl等,xlrd支持读取xls和.xlsx格式的Excel文件,只支持读取,不支持写入.xlwt只支持写入xls格式的文件,不支持读取。
openpyxl不支持xls格式,但是支持.xlsx格式的读取写入,并且支持写入公式等。
原始数据文件apis.xlsx内容:
namemethodurldatajsonresultget接口gethttps://httpbin.org/get?a=1& b=2文章表单接口posthttps://httpbin.org/post{名称:凯文,年龄:1}
post-json接口posthttps://httpbin.org/post
{名称:凯文,年龄:21}
读取数据
读取所有数据
import openpyxl #,打开excel 时间=excel openpyxl.load_workbook (& # 39; apis.xlsx& # 39;), #,有路径应带上路径 #,使用指定工作表 #=sheet excel.active 当前激活的工作表=#,sheet excel.get_sheet_by_name (& # 39; Sheet1& # 39;) #,读取所有数据 print(列表(sheet.values)), #, sheet.values 生成器 打印(sheet.max_column), #,最大列数 打印(sheet.max_row), #,最大行数
显示结果:
((& # 39;名字# 39;& # 39;方法# 39;,& # 39;url # 39;, & # 39;头# 39;,& # 39;数据# 39;,& # 39;json # 39;, & # 39;结果# 39;),(& # 39;得到接口& # 39;,& # 39;得到# 39;,& # 39;https://httpbin.org/get?a=1& b=2 & # 39;,没有,没有,没有,也没有),(& # 39;职位表单接口& # 39;,& # 39;文章# 39;,& # 39;https://httpbin.org/post& # 39;, & # 39;饼干:令牌=123 & # 39;,& # 39;{名称:凯文,年龄:21}& # 39;,没有,也没有),(& # 39;post-json接口& # 39;,& # 39;文章# 39;,& # 39;https://httpbin.org/post& # 39;,没有,没有,& # 39;{名称:凯文,年龄:21}& # 39;,None)]
引用>
7
4按行读取
代码接上例
,… #,按行读取 for row  sheet.iter_rows拷贝(min_row=1, min_col=1, max_col=3,, max_row=3):, ,打印(行) #,读取标题行 for row  sheet.iter_rows拷贝(max_row=1):=,title_row [cell.value for  cell 拷贝行) 打印(title_row) #,读取标题行以外数据 for row  sheet.iter_rows拷贝(min_row=2):=,row_data [cell.value for  cell 拷贝行) ,打印(row_data)打印结果:
(& lt;细胞& # 39;Sheet1& # 39; .A1>, & lt;细胞& # 39;Sheet1& # 39; .B1>, & lt;细胞& # 39;Sheet1& # 39; .C1>)
引用>
(& lt;细胞& # 39;Sheet1& # 39; .A2>, & lt;细胞& # 39;Sheet1& # 39; .B2>, & lt;细胞& # 39;Sheet1& # 39; .C2>)
(& lt;细胞& # 39;Sheet1& # 39; .A3>, & lt;细胞& # 39;Sheet1& # 39; .B3>, & lt;细胞& # 39;Sheet1& # 39; .C3>)
[& # 39;名字# 39;& # 39;方法# 39;,& # 39;url # 39;, & # 39;头# 39;,& # 39;数据# 39;,& # 39;json # 39;, & # 39;结果# 39;]
[& # 39;得到接口& # 39;,& # 39;得到# 39;,& # 39;https://httpbin.org/get?a=1& b=2 & # 39;,没有,没有,没有,没有]
[& # 39;职位表单接口& # 39;,& # 39;文章# 39;,& # 39;https://httpbin.org/post& # 39;, & # 39;饼干:令牌=123 & # 39;,& # 39;{名称:凯文,年龄:21}& # 39;,没有,没有]
[& # 39; post-json接口& # 39;,& # 39;文章# 39;,& # 39;https://httpbin.org/post& # 39;,没有,没有,& # 39;{名称:凯文,年龄:21}& # 39;,没有]读取单元格数据
代码接上例
… #,读取单元格数据 打印(表[& # 39;a1 # 39;]。value) 打印(sheet.cell (1, 1) value), #,索引从1开始打印结果:
名字
引用>
写入文件
代码接上例
#,写入单元格 表(& # 39;f2 # 39;],=, & # 39;通过# 39; 时间=result_col title_row.index(& # 39;结果# 39;)+ 1,#,& # 39;结果# 39;所在的列号 sheet.cell (3, result_col) .value =, & # 39;通过# 39; #,整行写入 new_row =,(& # 39;发送xml接口& # 39;,,& # 39;文章# 39;,,& # 39;https://httpbin.org/post& # 39;】 sheet.append (new_row) #,保存文件,也可覆盖原文件 excel.save (“apis2.xlsx")写入结果:
namemethodurldatajsonresultget接口gethttps://httpbin.org/get?a=1& b=2
PASSpost表单接口posthttps://httpbin.org/post{名称:凯文,年龄:1}
PASSpost-json接口posthttps://httpbin.org/post
{名称:凯文,年龄:21}
发送xml接口posthttps://httpbin.org/post
看完上述内容,你们掌握怎么在Python3中利用openpyxl读写Excel文件的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!
怎么在Python3中利用openpyxl读写Excel文件