如何以编程方式将 G Suite docs/sheets 下载到 pdf/xls?

How to Download G Suite docs/sheets to pdf/xls programatically?

我正在尝试通过 CLI.

以编程方式将 Google 文档下载到 PDF 或 Sheet 到 XLS,给定一个 ID ]

到目前为止我尝试过的步骤:

  1. Contact support,但看不到 (?) 帮助图标
  2. Google 10 分钟...我认为 Google Drive API 这样做(不确定)
  3. 启用Google Drive API
  4. 注册了 GCP 项目
  5. 导航认为 UI 到 enable the API
  6. 尝试 GET API results in 400 Invalid field selection 使用文档 ID 的字段

我现在有点卡住了,我不确定如何进行。有什么建议吗?

警告:希望 前面的信息墙!我还上传了完整的 Jupyter Notebook 供您克隆和 运行 here 因为,正如您所意识到的,将这类东西放在一起可能具有挑战性。


由于我们要通过 google 驱动器 API 导出文件,我们需要该范围的凭据,如 https://developers.google.com/drive/api/v3/reference/files/export#auth 中所述。

但是,首先我们需要选择一种身份验证方法,详见 https://developers.google.com/identity/protocols/oauth2#scenarios

既然你提到了创建 GCP 项目,我假设你有兴趣使用 GCP 服务帐户 详见 https://developers.google.com/identity/protocols/oauth2#serviceaccount

您可以在 https://console.developers.google.com/apis/credentials 创建一个服务帐户 或如 https://developers.google.com/identity/protocols/oauth2/service-account#creatinganaccount

中所述

确保在创建服务帐户时为该服务帐户启用全域委派,并在 https://admin.google.com/ac/owl/domainwidedelegation 下授予它 https://www.googleapis.com/auth/drive 范围,否则您将无法冒充其他用户,包括自己,并下载他们的文件。

然后我们使用刚刚下载的 SERVICE_ACCOUNT_FILE 和定义的 SCOPES 创建一个 Credentials 对象。

但是,您需要先根据 https://developers.google.com/drive/api/v3/quickstart/python (pip3 install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib)[=41 安装 Google API 的 Python 绑定=]

这样,下面的内容就足以验证 API:

from googleapiclient.discovery import build
from google.oauth2 import service_account

SCOPES = ['https://www.googleapis.com/auth/drive']
SERVICE_ACCOUNT_FILE = 'credentials.json'

credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE,
                                                      scopes=SCOPES)

# Remember, you must have created credentials.json with domain-wide delegation!
credentials = credentials.with_subject('user@example.com')

# We then build a drive_v3 service using the credentials we just created

service = build('drive', 'v3', credentials=credentials)

我们可以访问 files 资源,如 https://developers.google.com/drive/api/v3/reference/files/get and request the metadata of a file to which user@example.com has access https://docs.google.com/document/d/fileId/edit 所示。在你的情况下 fileId=141g8UkQfdMQSTfIn475gHj1ezZVV16f5ONDxpWrrvts.

files = service.files()
print(service.files().get(fileId='1U3eMevKxTwDxzvOBUsqa36zvwBzKPVYOFgy3k_9vxb8').execute())

{'kind': 'drive#file', 'id': '1U3eMevKxTwDxzvOBUsqa36zvwBzKPVYOFgy3k_9vxb8', 'name': 'empty', 'mimeType': 'application/vnd.google-apps.document'}

我们再次访问文件资源,但这次是导出文件,详见 https://developers.google.com/resources/api-libraries/documentation/drive/v3/python/latest/drive_v3.files.html#export 这也可以使用 https://developers.google.com/drive/api/v3/manage-downloads.

来实现

https://developers.google.com/drive/api/v3/ref-export-formats.

中列出了有效的 MIME 类型
fconr = files.export(fileId='1U3eMevKxTwDxzvOBUsqa36zvwBzKPVYOFgy3k_9vxb8',
mimeType='application/vnd.openxmlformats-officedocument.wordprocessingml.document')

fcont = fconr.execute()

print('{}...'.format(fcont[:10]))

file = open("/tmp/sample.doc", "wb")
file.write(fcont)
file.close()

b'MN\xc30\x10\x85O\xc0\x1d"'...

如您所见,fcont 包含一个与文档相对应的二进制 blob,我显示了它的前 10 个字节。最后,blob 被保存到 sample.doc.

ls -alh1 /tmp/sample.doc

-rw-rw-r-- 1 jdsalaro jdsalaro 6,0K Jan 20 23:38 /tmp/sample.doc

如上所述,我鼓励您尝试使用 Jupyter notebook 创建具有全域委派的服务帐户,将其保存到 credentials.json 并授予它 https://www.googleapis.com/auth/drive范围。