Docs API를 사용한 메일 병합

이 가이드에서는 Google Docs API를 사용하여 메일 병합을 수행하는 방법을 설명합니다.

소개

메일 병합은 스프레드시트 또는 다른 데이터 소스의 행에서 값을 가져옵니다. 템플릿 문서에 삽입합니다. 이렇게 하면 여러 개의 동일한 도메인에 대한 문서 (템플릿)를 사용하여 각각 유사한 문서를 여러 개 생성할 수 있습니다. 맞춤설정됩니다. 그 결과는 사용할 수 있지만, 일괄 문자 생성과 같이 어떤 목적으로든 사용할 수 있습니다. 고객 인보이스

메일 병합은 스프레드시트와 오늘날 많은 비즈니스 워크플로의 일부입니다. 규칙은 한 행당 하나의 레코드로 데이터를 구성하며, 열은 각 열의 필드를 나타냅니다. 데이터를 삭제할 수 있습니다.

이름 주소 영역
1 UrbanPq 서울특별시 강남구 테헤란로 152 서부
2 팍사나 456 2nd St. 남부

이 페이지의 샘플 앱은 Google Docs도구를 사용하는 방법을 보여주며, Google Sheets, Drive API를 사용하여 구현 방지를 위한 메일 병합 수행 방법에 대한 세부정보 있습니다 이 Python 샘플에 대한 자세한 내용은 GitHub 저장소를 참조하세요.

샘플 애플리케이션

이 샘플 앱은 기본 템플릿을 복사한 다음 복사됩니다. 이 샘플 앱을 사용해 보려면 먼저 템플릿을 설정합니다.

  1. Docs 파일 만들기 그런 다음 선택합니다.
  2. 새 파일의 문서 ID를 기록해 둡니다. 자세한 내용은 문서를 참조하세요. ID를 입력합니다.
  3. DOCS_FILE_ID 변수를 문서 ID로 설정합니다.
  4. 연락처 정보를 앱이 선택한 데이터와 병합됩니다.

여기에 있는 샘플 편지와 템플릿 일반 스프레드시트입니다. 이 템플릿은 다음과 같이 표시됩니다.

그런 다음 데이터 소스로 일반 텍스트 또는 Sheets를 선택합니다. SOURCE 변수 사용 이 샘플은 기본적으로 일반 텍스트로 설정되어 있습니다. 즉, 샘플 데이터는 TEXT_SOURCE_DATA 변수를 사용합니다. 소스 데이터 소스 Sheets에서 SOURCE 변수를 'sheets'로 업데이트하고 이를 가리킵니다. 샘플 URL은 시트 (또는 자체) 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))
    )

자세한 내용은 README 파일 및 전체 애플리케이션 소스 코드를 참고하세요. 를 참조하세요. GitHub 저장소를 참조하세요.