-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
""" | ||
Mail Merge | ||
This script will use mail merge to create personalized Word documents with | ||
data from Central. In this example, only data from approved submissions | ||
are included. | ||
For a tutorial on how to populate Word templates with Python, see: | ||
https://pbpython.com/python-word-template.html | ||
For more examples, see: | ||
https://github.com/iulica/docx-mailmerge/tree/master/tests | ||
Install requirements for this script in `requirements.txt`. The specified | ||
versions are those that were current when the script was last updated, | ||
though it should work with more recent versions. | ||
Install with `pip install -r requirements.txt`. | ||
To run the script, use `python mail_merge.py`. | ||
""" | ||
|
||
import os | ||
from datetime import datetime | ||
|
||
from mailmerge import MailMerge | ||
from pyodk.client import Client | ||
|
||
# customize these settings to your environment | ||
PROJECT_ID = 1 | ||
FORM_ID = "my_form" | ||
INPUT_DOCUMENT = "input.docx" | ||
OUTPUT_FOLDER = "output" | ||
|
||
with Client(project_id=PROJECT_ID) as client: | ||
submissions = client.submissions.get_table(form_id=FORM_ID) | ||
for submission in submissions["value"]: | ||
# only include approved submisisons | ||
if submission["__system"]["reviewState"] == "approved": | ||
with MailMerge(INPUT_DOCUMENT) as document: | ||
coordinates = submission["age_location"]["location"]["coordinates"] | ||
location = f"{coordinates[1]}, {coordinates[0]}" | ||
generation_date = datetime.now().strftime("%m-%d-%Y %H:%M:%S.%f") | ||
document.merge( | ||
first_name=submission["first_name"], | ||
age=submission["age_location"]["age"], | ||
location=location, | ||
instance_id=submission["meta"]["instanceID"], | ||
submission_date=submission["__system"]["submissionDate"], | ||
generation_date=generation_date, | ||
) | ||
# choose variables with unique values to prevent overwritten files | ||
output_path = os.path.join( | ||
OUTPUT_FOLDER, f"{submission['first_name']}.docx" | ||
) | ||
document.write(output_path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pyodk==0.3.0 | ||
docx-mailmerge2=0.8.0 |