0

Can I read directly from an HTTP location for use with xlrd?

I've tried the following:

import ntlm, urllib2
url = 'http://myurl/file.xls'
passman.add_password(None, url, login, password)
auth_NTLM = HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(passman)
opener = urllib2.build_opener(auth_NTLM)
sock = urllib2.urlopen(url)
content = sock.read()

and have a function ReadFromExcel that reads an Excel file and returns some data, but it can't read from content.

ReadFromExcel(content) 
    book = xlrd.open_workbook(filename)
  File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 400, in open_workbook
    f = open(filename, "rb")
TypeError: file() argument 1 must be encoded string without NULL bytes, not str

any ideas?

pedram
  • 2,931
  • 3
  • 27
  • 43

1 Answers1

3

You should pass a file name instead of file's content:

import os
import tempfile

with tempfile.NamedTemoraryFile(suffix='.xls') as file:
    file.write(content)
    file.delete = False

try:
    result = ReadFromExcel(file.name)
finally:
    os.remove(file.name)

Or use file_contents parameter.

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670