问题:
我正在使用 pd.read_excel 打开 .xls,但出现错误。 (“打包 excel 文件” 从 https://cima.aemps.es/cima/publico/nomenclator.html 下载)
df_cima = pd.read_excel("price_tracker/es/support/Presentaciones.xls")
xlrd.biffh.XLRDError: Excel xlsx file; not supported
这个文件的后缀是 .xls 但这个错误告诉我它是 .xlsx
然后我尝试添加engine="openpyxl"
,它通常用于在xlrd版本不再是1.2.0时读取.xlsx,然后它又给了我另一个错误
openpyxl.utils.exceptions.InvalidFileException: openpyxl does not support the old .xls file format, please use xlrd to read this file, or convert it to the more recent .xlsx file format.
我的环境:
我不想将我的 xlrd 版本改回 1.2.0,从其他答案中我看到新版本的 xlrd 仅支持 .xls,但我不明白为什么不支持为我的文件工作。
提前致谢。
答案 0 :(得分:1)
使用 Pandas 1.1.5 和 xlrd 2.1.0
将 Presentaciones.xls
重命名为 Presentaciones.xlsx
。
import pandas as pd
# Use openpyxl.
df = pd.read_excel(r'X:...\Presentaciones.xlsx', engine='openpyxl')
print(df)
享受吧! :)
我怎么知道您的文件是伪造的 .xls
和非常真实的 .xlsx
?
因为 openpyxl
不适用于 xls
文件。
import pandas as pd
df = pd.read_excel(r'X:...\test.xls', engine='openpyxl')
/*
ERROR:
InvalidFileException: openpyxl does not support the old .xls file format,
please use xlrd to read this file, or convert it to the more recent .xlsx file format.
*/
尝试简单地将 test.xls
重命名为 test.xlsx
也不起作用!
import pandas as pd
df = pd.read_excel(r'X:...\test.xlsx', engine='openpyxl')
/*
Error:
OSError: File contains no valid workbook part
*/
请注意,.xlsx
扩展名(由 Pandas 检测)意味着此文件中可能存在脚本。有时扩展名可能会撒谎,所以小心!
panda 停止支持 xlsx
文件的原因是这些文件存在安全隐患,并且没有人维护这部分代码。