【问题标题】:Open excel file in Python: XLRDError: Excel xlsx file; not supported在 Python 中打开 excel 文件:XLRDError: Excel xlsx file;不支持
【发布时间】:2021-07-27 09:16:43
【问题描述】:

我想在 Python 中打开一个 Excel 文件,使用:

import xlrd

loc = (r"C:\Users\my_path\my_file.xlsx")

wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
sheet.cell_value(0, 0)

它发现了错误:

---------------------------------------------------------------------------
XLRDError                                 Traceback (most recent call last)
<ipython-input-70-b399ced4986e> in <module>
      4 loc = (r"C:\Users\my_path\my_file.xlsx")
      5 
----> 6 wb = xlrd.open_workbook(loc)
      7 sheet = wb.sheet_by_index(0)
      8 sheet.cell_value(0, 0)

C:\Python38\lib\site-packages\xlrd\__init__.py in open_workbook(filename, logfile, verbosity, use_mmap, file_contents, encoding_override, formatting_info, on_demand, ragged_rows, ignore_workbook_corruption)
    168     # files that xlrd can parse don't start with the expected signature.
    169     if file_format and file_format != 'xls':
--> 170         raise XLRDError(FILE_FORMAT_DESCRIPTIONS[file_format]+'; not supported')
    171 
    172     bk = open_workbook_xls(

XLRDError: Excel xlsx file; not supported

怎么了?

【问题讨论】:

  • 你有正确的文件名吗?看起来您正在尝试打开一个目录,而不是其中的特定 Excel 文件;它通常会有一个.xls.xlsx 扩展名...(或.xlsm.xlsb 等)。
  • 嗨@sabik 感谢您指出,请参阅编辑后的问题。
  • 现在错误消息会告诉您究竟出了什么问题:“Excel xlsx 文件;不支持”。根据homepage of the xlrd library,它只能读取“历史 .xls 格式的 Excel 文件”。
  • engine="openpyxl" 传递给pd.read_excel。'
  • 嗨@QuangHoang 谢谢pd.read_excel(r'C:\Users\my_path\my_file.xlsx', engine="openpyxl") 工作!

标签: python excel pandas


【解决方案1】:

最新版本的xlrd只支持.xls文件,所以你可以安装旧版本

pip uninstall xlrd

pip install xlrd==1.2.0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-03
    • 1970-01-01
    • 1970-01-01
    • 2019-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多