python 使用xlrd读取xlsx文件时报错:XLRDError: Excel xlsx file; not supported
使用xlrd读取xlsx文件的报错与解决方法
·
使用xlrd
读取xlsx文件时,会报错:XLRDError: Excel xlsx file; not supported
from xlrd import open_workbook
th = open_workbook(r'文件.xlsx')
是因为新版的xlrd不支持xlsx
文件,只支持xls
文件,xlrd 已删除了对 xls 文件以外的任何内容的支持。
1.如果想要使用xlrd
读取xlsx
文件,需要安装旧版xlrd
pip install xlrd==1.2.0
但是这种方法会遇到一些其他的问题,会有安全警告,并且在读取excel文件时会报错:
AttributeError: 'ElementTree' object has no attribute 'getiterator'
该类报错是因为在新版python3.9
中,windows
中使用的更新删除了getiterator
方法,所以我们老版本的xlrd
库调用getiterator
方法时会报错,所以需要将老版本xlrd文件中的xlsx.py
里的getiterator()
改成iter()
如果知道自己的xlrd的安装路径可以直接打开,如果不知道,可以使用win +r
,输入cmd
,输入pip show xlrd
然后打开,查找getiterator
并替换成iter
即可。
2.如果嫌上面的步骤过于麻烦的话,还可以安装openpyxl
指定engine为openpyxl
pip install openpyxl
df1 = pd.read_excel(
os.path.join(APP_PATH, "Data", "aug_latest.xlsm"),
engine='openpyxl',
)
更多推荐
已为社区贡献4条内容
所有评论(0)