2

I am using the Google Drive API to export Google Sheets and save them locally but I've encountered the following problem:

One of the sheets is not downloading properly. I'm not sure if it's because of its size or complexity but by using the proposed code in the documentation the process gets stuck for several minutes and them times out.

Below is the code indicated in the documentation with minor changes to my work flow

file_id = '0BwwA4oUTeiV1UVNwOHItT0xfa2M'  # Changed to my file id
mimetype = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'  # MS Excel
request = drive_service.files().export_media(fileId=file_id, mimeType=mimetype)
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while not done:
    status, done = downloader.next_chunk()
if done:
    # process the downloaded file

Since I didn't need all the sheets, I tried another solution by requesting the export manually using a HTTP request and setting the gid=0 parameter on the URL to indicate the desired sheet.

Firstly I tried the simple get API call just to make sure it works, as follows:

curl https://www.googleapis.com/drive/v3/files/<my_file_id>?access_token=<my_access_token>

That worked fine and I got a response with the file details.

After that, I tried the export request below but I always get a TEMPORARY REDIRECT response even though the request is the same as in the documentation.

curl https://www.googleapis.com/drive/v3/files/<my_file_id>/export?mimeType=application%2Fvnd.openxmlformats-officedocument.spreadsheetml.sheet&access_token=<my_access_token>
<HTML>
<HEAD>
<TITLE>Temporary Redirect</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Temporary Redirect</H1>
The document has moved <A HREF="big_giant_huge_url">here</A>.
</BODY>
</HTML>

So my questions are:

  • is there any way to specify the sheets to download using the official Python API or resolve the download problems?
  • anyone knows the cause for the Temporary Redirect in my HTTP request?
  • is there another solution to download the sheets?

Thanks!

Rob
  • 26,989
  • 16
  • 82
  • 98
Ícaro
  • 1,432
  • 3
  • 18
  • 36
  • 2
    This might help: http://stackoverflow.com/questions/3287651/download-a-spreadsheet-from-google-docs-using-python – abielita Feb 26 '16 at 02:58

1 Answers1

0

So, to put and end in this after a long time, we've could not find a solution to this problem and ended up using a different method to approach this situation.

Here's what we did:

  • searched all the unique sheets we wanted
  • copied (linked) them to a separate file on Drive
  • used the API to fetch each of those files separately

This resulted in success since it was a pretty straight forward solution using the official APIs.

It was kinda of a hassle to have to copy everything to other files but AFAIK it is the only way to do it for now.

Ícaro
  • 1,432
  • 3
  • 18
  • 36
  • Did you save the file to disk? As I am having difficulties doing this – mp252 Jun 22 '17 at 16:22
  • @mp252 not really, I used the exact code in the original question to download the file to memory and process it. You can check the [documentation](https://developers.google.com/drive/v3/web/manage-downloads) for more examples. What difficulties are you having? – Ícaro Jun 22 '17 at 18:31