Domanda

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!

È stato utile?

Soluzione

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

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

Altri suggerimenti

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)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top