이 가이드에서는 Google Docs API를 사용하여 메일 병합을 실행하는 방법을 설명합니다.
소개
메일 병합은 스프레드시트 또는 다른 데이터 소스의 행에서 값을 가져와 템플릿 문서에 삽입합니다. 이렇게 하면 하나의 기본 문서 (템플릿)를 만들어 이를 토대로 여러 유사한 문서를 생성할 수 있으며, 각 문서는 병합할 데이터로 맞춤설정됩니다. 결과는 반드시 우편이나 양식 서신에 사용되는 것은 아니지만 고객 인보이스를 일괄 생성하는 등 다양한 목적으로 사용할 수 있습니다.
메일 병합은 스프레드시트와 워드 프로세서가 등장한 이후로 존재해 왔으며 오늘날 많은 비즈니스 워크플로의 일부로 사용되고 있습니다. 일반적으로 데이터를 행당 하나의 레코드로 구성하고 열은 데이터의 필드를 나타냅니다(다음 표 참고).
이름
주소
영역
1
UrbanPq
123 1st St.
서부
2
Pawxana
456 2nd St.
남부
이 페이지의 샘플 앱은 Google Docs, Sheets, Drive API를 사용하여 메일 병합이 실행되는 방식의 세부정보를 추상화하여 사용자를 구현 문제로부터 보호하는 방법을 보여줍니다. 이 Python 샘플에 관한 자세한 내용은 샘플의 GitHub 저장소를 참고하세요.
샘플 애플리케이션
이 샘플 앱은 기본 템플릿을 복사한 다음 지정된 데이터 소스의 변수를 각 사본에 병합합니다. 이 샘플 앱을 사용해 보려면 먼저 템플릿을 설정하세요.
다음은 일반 텍스트나 Sheets와 같은 소스의 실제 데이터와 병합할 수 있는 자리표시자가 있는 샘플 서신 템플릿입니다. 템플릿은 다음과 같습니다.
그런 다음 SOURCE 변수를 사용하여 일반 텍스트 또는 시트를 데이터 소스로 선택합니다. 샘플은 기본적으로 일반 텍스트로 설정됩니다. 즉, 샘플 데이터는 TEXT_SOURCE_DATA 변수를 사용합니다. Sheets에서 데이터를 가져오려면 SOURCE 변수를 'sheets'로 업데이트하고 SHEETS_FILE_ID 변수를 설정하여 샘플 시트(또는 자체 시트)를 가리킵니다.
다음은 형식을 확인할 수 있는 시트의 모습입니다.
샘플 데이터로 앱을 사용해 본 후 데이터와 사용 사례에 맞게 조정합니다. 명령줄 애플리케이션은 다음과 같이 작동합니다.
설정
데이터 소스에서 데이터 가져오기
데이터의 각 행을 반복합니다.
템플릿 사본 만들기
사본을 데이터와 병합
새로 병합된 문서 링크 출력
새로 병합된 모든 서신은 사용자의 내 드라이브에도 표시됩니다. 병합된 서신의 예는 다음과 같습니다.
importtimeimportgoogle.authfromgoogleapiclient.discoveryimportbuildfromgoogleapiclient.errorsimportHttpError# Fill-in IDs of your Docs template & any Sheets data sourceDOCS_FILE_ID="195j9eDD3ccgjQRttHhJPymLJUCOUjs-jmwTrekvdjFE"SHEETS_FILE_ID="11pPEzi1vCMNbdpqaQx4N43rKmxvZlgEHE9GqpYoEsWw"# authorization constantsSCOPES=(# 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 constantsSOURCES=("text","sheets")SOURCE="text"# Choose one of the data SOURCESCOLUMNS=["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 variablesmerge={# 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 APIsDRIVE=build("drive","v2",credentials=creds)DOCS=build("docs","v1",credentials=creds)SHEETS=build("sheets","v4",credentials=creds)defget_data(source):"""Gets mail merge data from chosen data source."""try:ifsourcenotin{"sheets","text"}:raiseValueError(f"ERROR: unsupported source {source}; choose from {SOURCES}")returnSAFE_DISPATCH[source]()exceptHttpErroraserror:print(f"An error occurred: {error}")returnerrordef_get_text_data():"""(private) Returns plain text data; can alter to read from CSV file."""returnTEXT_SOURCE_DATAdef_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")forkinSOURCES}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"))exceptHttpErroraserror:print(f"An error occurred: {error}")returnerrordefmerge_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 valuescopy_id=_copy_template(tmpl_id,source,service)context=merge.iteritems()ifhasattr({},"iteritems")elsemerge.items()# "search & replace" API requests for mail merge substitutionsreqs=[{"replaceAllText":{"containsText":{"text":"{{%s}}"%key.upper(),# {{VARS}} are uppercase"matchCase":True,},"replaceText":value,}}forkey,valueincontext]# send requests to Docs API to do actual mergeDOCS.documents().batchUpdate(body={"requests":reqs},documentId=copy_id,fields="").execute()returncopy_idexceptHttpErroraserror:print(f"An error occurred: {error}")returnerrorif__name__=="__main__":# get row data, then loop through & process each form letterdata=get_data(SOURCE)# get data from data sourcefori,rowinenumerate(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 파일과 전체 애플리케이션 소스 코드를 참고하세요.
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["필요한 정보가 없음","missingTheInformationINeed","thumb-down"],["너무 복잡함/단계 수가 너무 많음","tooComplicatedTooManySteps","thumb-down"],["오래됨","outOfDate","thumb-down"],["번역 문제","translationIssue","thumb-down"],["샘플/코드 문제","samplesCodeIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2024-12-21(UTC)"],[[["This guide demonstrates how to utilize the Google Docs API to execute a mail merge, automating the process of generating personalized documents from a template and data source."],["Users can choose between plain text or a Google Sheet as their data source, with the sample app providing examples for both options."],["The application copies a template document, merges variables from the data source, and outputs a link to the newly-merged document, accessible in Google Drive."],["Placeholder variables within the template document are replaced with data from the designated source, allowing for customization of individual documents."],["Refer to the GitHub repository for the complete source code, setup instructions, and further information on using the sample application."]]],[]]