Skip to content Skip to sidebar Skip to footer

Using Python To Download An Excel File From Onedrive Results In Corrupt File

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 = urllib

Solution 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.

Solution 2:

I got it. The url has the format below:

'https://onedrive.live.com/view.aspx?cid=.....app=Excel'

So, all that I had to do was change "view" to "download" at that url, and used the code below:

import urllib.requesturl='https://onedrive.live.com/view.aspx?cid=.....app=Excel'

urllib.request.urlretrieve(url, "test.xlsx")

Post a Comment for "Using Python To Download An Excel File From Onedrive Results In Corrupt File"