In this Python tutorial, I will be showing you how to perform a Word Document Mail Merge using Python.
This is one of the most requested topic I got, and I thought I finally have the time to record the tutorial. Couple of things to consider when using Python instead of VBA to perform Word Document Mail Merge.
🔔 Subscribe: https://www.youtube.com/channel/UCvVZ19DRSLIC2-RUOeWx8ug
Source Code:
import os
import win32com.client as win32 # pip install pywin32
working_directory = os.getcwd()
source_name = 'Data Source.xlsx'
destination_folder = os.path.join(working_directory, 'Destination')
"""
Create a Word application instance
"""
wordApp = win32.Dispatch('Word.Application')
wordApp.Visible = True
"""
Open Word Template + Open Data Source
"""
sourceDoc = wordApp.Documents.Open(os.path.join(working_directory, 'Word Template.docx'))
mail_merge = sourceDoc.MailMerge
mail_merge.OpenDataSource(
Name:=os.path.join(working_directory, source_name),
sqlstatement:="SELECT * FROM [Data Source$]"
)
record_count = mail_merge.DataSource.RecordCount
"""
Perform Mail Merge
"""
for i in range(1, record_count + 1):
mail_merge.DataSource.ActiveRecord = i
mail_merge.DataSource.FirstRecord = i
mail_merge.DataSource.LastRecord = i
mail_merge.Destination = 0
mail_merge.Execute(False)
# get record value
base_name = mail_merge.DataSource.DataFields('Name of Recipient'.replace(' ', '_')).Value
targetDoc = wordApp.ActiveDocument
"""
Save Files in Word Doc and PDF
"""
targetDoc.SaveAs2(os.path.join(destination_folder, base_name + '.docx'), 16)
targetDoc.ExportAsFixedFormat(os.path.join(destination_folder, base_name), exportformat:=17)
"""
Close target file
"""
targetDoc.Close(False)
targetDoc = None
sourceDoc.MailMerge.MainDocumentType = -1
#python