MailMerge class

MailMerge class

Represents the mail merge functionality. To learn more, visit the Mail Merge and Reporting documentation article.

Remarks

For mail merge operation to work, the document should contain Word MERGEFIELD and optionally NEXT fields. During mail merge operation, merge fields in the document are replaced with values from your data source.

There are two distinct ways to use mail merge: with mail merge regions and without.

The simplest mail merge is without regions and it is very similar to how mail merge works in Word. Use Execute methods to merge information from some data source such as DataTable, DataSet, DataView, IDataReader or an array of objects into your document. The MailMerge object processes all records of the data source and copies and appends content of the whole document for each record.

Note that when MailMerge object encounters a NEXT field, it selects next record in the data source and continues merging without copying any content.

Use MailMerge.execute_with_regions() and other overloads to merge information into a document with mail merge regions defined. You can use DataSet, DataTable, DataView or IDataReader as data sources for this operation.

You need to use mail merge regions if you want to dynamically grow portions inside the document. Without mail merge regions whole document will be repeated for every record of the data source.

Properties

NameDescription
cleanup_optionsGets or sets a set of flags that specify what items should be removed during mail merge.
cleanup_paragraphs_with_punctuation_marksGets or sets a value indicating whether paragraphs with punctuation marks are considered as empty and should be removed if the MailMergeCleanupOptions.REMOVE_EMPTY_PARAGRAPHS option is specified.
field_merging_callbackOccurs during mail merge when a mail merge field is encountered in the document.
mail_merge_callbackAllows to handle particular events during mail merge.
mapped_data_fieldsReturns a collection that represents mapped data fields for the mail merge operation.
merge_duplicate_regionsGets or sets a value indicating whether all of the document mail merge regions with the name of a data source should be merged while executing of a mail merge with regions against the data source or just the first one.
merge_whole_documentGets or sets a value indicating whether fields in whole document are updated while executing of a mail merge with regions.
preserve_unused_tagsGets or sets a value indicating whether the unused “mustache” tags should be preserved.
region_end_tagGets or sets a mail merge region end tag.
region_start_tagGets or sets a mail merge region start tag.
restart_lists_at_each_sectionGets or sets a value indicating whether lists are restarted at each section after executing of a mail merge.
retain_first_section_startGets or sets a value indicating whether the PageSetup.section_start of the first document section and its copies for subsequent data source rows are retained during mail merge or updated according to MS Word behaviour.
trim_whitespacesGets or sets a value indicating whether trailing and leading whitespaces are trimmed from mail merge values.
unconditional_merge_fields_and_regionsGets or sets a value indicating whether merge fields and merge regions are merged regardless of the parent IF field’s condition.
use_non_merge_fieldsWhen True, specifies that in addition to MERGEFIELD fields, mail merge is performed into some other types of fields and also into “{{fieldName}}” tags.
use_whole_paragraph_as_regionGets or sets a value indicating whether whole paragraph with TableStart or TableEnd field or particular range between TableStart and TableEnd fields should be included into mail merge region.

Methods

NameDescription
delete_fields()Removes mail merge related fields from the document.
execute(data_source)Performs a mail merge from a custom data source.
execute(field_names, values)Performs a mail merge operation for a single record.
execute_with_regions(data_source)Performs a mail merge from a custom data source with mail merge regions.
execute_with_regions(data_source_root)Performs a mail merge from a custom data source with mail merge regions.
get_field_names()Returns a collection of mail merge field names available in the document.
get_field_names_for_region(region_name)Returns a collection of mail merge field names available in the region.
get_field_names_for_region(region_name, region_index)Returns a collection of mail merge field names available in the region.
get_regions_by_name(region_name)Returns a collection of mail merge regions with the specified name.
get_regions_hierarchy()Returns a full hierarchy of regions (with fields) available in the document.

Examples

Shows how to execute a mail merge with data from a DataTable.

def test_execute_data_table(self):

    table = DataTable("Test")
    table.columns.add("CustomerName")
    table.columns.add("Address")
    table.rows.add(["Thomas Hardy", "120 Hanover Sq., London"])
    table.rows.add(["Paolo Accorti", "Via Monte Bianco 34, Torino"])

    # Below are two ways of using a DataTable as the data source for a mail merge.
    # 1 -  Use the entire table for the mail merge to create one output mail merge document for every row in the table:
    doc = ExMailMerge.create_source_doc_execute_data_table()

    doc.mail_merge.execute(table)

    doc.save(ARTIFACTS_DIR + "MailMerge.execute_data_table.whole_table.docx")

    # 2 -  Use one row of the table to create one output mail merge document:
    doc = ExMailMerge.create_source_doc_execute_data_table()

    doc.mail_merge.execute(table.rows[1])

    doc.save(ARTIFACTS_DIR + "MailMerge.execute_data_table.one_row.docx")

@staticmethod
def create_source_doc_execute_data_table() -> aw.Document:
    """Creates a mail merge source document."""

    doc = aw.Document()
    builder = aw.DocumentBuilder(doc)

    builder.insert_field(" MERGEFIELD CustomerName ")
    builder.insert_paragraph()
    builder.insert_field(" MERGEFIELD Address ")

    return doc

See Also