2017-03-22 16 views
1

我使用Pyrebase將我的文件上傳到Firebase。在df.to_excel(...)與熊貓之後獲取excel文件

我有一個數據幀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)回報None

回答

1

可以解決:

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

根據文檔你應該增加使用本地內存

writer.save() 

source

+0

是的,但後來它將它保存在我的服務器上。我只想將Excel對象存儲到我的(短期)內存中,並將其用於存儲在Firebase上,而不是存儲在我的服務器上。 E.g.當您在Javascript中使用FileReader時,它會創建一個文件Blob。這可以輸入以保存到外部。 – JohnAndrews

+0

你可以嘗試'storage.child(childRef).put(writer.save())' – philshem

+0

試過。不工作。我也試過.put(DF),但沒有奏效。必須有一種方法來獲取文件對象。 – JohnAndrews