Python:Pandas read_excel无法打开.xls文件,不支持xlrd

Map*_*ofu 6 python excel xlrd pandas openpyxl

问题:

我正在使用 pd.read_excel 打开 .xls,但出现错误。

df_cima = pd.read_excel("docs/Presentaciones.xls")

xlrd.biffh.XLRDError: Excel xlsx file; not supported
Run Code Online (Sandbox Code Playgroud)

该文件的后缀是 .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.
Run Code Online (Sandbox Code Playgroud)

我的环境:

  • 熊猫版本:1.1.5
  • xlrd版本:2.0.1
  • openpyxl版本:3.0.6

我不想将 xlrd 版本更改回 1.2.0,从其他答案中我看到新版本的 xlrd 仅支持 .xls,但我不明白为什么它不适用于我的文件。

提前致谢。

Flo*_*yer 10

我坚持认为,您的文件不是 .xls!:)

使用 Pandas 1.1.5 和 xlrd 2.1.0

重命名Presentaciones.xlsPresentaciones.xlsx.

import pandas as pd
# Use openpyxl.
df = pd.read_excel(r'X:...\Presentaciones.xlsx', engine='openpyxl')
print(df)
Run Code Online (Sandbox Code Playgroud)

享受!:)

更多信息

我怎么知道你的文件是假的.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.
*/
Run Code Online (Sandbox Code Playgroud)

并且尝试简单地重命名test.xlstest.xlsx也不起作用!

import pandas as pd
df = pd.read_excel(r'X:...\test.xlsx', engine='openpyxl')
/*
    Error:
    OSError: File contains no valid workbook part
*/
Run Code Online (Sandbox Code Playgroud)

历史

请注意,.xlsx扩展名(由 pandas 检测到)意味着该文件中可能存在脚本。有时分机可能会撒谎,所以要小心

panda 停止支持文件的原因xlsx这些文件存在安全隐患,并且没有人维护这部分代码。