I have a current script that fills out a template word doc using MailMerge. The issue I noticed is that after iterating through 15 rows/documents, the process works correctly, but in my Task Manager, there is 15 open idle Microsoft Word applications open.. This is not ideal as this will be used daily and should shut down the app after the doc has been created. Any idea on what to do? Will the Word app close after being idle for x amount of time or should I try to programmatically fix this?
for i in range(len(exampleData)):
#Open Template
template = r'exampletemplate.docx'
#Create Editable Doc
document = MailMerge(template)
#Create Word Naming Convention - Whatever the files should be named with
InsuredName = exampleData.loc[i, "InsuredName"]
PolicyNumber = exampleData.loc[i, "PolicyNumber"]
#Write the values into the doc
document.merge(
InsuredName = InsuredName,
Address1 = exampleData.loc[i, "Address1"],
Address2 = exampleData.loc[i, "Address2"],
ProducerName = exampleData.loc[i, "ProducerName"],
PolicyNumber = PolicyNumber,
ExpirationDate = datetime.strptime((str(exampleData.loc[i, "ExpirationDate"])), '%Y-%m-%d %H:%M:%S').strftime('%m/%d/%Y'),
DueDate = datetime.strptime((str(exampleData.loc[i, "DueDate"])), '%Y-%m-%d %H:%M:%S').strftime('%m/%d/%Y'),
EffectiveDate = datetime.strptime((str(exampleData.loc[i, "EffectiveDate"])), '%Y-%m-%d %H:%M:%S').strftime('%m/%d/%Y'),
ReportPeriodBegin = datetime.strptime((str(exampleData.loc[i, "ReportPeriodBegin"])), '%Y-%m-%d %H:%M:%S').strftime('%m/%d/%Y'),
ReportPeriodEnd = datetime.strptime((str(exampleData.loc[i, "ReportPeriodEnd"])), '%Y-%m-%d %H:%M:%S').strftime('%m/%d/%Y')
)
document.write(r'\\endpath\\' + PolicyNumber + '_' + InsuredName + '.docx')
I was facing the same problem. You're doing the process but you're not closing the template file.
try this:
document.close()
documentation