Python读写/追加excel文件Demo分享

作者:yeatcsdn 时间:2021-11-11 15:35:41 

三个工具包

python操作excel的三个工具包如下,注意,只能操作.xls,不能操作.xlsx。

• xlrd: 对excel进行读相关操作

• xlwt: 对excel进行写相关操作

• xlutils: 对excel读写操作的整合

这三个工具包都可以直接使用pip进行下载:


sudo pip install xlrd
sudo pip install xlwt
sudo pip install xlutils1

xlwt的缺陷

xlwt只能创建一个全新的excel文件,然后对这个文件进行写入内容以及保存。但是大多数情况下我们希望的是读入一个excel文件,然后进行修改或追加,这个时候就需要xlutils了。

xlutils的简单使用

下面的demo是给一个excel文件追加内容:


#coding:utf-8

from xlrd import open_workbook
from xlutils.copy import copy

rexcel = open_workbook("collection.xls") # 用wlrd提供的方法读取一个excel文件
rows = rexcel.sheets()[0].nrows # 用wlrd提供的方法获得现在已有的行数
excel = copy(rexcel) # 用xlutils提供的copy方法将xlrd的对象转化为xlwt的对象
table = excel.get_sheet(0) # 用xlwt对象的方法获得要操作的sheet
values = ["1", "2", "3"]
row = rows
for value in values:
 table.write(row, 0, value) # xlwt对象的写方法,参数分别是行、列、值
 table.write(row, 1, "haha")
 table.write(row, 2, "lala")
 row += 1
excel.save("collection.xls") # xlwt对象的保存方法,这时便覆盖掉了原来的excel

相关文档

•xlrd:http://xlrd.readthedocs.io/en/latest/

•xlwt:http://xlwt.readthedocs.io/en/latest/

•xlutils:http://xlutils.readthedocs.io/en/latest/index.html

来源:https://blog.csdn.net/yedouble/article/details/77816588

标签:python,读写,excel,追加
0
投稿

猜你喜欢

  • PHP实现克鲁斯卡尔算法实例解析

    2023-09-08 19:35:57
  • pytorch 实现打印模型的参数值

    2022-11-11 22:22:28
  • 数字人组件反写[asp组件开发实例5]

    2009-06-09 13:23:00
  • Python通过两个dataframe用for循环求笛卡尔积

    2023-11-02 04:32:24
  • Python开发时报TypeError: ‘int‘ object is not iterable错误的解决方式

    2023-08-23 20:30:05
  • Oracle判断表、列、主键是否存在的方法

    2023-07-22 19:13:06
  • SQL SERVER数据操作类代码

    2012-07-11 16:16:12
  • Python通过Tesseract库实现文字识别

    2023-02-28 13:29:46
  • numpy.insert()的具体使用方法

    2021-12-23 15:33:34
  • Python tkinter实现计算器功能

    2023-06-29 15:41:29
  • 你会使用python爬虫抓取弹幕吗

    2021-04-28 03:06:50
  • python读取查看npz/npy文件数据以及数据完全显示方法实例

    2022-05-15 15:45:36
  • 利用javaScript实现点击输入框弹出窗体选择信息

    2023-09-13 03:14:50
  • python中main函数(主函数)相关应用例子

    2023-08-23 08:54:59
  • 网页视频播放器程序代码(通用代码),支持avi,wmv,asf,mov,rm,ra,ram等

    2008-07-16 11:56:00
  • Python爬虫基础之简单说一下scrapy的框架结构

    2022-01-04 23:19:00
  • Python Pandas工具绘制数据图使用教程

    2023-02-08 01:16:40
  • Python3 filecmp模块测试比较文件原理解析

    2021-10-28 15:24:19
  • 在Python中使用gRPC的方法示例

    2021-02-02 16:20:21
  • 浅析Python 中的 WSGI 接口和 WSGI 服务的运行

    2023-02-18 14:45:40
  • asp之家 网络编程 m.aspxhome.com