- You want to export Google Spreadsheet as a custom PDF file.
If my understanding is correct, how about this sample script? In order to export the custom PDF file, this sample script uses requests.get()
. Please think of this as just one of them.
About access token:
In this sample script, the access token is used. From your script, you are using files_service.files().export_media()
. So I thought that you have already used service = build('drive', 'v3', credentials=creds)
as shown in Quickstart. In this case, you can retrieve the access token as follows.
accessToken = creds.token
Sample script:
When you use this script, please replace your script to the following script. In this sample script, the orientation to landscape, custom margins and the page number at the footer can be set.
accessToken = creds.token
spreadsheetId = '### Spreadsheet ID ###'
sheetId = '0' # sheetId
url = ('https://docs.google.com/spreadsheets/d/' + spreadsheetId + '/export?'
+ 'format=pdf' # export as PDF
+ '&portrait=false' # landscape
+ '&top_margin=0.00' # Margin
+ '&bottom_margin=0.00' # Margin
+ '&left_margin=0.00' # Margin
+ '&right_margin=0.00' # Margin
+ '&pagenum=RIGHT' # Put page number to right of footer
+ '&gid=' + sheetId # sheetId
+ '&access_token=' + accessToken) # access token
r = requests.get(url)
with open('downloadedPDFfile.pdf', 'wb') as saveFile:
saveFile.write(r.content)
print('Done.')
accessToken = creds.token
is access token.
- Please set Spreadsheet ID to
spreadsheetId = '### Spreadsheet ID ###'
.
- Please set each parameter as the query parameters.
Note:
- Unfortunately, in the current stage, there are no query parameters for setting the custom footer using when the Spreadsheet is manually exported, yet. I apologize for this situation. About other query parameters, you can see them at this thread.
- If you want to export all sheets in a Spreadsheet, please remove
'&gid=' + sheetId
.
Reference:
If this was not the result you want, I apologize.