【问题标题】:Getting the excel file after df.to_excel(...) with Panda使用 Panda 在 df.to_excel(...) 之后获取 excel 文件
【发布时间】:2017-03-22 14:08:55
【问题描述】:

我正在使用 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) 返回None

【问题讨论】:

    标签: python excel pandas firebase writer


    【解决方案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)
    

    【讨论】:

      【解决方案2】:

      根据你应该添加的文档

      writer.save()
      

      source

      【讨论】:

      • 是的,但它会保存在我的服务器上。我只想将 Excel 对象放入我的(短期)内存中,并使用它将它存储在 Firebase 上而不是我的服务器上。例如。当您在 Javascript 中使用 FileReader 时,它会创建一个 File Blob。这个你可以输入到外部保存。
      • 你可以试试storage.child(childRef).put(writer.save())
      • 试过了。不工作。我也尝试过 .put(df) 但这没有用。一定有办法获取文件对象。
      • 我认为因为 Pyrebase 是一个 API 包装器,所以您必须具体化文件才能上传它。上传成功后即可删除。
      猜你喜欢
      • 1970-01-01
      • 2022-08-17
      • 1970-01-01
      • 2017-04-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多