Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing file contents rather than filename to the xlrd module's open_workbook() method

Tags:

python

xlrd

Is there a way to pass the contents of an Excel file, rather than a file name/reference, to the xlrd module's open_workbook() function? I've been trying to use the "file_contents" parameter for this purpose, but haven't had any success with it so far. Thank you.

like image 564
Lamps1829 Avatar asked Oct 21 '22 18:10

Lamps1829


2 Answers

@Ber's comment is correct. You will need to use the getvalue() method of the StringIO object and pass that to the file_content parameter in the function call.

f = StringIO.StringIO(content)
book = xlrd.open_workbook(file_contents = f.getvalue() )
like image 159
David Avatar answered Oct 27 '22 00:10

David


In most places where an open file is needed, a StringIO object will also work.

You just cread a StringIO object from the file data and pass that object as the file to your function.

like image 31
Ber Avatar answered Oct 27 '22 00:10

Ber