0

I have a google sheet and I want to export it as a PDF with custom setting, such as:

size=6.29921x23.2283
portrait=true
fitw=true&source=labnol
sheetnames=false&printtitle=false
pagenumbers=false&gridlines=false

I'm using from googleapiclient.discovery import build and sending this request:

serivices = build('drive', 'v3', credentials=credentials)
serivices.files().export(fileId = google_sheet_id, mimeType = 'application/pdf').execute()

It's any way to add this setting to the request?
Thank you for your time

Godoy32
  • 35
  • 5

1 Answers1

0

I believe your goal is as follows.

  • You want to export a Google Spreadsheet as a PDF file.

  • When the Spreadsheet is converted to a PDF format, you want to inglude the following settings.

    size=6.29921x23.2283
    portrait=true
    fitw=true&source=labnol
    sheetnames=false&printtitle=false
    pagenumbers=false&gridlines=false
    
    • I guessed that the unit of 6.29921x23.2283 is inch.
  • You are using googleapis for python. And credentials of serivices = build('drive', 'v3', credentials=credentials) can be used for downloading the Spreadsheet in PDF format.

In this case, how about the following sample script? In this case, the access token is retrieved from your credentials. And, the endpoint is requested with the requests module.

Sample script:

serivices = build('drive', 'v3', credentials=credentials) # This is from your script.

# If your "creds" is from the service account, please enable the following line. If your "creds" is from OAuth2, please disable the following line.
# credentials.refresh(google.auth.transport.requests.Request())

accessToken = credentials.token

outputFilename = "sample.pdf" # Please set the output filename.
spreadsheetId = "###" # Please set your Spreadsheet ID.
q = {
    'format': 'pdf',
    'size': '6.29921x23.2283',
    'portrait': 'true',
    'fitw': 'true',
    'source': 'labnol',
    'sheetnames': 'false',
    'printtitle': 'false',
    'pagenumbers': 'false', # or 'pagenum': 'UNDEFINED',
    'gridlines': 'false'
}
queryParameters = urllib.parse.urlencode(q)
url = f'https://docs.google.com/spreadsheets/d/{spreadsheetId}/export?{queryParameters}'
headers = {'Authorization': 'Bearer ' + accessToken}
res = requests.get(url, headers=headers)
with open(outputFilename, 'wb') as f:
    f.write(res.content)
  • Please add the following import.

    import urllib.parse
    import requests
    
  • When this script is run, the Google Spreadsheet is exported as a PDF format by customizing with the values of q.

  • About the comment line of # credentials.refresh(google.auth.transport.requests.Request()), unfortunately, from your question, I couldn't know which you are using the OAuth2 or the service account. So, I proposed a script for both patterns. When this line is put as a comment, it is for the OAuth2. When this line is put as a script by removing #, it is for the service account. Please be careful about this.

References:

Tanaike
  • 181,128
  • 11
  • 97
  • 165