Saving openpyxl file via text and filestream

Nelson Shaw picture Nelson Shaw · Dec 12, 2011 · Viewed 14.2k times · Source

I'm building OpenPyXL into an application that expects a string containing the content of the excel file, for it to write via file stream.

From my investigation into the OpenPyXL source code, it doesn't look like it supports this kind of output. Does anyone have any experience with modifying openpyxl to support this?

Or any general advice/workarounds?

Thanks.

Answer

Eric Gazoni picture Eric Gazoni · Jan 3, 2012

jcollado's answer is actually valid, but there is also a function (sadly not documented yet) called "save_virtual_workbook" in openpyxl.writer.excel that will take your workbook and return the workbook as a string:

from openpyxl.workbook import Workbook
from openpyxl.writer.excel import save_virtual_workbook

wb = Workbook()
print save_virtual_workbook(wb)

What you're looking for is the string returned by save_virtual_workbook()