
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 都有详细记录的安全问题,特别是,defusedxml和xlrd似乎在 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。

为了解决这个问题,你可以:
pandas中把默认的 engine 由原来的xlrd替换成openpyxl。
# Install openyxl
pip install openpyxl
# set engine parameter to "openpyxl"
pd.read_excel(path, engine = 'openpyxl')
接下来,介绍一下 Python 读写 Excel 需要导入的xlrd(读),xlwd(写)模块的一些常用操作。
import xlrd
excel = xlrd.open_workbook("data.xlsx")
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数组里
import datetime
from xlrd import xldate_as_datetime
xldate_as_datetime(43346.0, 0).strftime('%Y/%m/%d')
# '2018/09/03'
import xlwt
workbook = xlwt.Workbook(encoding = 'utf-8') # 创建一个workbook并设置编码形式
worksheet = workbook.add_sheet('My Worksheet') # 创建一个worksheet
worksheet.write(1,0, label = 'this is test') # 参数对应行, 列, 值
workbook.save('save_excel.xls') # 保存
