Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write an Excel file to memory?

I am writing a set of records in excel but I am not supposed to create a physical file. How do I write the records in excel in memory? If I do write it, how do I see the output of the records? I have tried:

import xlsxwriter
import io

filename= io.BytesIO()
workbook=xlsxwriter.Workbook(filename,{'in_memory': True})
worksheet=workbook.add_worksheet('sheet1')
worksheet.write(1,1,'Testing')
print(workbook.get_worksheet_by_name('sheet1'))
workbook.close()
like image 319
Kumar Govind Avatar asked Dec 20 '25 05:12

Kumar Govind


1 Answers

From the XlsxWriter docs:

It is possible to write files to in-memory strings using BytesIO as follows:

    from io import BytesIO

    output = BytesIO()
    workbook = xlsxwriter.Workbook(output)
    worksheet = workbook.add_worksheet()

    worksheet.write('A1', 'Hello')
    workbook.close()


    xlsx_data = output.getvalue()

To avoid the use of any temporary files and keep the entire file in-memory use the in_memory constructor option shown above.

See also Example: Simple HTTP Server.

like image 105
jmcnamara Avatar answered Dec 21 '25 20:12

jmcnamara