Pandas读取XLSX文件错误

来自YTYZX有图有真相的百科
跳转至: 导航搜索
1. 使用Pandas读取XLSX文件(新版本Excel文件)出现错误。
   报错信息:File "C:\Users\username\AppData\Local\Programs\Python\Python38\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
   错误原因:Pandas默认使用xlrd引擎读取Excel 文件,但是xlrd新版本(2.0.1)只支持xls文件(Office2007之前版本),所以使用openpyxl来处理,相比xlrd速度会慢,但是可以解决上面所说的问题。
   官方解释:xlrd is a library for reading data and formatting information from Excel files in the historical .xls format. #仅支持操作xls老版本文件。

PandasXLSXError1.png

2. 首先确认Pandas版本高于1.0.1.
   查看Pandas版本方法:  >>> import pandas as pd
                         >>> pd.show_versions()
                         >>> pd.__version__

PandasXLSXError2.png

3. 然后安装并导入openpyxl,并且加上engine='openpyxl'(类似下图2),重新运行即可。
   安装openpyxl命令:pip install openpyxl

PandasXLSXError3.png