为什么 Python 中的 xlrd 库无法打开 xlsx 文件?

Why can't xlrd library in Python open xlsx file?

我正在尝试使用 Python 读取 .xlsx 文件。 但是我注意到流行的库 xlrd 不适用于 .xlsx 文件。 这是我收到错误消息的示例:

import xlrd

if __name__ == '__main__':
    loc = ("./excel_file.xlsx")
    wb = xlrd.open_workbook(loc)

我在尝试 运行 上面的代码时收到以下错误消息:

Traceback (most recent call last):
  File "/Users/username/projects/excel_processing_example/main.py", line 15, in <module>
    wb = xlrd.open_workbook(loc)
  File "/Users/username/projects/excel_processing_example/venv/lib/python3.8/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

xlrd 库不支持 xlsx 文件是否有原因?

在 2.0.0 版(2000 年 12 月)中删除了支持,请参阅 https://xlrd.readthedocs.io/en/latest/changes.html#id1

Remove support for anything other than .xls files.

来自PyPI

xlrd is a library for reading data and formatting information from Excel files in the historical .xls format.

2020 年做出的选择是只关注 .xls 格式,因为他们不想复制出色的 openpyxl 项目所做的工作。

XLSX 格式是继承自 Office Open XML while XLS is proprietary format of Microsoft 的 XML 格式。

因此解析 .xls 文件与解析 .xlsx.

完全不同

如果要读取xlsx文件,可以使用openpyxl