使用 Panda 在 df.to_excel(...) 之后获取 excel 文件

Getting the excel file after df.to_excel(...) with Panda

我正在使用 Pyrebase 将我的文件上传到 Firebase。

我有一个 DataFrame df 并将其转换为 Excel 文件,如下所示:

writer      = ExcelWriter('results.xlsx')
excelFile   = df.to_excel(writer,'Sheet1')

print(excelFile)

# Save to firebase
childRef        = "path/to/results.xlsx"

storage         = firebase.storage()
storage.child(childRef).put(excelFile)

但是,这会将 Excel 文件存储为零字节的 Office 电子表格。如果我 运行 writer.save() 那么我确实得到了适当的文件类型(xlsx),但它存储在我的服务器上(我想避免)。我怎样才能像使用 writer.save()?

那样生成正确的文件类型

注:print(excelFile) returns None

根据文档你应该添加

writer.save()

source

使用本地内存可以解决:

# init writer
bio         = BytesIO()
writer      = pd.ExcelWriter(bio, engine='xlsxwriter')
filename    = "output.xlsx"

# sheets
dfValue.to_excel(writer, "sheetname")

# save the workbook
writer.save()
bio.seek(0)      

# get the excel file (answers my question)          
workbook    = bio.read()  
excelFile   = workbook

# save the excelfile to firebase
# see also issue: https://github.com/thisbejim/Pyrebase/issues/142
timestamp       = str(int(time.time()*1000));
childRef        = "/path/to/" + filename

storage         = firebase.storage()
storage.child(childRef).put(excelFile)
fileUrl         = storage.child(childRef).get_url(None)