Open an excel from http website using xlrd

久未见 提交于 2020-06-28 06:42:25

问题


I trying to open an excel file from web using xlrd, in Python 3.5.4.

import requests
import xlrd
import urllib

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

I'm getting this error.

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

Anyone have a hint?

Thanks!


回答1:


The urlretrieve returns a tuple, not the url content.

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)



回答2:


Something like this might work.

import urllib2

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


来源:https://stackoverflow.com/questions/46437357/open-an-excel-from-http-website-using-xlrd

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!