Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active February 1, 2023 19:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aspose-com-gists/1cc7c8325de32009db5889cbcb8e8c5b to your computer and use it in GitHub Desktop.
Save aspose-com-gists/1cc7c8325de32009db5889cbcb8e8c5b to your computer and use it in GitHub Desktop.
Automate MS Word Mail Merge in Python | Generate Word Documents from Mail Merge Templates
import aspose.words as aw
# Create a document builder
builder = aw.DocumentBuilder()
# Insert a text input field the unique name of this field is "Hello", the other parameters define
# what type of FormField it is, the format of the text, the field result and the maximum text length (0 = no limit)
builder.insert_text_input("TextInput", aw.fields.TextFormFieldType.REGULAR, "", "Hello ", 0)
builder.insert_field("MERGEFIELD CustomerFirstName \\* MERGEFORMAT")
builder.insert_text_input("TextInput1", aw.fields.TextFormFieldType.REGULAR, "", " ", 0)
builder.insert_field("MERGEFIELD CustomerLastName \\* MERGEFORMAT")
builder.insert_text_input("TextInput1", aw.fields.TextFormFieldType.REGULAR, "", " , ", 0)
# Insert a paragraph break into the document
builder.insert_paragraph()
# Insert mail body
builder.insert_text_input("TextInput", aw.fields.TextFormFieldType.REGULAR, "", "Thanks for purchasing our ", 0)
builder.insert_field("MERGEFIELD ProductName \\* MERGEFORMAT")
builder.insert_text_input("TextInput", aw.fields.TextFormFieldType.REGULAR, "", ", please download your Invoice at ", 0)
builder.insert_field("MERGEFIELD InvoiceURL \\* MERGEFORMAT")
builder.insert_text_input("TextInput", aw.fields.TextFormFieldType.REGULAR, "", ". If you have any questions please call ", 0)
builder.insert_field("MERGEFIELD Supportphone \\* MERGEFORMAT")
builder.insert_text_input("TextInput", aw.fields.TextFormFieldType.REGULAR, "", ", or email us at ", 0)
builder.insert_field("MERGEFIELD SupportEmail \\* MERGEFORMAT")
builder.insert_text_input("TextInput", aw.fields.TextFormFieldType.REGULAR, "", ".", 0)
builder.insert_paragraph()
# Insert mail ending
builder.insert_text_input("TextInput", aw.fields.TextFormFieldType.REGULAR, "", "Best regards,", 0)
builder.insert_break(aw.BreakType.LINE_BREAK)
builder.insert_field("MERGEFIELD EmployeeFullname \\* MERGEFORMAT")
builder.insert_text_input("TextInput1", aw.fields.TextFormFieldType.REGULAR, ",", " ", 0)
builder.insert_field("MERGEFIELD EmployeeDepartment \\* MERGEFORMAT")
# Save the template as a DOCX file
builder.document.save("mail_merge_template.docx")
import aspose.words as aw
# Load the mail merge template
doc = aw.Document("mail_merge_template.docx")
# Fill the fields in the document with data
doc.mail_merge.execute(["CustomerFirstName", "CustomerLastName", "ProductName", "InvoiceURL", "SupportPhone", "SupportEmail", "EmployeeFullname", "EmployeeDepartment"],
["John", "Doe", "Aspose.Words", "aspose.com", "111-222-333", "support@aspose.com", "Jimmy", "Sales"]
)
# Save the document
doc.save("mail_merge_populated.docx")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment