我正在使用此命令以pdf格式下载Google电子表格。
response = files_service.files().export_media( fileId=spreadsheet_id, mimeType='application/pdf').execute()
with open("download.pdf", "wb") as wer:
wer.write(response)
将文件下载为pdf。但是,我需要将方向设置为横向,自定义边距和自定义页脚。如何使用Google Drive API v3进行设置?
谢谢
答案 0 :(得分:1)
如果我的理解是正确的,那么该示例脚本如何?为了导出自定义PDF文件,此示例脚本使用{# Loop for grouping items by year ('set array' for not repeating same years) #}
{% set newArray = [] %}
{% for itemY in season %}
<h2 class="accordion-toggle">
{% if itemY.year not in newArray %}
<strong>Season {{ (itemY.year - 1) ~ " - " ~ itemY.year }}</strong></h2>
{% set newArray = newArray|merge([itemY.year]) %}
{% endif %}
{# Todo: Ordering months in this way: y-1(months n° 09 to 12) + y(months n° 01 to 08).
(Ex.: if the event is taking place in Sept 2019, it should be attributed the year 2020,
because it will be displayed in the season 2019-2020 #}
<div class="accordion-content default">
<div class="row fondAgenda">
{# (In each year) loop for grouping item by month ('set array' for not repeating them).
'groupBy' via the QueryBuilder is not working well with Join/addSelect.
The month numbers will be replaced by their real name (other than English) #}
{% set newArrayM = [] %}
{% for itemM in season %}
<div class="col-xs-12">
{% if itemM.month not in newArrayM %}
<h1 class="agendaMois">{{ itemM.month|upper }}</h1>
{% set newArrayM = newArrayM|merge([itemM.month]) %}
{% endif %}
</div>
<div class="col-xs-12 col-sm-6">
{# Loop for displaying Events of each month of the season (ideally m09(y-1) to m08(y) #}
{% for itemE in season %}
<p><strong>{{ itemE.name|upper ~ " > " ~ itemE.city|upper }}</strong><br>
{# Place name, etc. <br> #}
{# Event dates (string ?)</p> #}
{% endfor %}
</div>
{% endfor %}
</div>
</div>
{% endfor %}
。请只考虑其中之一。
在此示例脚本中,使用了访问令牌。在脚本中,您正在使用requests.get()
。因此,我认为您已经使用了Quickstart中所示的files_service.files().export_media()
。在这种情况下,您可以按以下方式检索访问令牌。
service = build('drive', 'v3', credentials=creds)
使用此脚本时,请将您的脚本替换为以下脚本。在此示例脚本中,可以设置横向的方向,自定义边距和页脚处的页码。
accessToken = creds.token
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
。spreadsheetId = '### Spreadsheet ID ###'
。如果这不是您想要的结果,我表示歉意。