问题描述
我正在尝试从OneDrive位置下载Excel文件.我的代码可以正常获取文件,但是文件已损坏(我收到错误消息):
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"
我使用来宾访问excel文件的方式,因此不必进行身份验证.生成的文件似乎为15KB,原始文件为22KB.
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.
推荐答案
您不能仅使用URL直接从OneDrive下载Excel文件.即使在未经授权的情况下共享文件,您仍可能会获得指向中间HTML页面的链接,而不是Excel二进制文件本身.
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.
要从OneDrive下载项目,您首先需要进行身份验证,然后传递您要保存的文件的位置.您可能需要使用OneDrive REST API.有关如何执行此操作的详细信息,请参见 OneDrive适用于Python的SDK GitHub页面,其中包含一些内容入门示例.
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.
这篇关于使用Python从OneDrive下载Excel文件会导致文件损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!