简体   繁体   English

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

[英]Getting the excel file after df.to_excel(…) with Panda

I am using Pyrebase to upload my files to Firebase. 我正在使用Pyrebase将我的文件上传到Firebase。

I have a DataFrame df and convert it to an Excel File as follows: 我有一个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)

However, this stores the Excel file as an Office Spreadsheet with zero bytes. 但是,这会将Excel文件存储为零字节的Office电子表格。 If I run writer.save() then I do get the appropriate filetype ( xlsx ), but it is stored on my Server (which I want to avoid). 如果我运行writer.save()然后我得到相应的文件类型( xlsx ),但它存储在我的服务器上(我想避免)。 How can I generate the right filetype as one would do with writer.save() ? 如何生成正确的文件类型,就像使用writer.save()

Note: print(excelFile) returns None 注意: print(excelFile)返回None

It can be solved by using local memory: 它可以通过使用本地内存来解决:

# 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)

According to the documentation you should add 根据你应该添加的文档

writer.save()

source 资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM