Using Python to download an Excel file from OneDrive results in corrupt file

耗尽温柔 提交于 2019-12-24 08:47:01

问题


I am trying to download an excel file from a OneDrive location. My code works okay to get the file, but the file is corrupt (I get an error message):

import urllib2

data = urllib2.urlopen("enter url here")
with open('C:\\Video.xlsx', 'wb') as output:
    output.write(data.read())
output.close()
print "done"

I use the guest access to the excel file so that I don't have to work with authentication. The resulting file seems to be 15KB, the original is 22KB.


回答1:


You can't just download the Excel file directly from OneDrive using a URL. Even when you would share the file without any authorization, you'll probably still get a link to an intermediate HTML page, rather than the Excel binary itself.

To download items from your OneDrive, you'll first need to authenticate and then pass the location of the file you're after. You'll probably want to use the OneDrive REST API. The details on how to do that are documented on the OneDrive's SDK for Python GitHub page with some examples to get you started.



来源:https://stackoverflow.com/questions/38531278/using-python-to-download-an-excel-file-from-onedrive-results-in-corrupt-file

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