4

I am getting an Excel file using urllib2 and saving into response below. I want to be able to process this excel file using xlrd or similar. I included some info below, let me know if I can provide more info. How can I have response object transformed into an object I can play with?

response = <addinfourl at 199999998 whose fp = <socket._fileobject object at 0x100001010>>

response.read() prints: '\xd0\xcf\x11\xe0...'

Headers:
Content-Type: application/vnd.ms-excel
Transfer-Encoding: chunked
Ulas Keles
  • 1,681
  • 16
  • 20

1 Answers1

5

Using xlrd, and based on its API documentation, it appears like you can use something similar to this:

book = xlrd.open_workbook(file_contents=response.read())

It doesn't appear to support reading a file object (which, IMO, would be ideal), only taking in a filename itself or the above file_contents method.

If file_contents didn't exist or didn't work, you'd have to use tempfile to write the response to a temporary file and read that.

Mattie
  • 20,280
  • 7
  • 36
  • 54
  • Thanks, @zigg. When I first tried this, I had `xlrd.open_workbook(file_contents=response)` but no `.read()` That was causing an error, `AttributeError: addinfourl instance has no attribute '__len__'` – Martin Burch Feb 18 '14 at 18:45