Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:Pass`gridlines=false`使用“导出媒体”(Google Drive API)下载PDF时_Python_Google Drive Api - Fatal编程技术网

Python:Pass`gridlines=false`使用“导出媒体”(Google Drive API)下载PDF时

Python:Pass`gridlines=false`使用“导出媒体”(Google Drive API)下载PDF时,python,google-drive-api,Python,Google Drive Api,我已经按照驱动器API v3文档进行了操作。我能够成功下载PDF格式的电子表格,如示例所示,生成如下请求: from googleapiclient.discovery import build drive_service = build('drive', 'v3', credentials=creds) SPREADSHEET_ID = 'mySpreadSheetID' request = drive_service.files().export_media(fileId=SPREADS

我已经按照驱动器API v3文档进行了操作。我能够成功下载PDF格式的电子表格,如示例所示,生成如下请求:

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',
                                             )
我希望作为请求的一部分从中传递自定义参数,特别是设置
gridlines=false

我从文档中发现,我可以通过以下方式进一步检查请求:

request.to_json()
这给了我:

('{"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}')```
我怀疑在发出请求之前需要将
&gridlines=false
附加到
uri
,但是我不确定如何修改此选项

我的思路是否正确?如果没有,是否有其他方法从Python库传递这些参数?
  • 您希望将Google电子表格导出为PDF文件
  • 您希望使用查询参数
    gridlines=false
  • 您希望使用python实现这一点
  • 您的访问令牌可用于导出Google电子表格
如果我的理解是正确的,那么这个答案呢?请把这看作是几个可能的答案之一

修改点:
  • 在此修改中,通过直接修改
    请求
    的端点导出
    gridlines=false
    的PDF文件
修改脚本:
  • 在这种情况下,您还可以使用
    request=drive\u service.files().export\u media(fileId=“”,mimeType=“”)
    。因为端点被修改了

如果我误解了你的问题,而这不是你想要的方向,我道歉。

太好了。就这么简单!最初,我尝试了相同的方法,除了
googleapis.com/drive/v3/files/
URL,但这似乎忽略了提供的网格线参数。不幸的是,API文档中似乎没有清楚地记录这种方法。再次感谢@谢谢你的回复。我很高兴你的问题解决了。
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))