Write to StringIO object using Pandas Excelwriter?

后端 未结 3 1855
夕颜
夕颜 2020-11-30 10:37

I can pass a StringIO object to pd.to_csv() just fine:

io = StringIO.StringIO()
pd.DataFrame().to_csv(io)

But when using the excel writer,

3条回答
  •  一生所求
    2020-11-30 10:54

    For those not using xlsxwriter as their engine= for to_excel here is a solution to use openpyxl in memory:

    in_memory_file = StringIO.StringIO()
    xlw = pd.ExcelWriter('temp.xlsx', engine='openpyxl')
    # ... do many .to_excel() thingies
    xlw.book.save(in_memory_file)
    # if you want to read it or stream to a client, don't forget this
    in_memory_file.seek(0)
    

    explanation: the ExcelWriter wrapper class exposes the engines individual workbook through the .book property. For openpyxl you can then use the Workbook.save method as usual!

提交回复
热议问题