使用 Google 文档 API 合并邮件

本指南介绍了如何使用 Google 文档 API 执行邮件合并。

简介

邮件合并功能会从电子表格或其他数据源的行中提取值,并将其插入模板文档中。这样,您就可以创建一个主要文档(模板),并根据该文档生成许多类似文档,每个文档都根据要合并的数据进行自定义。结果不一定用于邮件或信件,但可用于任何用途,例如生成一批客户账单。

电子邮件合并功能自从电子表格和文字处理器问世以来就一直存在,并且是当今许多业务工作流的一部分。惯例是按每行一个记录的格式整理数据,其中列代表数据中的字段,如下表所示:

名称 地址 可用区
1 UrbanPq 123 1st St. 西
2 Pawxana 456 2nd St.

本页面的示例应用展示了如何使用 Google 文档、表格和云端硬盘 API 来抽象出执行邮件合并的详细信息,从而保护用户免受实现问题的困扰。如需详细了解此 Python 示例,请访问该示例的 GitHub 代码库

示例应用

此示例应用会复制您的主要模板,然后将指定数据源中的变量合并到每个副本中。如需试用此示例应用,请先设置模板:

  1. 创建 Google 文档文件。选择要使用的模板。
  2. 记下新文件的文档 ID。如需了解详情,请参阅文档 ID
  3. DOCS_FILE_ID 变量设置为文档 ID。
  4. 将联系信息替换为模板占位符变量,应用会将这些变量与所选数据合并。

以下是电子邮件示例模板,其中包含可与来自纯文本或 Google 表格等来源的真实数据合并的占位符。该模板如下所示:

接下来,使用 SOURCE 变量选择“纯文本”或“表格”作为数据源。示例默认为纯文本,这意味着示例数据使用 TEXT_SOURCE_DATA 变量。如需从 Google 表格中获取数据,请将 SOURCE 变量更新为 'sheets',并通过设置 SHEETS_FILE_ID 变量将其指向我们的示例表格(或您自己的表格)。

该工作表如下所示,以便您查看格式:

使用我们的示例数据试用该应用,然后将其调整为适合您的数据和用例。命令行应用的运作方式如下:

  • 设置
  • 从数据源中提取数据
  • 循环遍历每行数据
    • 创建模板的副本
    • 将副本与数据合并
    • 指向新合并的文档的输出链接

所有新合并的信件也会显示在用户的“我的云端硬盘”中。合并后的信件示例如下所示:

源代码

Python

docs/mail-merge/docs_mail_merge.py
import time

import google.auth
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError

# Fill-in IDs of your Docs template & any Sheets data source
DOCS_FILE_ID = "195j9eDD3ccgjQRttHhJPymLJUCOUjs-jmwTrekvdjFE"
SHEETS_FILE_ID = "11pPEzi1vCMNbdpqaQx4N43rKmxvZlgEHE9GqpYoEsWw"

# authorization constants

SCOPES = (  # iterable or space-delimited string
    "https://www.googleapis.com/auth/drive",
    "https://www.googleapis.com/auth/documents",
    "https://www.googleapis.com/auth/spreadsheets.readonly",
)

# application constants
SOURCES = ("text", "sheets")
SOURCE = "text"  # Choose one of the data SOURCES
COLUMNS = ["to_name", "to_title", "to_company", "to_address"]
TEXT_SOURCE_DATA = (
    (
        "Ms. Lara Brown",
        "Googler",
        "Google NYC",
        "111 8th Ave\nNew York, NY  10011-5201",
    ),
    (
        "Mr. Jeff Erson",
        "Googler",
        "Google NYC",
        "76 9th Ave\nNew York, NY  10011-4962",
    ),
)

# fill-in your data to merge into document template variables
merge = {
    # sender data
    "my_name": "Ayme A. Coder",
    "my_address": "1600 Amphitheatre Pkwy\nMountain View, CA  94043-1351",
    "my_email": "http://google.com",
    "my_phone": "+1-650-253-0000",
    # - - - - - - - - - - - - - - - - - - - - - - - - - -
    # recipient data (supplied by 'text' or 'sheets' data source)
    "to_name": None,
    "to_title": None,
    "to_company": None,
    "to_address": None,
    # - - - - - - - - - - - - - - - - - - - - - - - - - -
    "date": time.strftime("%Y %B %d"),
    # - - - - - - - - - - - - - - - - - - - - - - - - - -
    "body": (
        "Google, headquartered in Mountain View, unveiled the new "
        "Android phone at the Consumer Electronics Show. CEO Sundar "
        "Pichai said in his keynote that users love their new phones."
    ),
}

creds, _ = google.auth.default()
# pylint: disable=maybe-no-member

# service endpoints to Google APIs

DRIVE = build("drive", "v2", credentials=creds)
DOCS = build("docs", "v1", credentials=creds)
SHEETS = build("sheets", "v4", credentials=creds)


def get_data(source):
  """Gets mail merge data from chosen data source."""
  try:
    if source not in {"sheets", "text"}:
      raise ValueError(
          f"ERROR: unsupported source {source}; choose from {SOURCES}"
      )
    return SAFE_DISPATCH[source]()
  except HttpError as error:
    print(f"An error occurred: {error}")
    return error


def _get_text_data():
  """(private) Returns plain text data; can alter to read from CSV file."""
  return TEXT_SOURCE_DATA


def _get_sheets_data(service=SHEETS):
  """(private) Returns data from Google Sheets source. It gets all rows of
  'Sheet1' (the default Sheet in a new spreadsheet), but drops the first
  (header) row. Use any desired data range (in standard A1 notation).
  """
  return (
      service.spreadsheets()
      .values()
      .get(spreadsheetId=SHEETS_FILE_ID, range="Sheet1")
      .execute()
      .get("values")[1:]
  )
  # skip header row


# data source dispatch table [better alternative vs. eval()]
SAFE_DISPATCH = {k: globals().get(f"_get_{k}_data") for k in SOURCES}


def _copy_template(tmpl_id, source, service):
  """(private) Copies letter template document using Drive API then
  returns file ID of (new) copy.
  """
  try:
    body = {"name": f"Merged form letter ({source})"}
    return (
        service.files()
        .copy(body=body, fileId=tmpl_id, fields="id")
        .execute()
        .get("id")
    )
  except HttpError as error:
    print(f"An error occurred: {error}")
    return error


def merge_template(tmpl_id, source, service):
  """Copies template document and merges data into newly-minted copy then
  returns its file ID.
  """
  try:
    # copy template and set context data struct for merging template values
    copy_id = _copy_template(tmpl_id, source, service)
    context = merge.iteritems() if hasattr({}, "iteritems") else merge.items()

    # "search & replace" API requests for mail merge substitutions
    reqs = [
        {
            "replaceAllText": {
                "containsText": {
                    "text": "{{%s}}" % key.upper(),  # {{VARS}} are uppercase
                    "matchCase": True,
                },
                "replaceText": value,
            }
        }
        for key, value in context
    ]

    # send requests to Docs API to do actual merge
    DOCS.documents().batchUpdate(
        body={"requests": reqs}, documentId=copy_id, fields=""
    ).execute()
    return copy_id
  except HttpError as error:
    print(f"An error occurred: {error}")
    return error


if __name__ == "__main__":
  # get row data, then loop through & process each form letter
  data = get_data(SOURCE)  # get data from data source
  for i, row in enumerate(data):
    merge.update(dict(zip(COLUMNS, row)))
    print(
        "Merged letter %d: docs.google.com/document/d/%s/edit"
        % (i + 1, merge_template(DOCS_FILE_ID, SOURCE, DRIVE))
    )

如需了解详情,请参阅示例应用的 GitHub 代码库中的 README 文件和完整应用源代码。