Question

I have an Excel file created inside a cStringIO variable.

I need to open it and read it. But to open an excel file with the xlrd function xlrd.open_workbook(excel_file_name), I need to call it by its file name. But in this case there is no file name because it is a cStrinIO variable that contains the representation of the Excel file.

How can I convert the cStringIO variable into a real excel file that I can open?

Thank you!

Was it helpful?

Solution

Looks like xlrd.open_workbook() accepts file_contents argument as well, so maybe as follows?

xlrd.open_workbook(file_contents=cstringio_var.getvalue())

OTHER TIPS

You can use tempfile.NamedTemporaryFile.

Example (not tested):

with tempfile.NamedTemporaryFile() as f:
    f.write(your_cStringIO_variable.read())
    f.flush()
    something = xlrd.open_workbook(f.name)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top