将 Google Sheet 转换为 PDF 并存储在与 sheet 相同的 Google Drive 文件夹中

Convert Google Sheet to PDF and store in same Google Drive folder as sheet

设置

我的文件夹中有一个空的 Google Sheet 发票模板。

使用 Python、Gspread 和 PyDrive,我复制了模板并用数据填充了副本。


问题

客户需要收到发票的 pdf 格式,而不是 google sheet。

我遇到了 this question,其中说明了如何在本地将 sheet 导出为 pdf,即

file_obj = drive.CreateFile({'id': 'sfkldjlfsdkjfgdklj'})
file_obj.GetContentFile('temp.pdf', mimetype='application/pdf')

如何创建 google sheet 的 PDF,而不是在本地下载 PDF,而是将其存储在与 Google 相同的 Google Drive 文件夹中Sheet?

您可以使用驱动器 API 的 Files.export 方法,将 sheet 转换为 'application/pdf' mime 类型。 在 Files API explorer.

中试用
  • 您想将 Google 电子表格转换为 PDF 格式,并在 Google 电子表格的同一文件夹中将其创建为一个文件。
  • 在这种情况下,PDF 文件是在 Google 驱动器上创建的。
  • 您想通过 Python.
  • 使用 pydrive 来实现此目的

问题和解决方法:

遗憾的是,Google 电子表格无法使用 Google API 直接转换和导出到 Google 驱动器。在这种情况下,需要使用变通方法。在这个答案中,我想提出以下 2 个解决方法。

模式 1:

在此模式中,您的目标是使用 pydrive 实现的。在这种情况下,电子表格以PDF文件的形式下载到本地PC,并将PDF文件上传到Google驱动器上与电子表格相同的文件夹。

示例脚本:

spreadsheet_id = '###'  # Please set the Spreadsheet ID.
pdf_filename = 'sample.pdf'

drive = GoogleDrive(gauth)

# Download Spreadsheet as PDF file.
dl_file = drive.CreateFile({'id': spreadsheet_id})
dl_file.GetContentFile(pdf_filename, mimetype='application/pdf')

# Get parent folder ID of the Spreadsheet.
url = 'https://www.googleapis.com/drive/v3/files/' + spreadsheet_id + '?fields=parents'
headers = {'Authorization': 'Bearer ' + drive.auth.credentials.token_response["access_token"]}
res = requests.get(url, headers=headers)

# Upload the PDF file to the same folder of Spreadsheet.
up_file = drive.CreateFile({'parents': [{'id': res.json()['parents'][0]}]})
up_file.SetContentFile(pdf_filename)
up_file.Upload()
print('title: %s, mimeType: %s' % (up_file['title'], up_file['mimeType']))
  • 为了找回Spreadsheet的父文件夹ID,我在Drive API中使用了files.get的方法using requests

模式二:

在此模式中,您的目标是使用由 Google Apps 脚本创建的 Web 应用程序实现的。在这种情况下,使用 Web 应用程序将电子表格转换为 PDF 格式,并在电子表格的同一文件夹中创建一个文件。所以进程只能在Google端运行。 python 脚本仅通过请求发送电子表格 ID。为此,请执行以下流程。

1。创建 Google Apps 脚本的新项目。

Web Apps 的示例脚本是 Google Apps 脚本。所以请创建一个 Google Apps 脚本的项目。

如果要直接创建,请访问https://script.new/。在这种情况下,如果您未登录 Google,则会打开登录屏幕。所以请登录Google。这样,Google Apps Script 的脚本编辑器就打开了。

2。准备脚本。

请将以下脚本(Google Apps 脚本)复制并粘贴到脚本编辑器中。此脚本用于 Web 应用程序。

function doGet(e) {
  var spreadsheetId = e.parameter.id;
  var file = DriveApp.getFileById(spreadsheetId);
  var blob = file.getBlob();
  var folder = file.getParents().next();
  var pdfFile = folder.createFile(blob.setName(file.getName() + ".pdf"));
  return ContentService.createTextOutput(pdfFile.getId());
}
  • 本例中使用的是GET方法。

3。部署 Web 应用程序。

  1. 在脚本编辑器上,通过“发布”->“部署为网络应用程序”打开一个对话框。
  2. Select "Me" for "执行应用程序:"
    • 至此,脚本运行作为所有者。
  3. Select “任何人,即使是匿名的” for “有权访问该应用程序的人:”
    • 在这种情况下,请求不需要访问令牌。我认为作为测试用例,我推荐这个设置。
    • 当然你也可以使用access token。届时请设置为"任何人".
  4. 单击“部署”按钮作为新的“项目版本”。
  5. 自动打开“需要授权”的对话框。
    1. 单击“查看权限”。
    2. Select自己的账号。
    3. 点击“此应用未验证”处的“高级”。
    4. 点击“转到###项目名称###(不安全)”
    5. 单击“允许”按钮。
  6. 点击“确定”。
  7. 复制 Web 应用程序的 URL。就像 https://script.google.com/macros/s/###/exec
    • 当您修改 Google Apps 脚本时,请重新部署为新版本。这样,修改后的脚本就会反映到 Web 应用程序中。请注意这一点。

3。 运行 使用 Web 应用程序的功能。

这是用于请求 Web 应用程序的示例 python 脚本。请设置您的网络应用程序 URL 和电子表格 ID。

import requests
spreadsheet_id = '###'  # Please set the Spreadsheet ID.
url = 'https://script.google.com/macros/s/###/exec?id=' + spreadsheet_id
res = requests.get(url)
print(res.text)

参考文献: