【发布时间】: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
xlrdlibrary,它只能读取“历史 .xls 格式的 Excel 文件”。 -
将
engine="openpyxl"传递给pd.read_excel。' -
嗨@QuangHoang 谢谢
pd.read_excel(r'C:\Users\my_path\my_file.xlsx', engine="openpyxl")工作!