使用 xlrd 从 http 网站打开 excel

Open an excel from http website using xlrd

我试图在 Python 3.5.4 中使用 xlrd 从 Web 打开一个 excel 文件。

import requests
import xlrd
import urllib

link='http://www.bla.com/bla.xlsx'
request = urllib.request.urlretrieve(link) 
workbook = xlrd.open_workbook(request)  

我遇到了这个错误。

TypeError: invalid file: ('0xlxs', <http.client.HTTPMessage object at 0x04600590>)

有人有提示吗?

谢谢!

这样的事情可能会奏效。

import urllib2

req = urllib2.Request('http://www.bla.com/bla.xlsx')
response = urllib2.urlopen(req)
workbook = xlrd.open_workbook(response.read())

url检索 returns 元组,而不是 url 内容。

urllib.request.urlretrieve(url, filename=None, reporthook=None, data=None)

Returns a tuple (filename, headers) where filename is the local file name under which the object can be found, and headers is whatever the info() method of the object returned by urlopen() returned (for a remote object).

import requests
import xlrd
import urllib

link = 'https://raw.githubusercontent.com/SheetJS/test_files/a9c6bbb161ca45a077779ecbe434d8c5d614ee37/AutoFilter.xls'
file_name, headers = urllib.request.urlretrieve(link)
print (file_name)
workbook = xlrd.open_workbook(file_name)
print (workbook)