Python Excel 操作 | xlrd+xlwt 模块笔记

Python 的pandas模块使用xlrd作为读取 excel 文件的默认引擎。但是,xlrd在其最新版本(从 2.0.1 版本开始)中删除了对 xls 文件以外的任何文件的支持。

xlsx files are made up of a zip file wrapping an xml file.


Both xml and zip have well documented security issues, which xlrd was not doing a good job of handling. In particular, it appeared that defusedxml and xlrd did not work on Python 3.9, which lead people to uninstall defusedxml as a solution, which is absolutely insane, but then so is sticking with xlrd 1.2 when you could move to openpyxl.

从官方的邮件中,说的应该是 xlsx 本身是由一个 zip 文件和 xml 的头文件构成的,但是 xml 和 zip 都有详细记录的安全问题,特别是,defusedxmlxlrd似乎在 Python 3.9 上不起作用,这导致人们卸载defusedxml作为解决方案,这绝对是疯了,但是,当然了,您也可以转移到openpyxl,或者仍然坚持使用xlrd 1.2

$ conda search xlrd
Loading channels: done
# Name                       Version           Build  Channel
xlrd                           1.0.0          py27_0  conda-forge
xlrd                           1.0.0          py27_1  conda-forge
xlrd                           1.0.0          py35_0  conda-forge
xlrd                           1.0.0          py35_1  conda-forge
xlrd                           1.0.0          py36_0  conda-forge
xlrd                           1.0.0          py36_1  conda-forge
xlrd                           1.1.0          py27_1  pkgs/main
xlrd                           1.1.0  py27ha77178f_1  pkgs/main
xlrd                           1.1.0          py35_1  pkgs/main
xlrd                           1.1.0  py35h45a0a2a_1  pkgs/main
xlrd                           1.1.0          py36_1  pkgs/main
xlrd                           1.1.0  py36h1db9f0c_1  pkgs/main
xlrd                           1.1.0          py37_1  pkgs/main
xlrd                           1.1.0            py_2  conda-forge
xlrd                           1.2.0          py27_0  pkgs/main
xlrd                           1.2.0          py36_0  pkgs/main
xlrd                           1.2.0          py37_0  pkgs/main
xlrd                           1.2.0            py_0  conda-forge
xlrd                           1.2.0            py_0  pkgs/main
xlrd                           1.2.0    pyh9f0ad1d_1  conda-forge
xlrd                           2.0.1    pyhd3eb1b0_0  pkgs/main
xlrd                           2.0.1    pyhd8ed1ab_3  conda-forge

上面的问题将导致您在使用pandas调用 xlsx excel 上的read_excel函数时收到一个错误,即不再支持 xlsx filetype。

d950b532-035a-409b-ad76-bd1256b8d58a.png

为了解决这个问题,你可以:
  1. 安装 openpyxl 模块:这是另一个仍然支持 xlsx 格式的 excel 处理包。
  2. pandas中把默认的 engine 由原来的xlrd替换成openpyxl


# Install openyxl
pip install openpyxl

# set engine parameter to "openpyxl"
pd.read_excel(path, engine = 'openpyxl')


接下来,介绍一下 Python 读写 Excel 需要导入的xlrd(读),xlwd(写)模块的一些常用操作。

1. xlrd 模块

1.1 Excel 文件处理

  • 打开 excel 文件

import xlrd
excel = xlrd.open_workbook("data.xlsx")  
  • 获取并操作 sheet 工作表

sheet_names = excel.sheet_names()            # 返回book中所有工作表的名字, ['Sheet1', 'Sheet2', 'Sheet3']    
excel.sheet_loaded(sheet_name or indx)   # 检查某个sheet是否导入完毕

# 以下三个函数都会返回一个 xlrd.sheet.Sheet() 对象
sheet = excel.sheet_by_index(0)         # 通过索引获取,例如打开第一个 sheet 表格
sheet = excel.sheet_by_name("sheet1")  # 通过名称获取,如读取 sheet1 表单
sheet = excel.sheets()[0]               # 通过索引顺序获取

sheet.row_values(0#获取第一行的数据
sheet.col_values(0#获取第一列的数据
sheet.nrows          #获取总共的行数
sheet.ncols          #获取总共的列数
  • 遍历所有行

for i in range(0, sheet.nrows):
    row_list = sheet.row_values(i) # 每一行的数据在row_list数组里

1.2 日期处理

import datetime
from xlrd import xldate_as_datetime

xldate_as_datetime(43346.00).strftime('%Y/%m/%d'
# '2018/09/03'

2. xlwt 模块

2.1 创建 Book 工作簿(即 excel 工作簿)

import xlwt
workbook = xlwt.Workbook(encoding = 'utf-8')    # 创建一个workbook并设置编码形式

2.2 添加 sheet 工作表

worksheet = workbook.add_sheet('My Worksheet')  # 创建一个worksheet

2.3 向工作表中添加数据并保存

worksheet.write(1,0, label = 'this is test'# 参数对应行, 列, 值

workbook.save('save_excel.xls')  # 保存


fa5c120d-be83-44ac-b709-bc3ae1c857e6.gif


Python 中常见的 TypeError 是什么?

2021-04-16

f95ed577-87f1-4f5b-bd20-9859ede504d5.jpg

Python 列表、字典、元组的一些小技巧

2021-03-30

daa93353-f186-4443-a5d3-dadb7101b7bc.jpg

如何卸载 python setup.py install 安装的包?

2020-05-15

f8ad5fbe-cafe-4c5d-aa9b-d31f8b9de8ab.jpg

生物信息学 Python 入门之源码安装

2019-09-29

c08e0ee0-a620-4402-8ccb-abce1f2d221f.jpg

Python 文件与目录操作方法总结

2019-02-17

9e4186df-6e55-426a-8b67-34e282cc8443.jpg


本文分享自微信公众号 - 生信科技爱好者(bioitee)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

posted @ 2021-08-09 09:23  章鱼猫先生  阅读(126)  评论(0编辑  收藏  举报