报错信息:
Traceback (most recent call last):
File "d:\cjavapy\actice.py", line 4, in <module>
df = pandas.read_excel('cat.xlsx')
File "D:\python\lib\site-packages\pandas\util\_decorators.py", line 296, in wrapper
return func(*args, **kwargs)
File "D:\python\lib\site-packages\pandas\io\excel\_base.py", line 304, in read_excel
io = ExcelFile(io, engine=engine)
File "D:\python\lib\site-packages\pandas\io\excel\_base.py", line 867, in __init__
self._reader = self._engines[engine](self._io)
File "D:\python\lib\site-packages\pandas\io\excel\_xlrd.py", line 22, in __init__
super().__init__(filepath_or_buffer)
File "D:\python\lib\site-packages\pandas\io\excel\_base.py", line 353, in __init__
self.book = self.load_workbook(filepath_or_buffer)
File "D:\python\lib\site-packages\pandas\io\excel\_xlrd.py", line 37, in load_workbook
return open_workbook(filepath_or_buffer)
File "D:\python\lib\site-packages\xlrd\__init__.py", line 170, in open_workbook
raise XLRDError(FILE_FORMAT_DESCRIPTIONS[file_format]+'; not supported')
xlrd.biffh.XLRDError: Excel xlsx file; not supported
问题原因:
使用xlrd1.2
或更早版本读取.xlsx
文件相关的潜在安全漏洞。最新版本的 xlrd (2.0.1)
仅支持 .xls
文件。
解决方法:
确保使用的是最新版本的 Pandas,至少是 1.0.1
,最好是最新版本。安装openpyxl
(https://openpyxl.readthedocs.io/en/stable/),使用如下代码读取:
pandas.read_excel('cat.xlsx', engine='openpyxl')
或者
如果不考虑潜在的安全漏洞的影响,则可以通过安装旧版本的 xlrd
来解决此错误。
使用如下命令安装1.2.0的xlrd解决:
pip install xlrd==1.2.0