1

I've followed the Drive API v3 docs to Download a Google Document. I am able to sucessfully download a spreadsheet in PDF format, as per the example, building the request as follows:

from googleapiclient.discovery import build

drive_service = build('drive', 'v3', credentials=creds)

SPREADSHEET_ID = 'mySpreadSheetID'
request = drive_service.files().export_media(fileId=SPREADSHEET_ID,
                                             mimeType='application/pdf',
                                             )

I wish to pass the custom params from this answer as part of the request, specifically to set gridlines=false.

I found from the docs for googleapiclient.http.HttpRequest that I can inspect the request further with:

request.to_json()

which gives me:

('{"uri": '
 '"https://www.googleapis.com/drive/v3/files/mySpreadSheetID/export?mimeType=application%2Fpdf&alt=media", '
 '"method": "GET", "body": null, "headers": {"accept": "*/*", '
 '"accept-encoding": "gzip, deflate", "user-agent": "(gzip)", '
 '"x-goog-api-client": "gdcl/1.7.11 gl-python/3.7.4"}, "methodId": '
 '"drive.files.export", "resumable": null, "response_callbacks": [], '
 '"_in_error_state": false, "body_size": 0, "resumable_uri": null, '
 '"resumable_progress": 0}')```

I suspect &gridlines=false needs to be appended to the uri before the request is made, however I'm unsure how to modify this.

Am I on the right track, and if not, is there another way to pass these parameters from the Python library?

Tanaike
  • 181,128
  • 11
  • 97
  • 165
v25
  • 7,096
  • 2
  • 20
  • 36

1 Answers1

2
  • You want to export the Google Spreadsheet as a PDF file.
  • You want to use the query parameter of gridlines=false.
  • You want to achieve this using python.
  • Your access token can be used for exporting the Google Spreadsheet.

If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

Modification point:

  • In this modification, the PDF file with gridlines=false is exported by directly modifying the endpoint of requests.

Modified script:

SPREADSHEET_ID = "###"  # Please set the Spreadsheet ID.

drive_service = build('drive', 'v3', credentials=creds)
request = drive_service.files().export_media(fileId=SPREADSHEET_ID, mimeType='application/pdf')

# Here, the endpoint is modified.
request.uri = "https://docs.google.com/spreadsheets/d/" + SPREADSHEET_ID + "/export?format=pdf&gridlines=false"

fh = io.FileIO('sample.pdf', mode='wb')
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
    status, done = downloader.next_chunk()
    print('Download %d%%.' % int(status.progress() * 100))
  • In this case, you can also use request = drive_service.files().export_media(fileId="", mimeType=""). Because the endpoint is modified.

If I misunderstood your question and this was not the direction you want, I apologize.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Fantastic. It was this simple! Initially I tried the same, except with the `googleapis.com/drive/v3/files/` URL, but this seems to ignore the gridline param when provided. Unfortunately this method doesn't appear to be clearly documented in the API docs. Thanks again! – v25 Mar 05 '20 at 15:23
  • @v25 Thank you for replying. I'm glad your issue was resolved. – Tanaike Mar 05 '20 at 22:50