用pandas获取df.to_excel(…)后的excel文件

我正在使用Pyrebase将我的file upload到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()然后我得到适当的文件types( xlsx ),但它存储在我的服务器(我想避免)。 我如何才能生成正确的文件types,就像一个writer.save()

注意: print(excelFile)返回None

它可以通过使用本地内存来解决:

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

根据你应该添加的文档

 writer.save() 

资源