首页 > 解决方案 > 如何使用 Pandas.read_excel 在内存中打开 XLS 文件?

问题描述

我有一个文件,我正在尝试使用 Pandas.read_excel 打开它。我收到这样的错误 -

无效文件:

InMemoryUploadedFile:file.xlsx (application/vnd.openxmlformats-officedocument.spreadsheetml.sheet)

标签: python-3.xpandasxlsxxlrd

解决方案


首先,您需要使用 xlrd 打开文件,然后使用参数 engine="xlrd" 将结果传递给 pandas.read_excel

import pandas as pd
import xlrd

book = xlrd.open_workbook(file_contents=myfile)
df = pd.read_excel(book,engine='xlrd')
print(df.head())

这里“myfile”是你的内存文件


推荐阅读