如何修复 pandas pd.read_excel() error XLRDError: Excel xlsx file; not supported

问题:

尝试使用 pandas pd.read_excel() 读取 .xlsx 文件时,你看到此错误消息:

xlrd_error.txt
XLRDError: Excel xlsx file; not supported

解决方案

xlrd 库只支持 .xls 文件,不支持 .xlsx 文件。为了使 pandas 能够读取 .xlsx 文件,安装 openpyxl

install_openpyxl.sh
pip install -U openpyxl

之后,重试运行你的脚本(如果你运行的是 Jupyter Notebook,请确保重启 notebook 以重新加载 pandas!)。

如果错误仍然存在,你有两个选择:

选择 1(首选):更新 pandas

Pandas 1.1.3 不会自动选择正确的 XLSX 读取器引擎,但 pandas 1.3.1 会:

install_pandas.sh
pip install -U pandas

如果你运行的是 Jupyter Notebook,请确保重启 notebook 以加载更新后的 pandas 版本!

选择 2:在 pd.read_excel() 中显式设置引擎

engine='openpyxl' 添加到你的 pd.read_excel() 命令中,例如:

pd_read_excel_example.py
pd.read_excel('my.xlsx', engine='openpyxl')

Check out similar posts by category: Pandas, Python