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, I am having a lot more trouble.
io = StringIO.StringIO()
writer = pd.ExcelWriter(io)
pd.DataFrame().to_excel(writer,"sheet name")
writer.save()
Returns an
AttributeError: StringIO instance has no attribute 'rfind'
I'm trying to create an ExcelWriter object without calling pd.ExcelWriter() but am having some trouble. This is what I've tried so far:
from xlsxwriter.workbook import Workbook
writer = Workbook(io)
pd.DataFrame().to_excel(writer,"sheet name")
writer.save()
But now I am getting an AttributeError: 'Workbook' object has no attribute 'write_cells'
How can I save a pandas dataframe in excel format to a StringIO object?
to_excelis a path to an Excel file, not an actual file object. Why do you want to create an in-memory representation of an Excel file anyway? - BrenBarnio.BytesIO, as the output of writing an Excel file is a series of bytes, not a (unicode) string. - LeoRochael