python 对excel交互工具的使用详情
作者:lianghaibos 时间:2021-11-25 19:10:06
python 对excel的 读入 与 改写
(对比xlwt、openpyxl、xlrd)
xlwt不支持写xlsx文件。
openpyxl不支持读xls文件。
计划任务xlrd支持读xls,xlsx文件。
计划任务推荐读文件用xlrd,写文件用openpyxl。
#一、xlrd 读
# 1.引入库& 下载库 xlrd
pip install xlrd # 下载
pip show xlrd # 显示版本
pip install xlrd==1.2.0 # 下载指定版本
import xlrd# 导入
workBook = xlrd.open_workbook('D:\project\info.xls', 'rb') # 打开文件
workBook = xlrd.open_workbook(r'D:\project\info.xls')
allSheetNames = workBook.sheet_names() # 获取所有sheet的名字(list类型)
SheetName1= workBook.sheet_names()[0]# 按索引号
print(allSheetNames, SheetName1)
#输出:
['Sheet1', 'Sheet2', 'Sheet3'] Sheet1
# 获取sheet内容
sheet1_content1 = workBook.sheet_by_index(0) # sheet索引从0开始
sheet1_content2 = workBook.sheet_by_name('sheet1') # 按sheet名字获取
# 获取整行和整列的值(数组)
print(sheet1_content1.name,sheet1_content1.nrows,sheet1_content1.ncols)
# 获取整行和整列的值(数组)
rows = sheet1_content1.row_values(3) # 获取第四行内容
cols = sheet1_content1.col_values(2) # 获取第三列内容
print(rows)
print(cols )
# 获取单元格内容(三种方式)
print(sheet1_content1.cell(1, 0).value)
print(sheet1_content1.cell_value(2, 2))
print(sheet1_content1.row(2)[2].value)
二、python 写入数据
1 、 xlwt包写入Excel文件
xlwt 写库的局限性: 只能写入新建的 excel。
(写入打开文档 可用xlutils.copy的 copy 复制一份)
xlwt中生成的xls文件最多能支持65536行数据
创建表写入数据
# 向execl中 批量写入虚假数据
import xlwt,faker,random
wb=xlwt.Workbook()
sheet002=wb.add_sheet("002")
head=["姓名","年龄","性别"]
for h in head:
sheet002.write(0,head.index(h),h)
#利用for 循环 挨个写入 数据 行,列,数据值 这里列使用下标即可
fake=faker.Faker()
for i in range(1,101):
sheet002.write(i, 0, fake.name())
sheet002.write(i, 1, random.randint(10,60))
sheet002.write(i, 2, random.choice(['男','女']))
wb.save("002.xls")
#2 复制表写入数据
import xlwt
import xlrd
import xlutils.copy
rd = xlrd.open_workbook("Hello.xls", formatting_info = True) # 打开文件
wt = xlutils.copy.copy(rd) # 复制
sheets = wt.get_sheet(0) # 读取第一个工作表
sheets.write(m, n, "I love you!") # 向 m-1 行 n-1 列的单元格写入内容
wt.save("Hi.xls") # 保存
2、openpyx 只可以读xlsx 不可读xls文档
xl = openpyxl.load_workbook('D:\project\infoexcel.xlsx', data_only=True)
# 设置工作表
sheet1 = xl.worksheets[0]
for i in range(1, 24):
sheet1.cell(i, 3).value = cvalue
# 保存表格
xl.save('D:\project\infoexcel.xlsx')
三、小结
python 提供excel交互的工具包很多,
一、 找到适合自己
二、 区分不同包的权限与功能
如:(xlrd 新版本不支持xlsx ,需要回滚老版本才可使用)
三、出现问题debug运行寻找问题点,合理尽快解决问题!
来源:https://blog.csdn.net/qq_40761920/article/details/125819263
标签:python,excel,交互,工具,使用
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python numpy--数组的组合和分割实例
2023-02-17 03:58:39
![](https://img.aspxhome.com/file/2023/2/87942_0s.jpg)
浅谈python opencv对图像颜色通道进行加减操作溢出
2021-12-26 11:45:23
![](https://img.aspxhome.com/file/2023/3/99473_0s.jpg)
IE中雅黑字体给布局带来的变化
2008-06-13 11:22:00
![](https://img.aspxhome.com/file/UploadPic/20086/13/2008613122420492s.gif)
Python判断对象是否为文件对象(file object)的三种方法示例
2021-05-27 09:46:17
边框样式的写法总结
2009-01-18 13:00:00
模拟兼容性的 inline-block 属性
2008-04-08 12:37:00
Python 基于Selenium实现动态网页信息的爬取
2023-10-31 12:00:22
![](https://img.aspxhome.com/file/2023/3/100773_0s.jpg)
Python super()函数使用及多重继承
2022-10-06 19:21:37
Python如何发送与接收大型数组
2022-07-26 06:54:54
Python 循环读取数据内存不足的解决方案
2022-02-25 17:32:17
![](https://img.aspxhome.com/file/2023/1/80771_0s.jpg)
python多线程同步实例教程
2022-08-15 20:45:19
![](https://img.aspxhome.com/file/2023/0/85320_0s.png)
js 操作cookie的教程
2008-03-05 13:08:00
Flask使用Pyecharts在单个页面展示多个图表的方法
2021-10-12 18:16:35
![](https://img.aspxhome.com/file/2023/4/82694_0s.jpg)
jquery AJAX 三个发送状态 posting, error, success
2010-07-31 18:59:00
举例讲解Python的Tornado框架实现数据可视化的教程
2022-01-16 23:56:52
![](https://img.aspxhome.com/file/2023/1/123811_0s.png)
python3学习笔记之多进程分布式小例子
2022-08-23 04:46:07
![](https://img.aspxhome.com/file/2023/0/103320_0s.png)
PHP中的traits实现代码复用使用实例
2023-11-22 04:27:55
Python读取本地文件并解析网页元素的方法
2022-01-10 08:06:27
python文件目录操作之os模块
2023-01-10 14:22:59
Pytorch 如何实现LSTM时间序列预测
2023-06-26 01:04:24
![](https://img.aspxhome.com/file/2023/9/121509_0s.jpg)