Hi Amy Kunce,
Thank you for replying, and the information that you have given to me I really appreciate it.
To give you more specific recommendations, it would be helpful to know:
- What version of Microsoft Office are you using? (e.g., Office 365, Office 2019, etc.)
- Does your organization use Microsoft 365? (This impacts Power Automate availability).
- Are you comfortable with light scripting or learning a new tool (like Power Automate)?
- Are there any budget constraints for third-party software or tools?
I understand you want to streamline a complex process involving Word mail merge, PDF creation, and email distribution. I'll propose a Python script using libraries like python-docx, pandas, comtypes (for Windows COM automation), and PyPDF2 to automate most of your workflow. This solution assumes you're on Windows (due to Outlook and Word usage) and have Microsoft Office installed. The script will handle data extraction from the Excel Master File, perform the mail merge, split the PDF, rename files, and automate email creation with attachments.
Here’s a Python script to automate the process:
- Prerequisites:
- Install Python 3.x.
- Install required libraries: pip install pandas python-docx PyPDF2 comtypes pywin32
- Ensure Microsoft Word and Outlook are installed.
- Update the file paths (EXCEL_FILE, WORD_TEMPLATE, OUTPUT_DIR) in the script to match your system.
- Excel File Setup:
- Your Master File Excel sheet should have columns: Name, Email, CC_Email (optional), and Status (with values like "Successful" for approved applicants).
- Ensure the data is clean (no missing required fields for successful applicants).
- Word Template:
- Create a Word mail merge template with fields matching your Excel columns (e.g., <
- Save it as a .docx file.
- Running the Script:
- Save the script as automate_mail_merge.py.
- Run it: python automate_mail_merge.py.
- The script will:
- Read the Excel file and filter successful applicants.
- Perform the mail merge and save as a single Word document.
- Convert the merged document to a PDF.
- Split the PDF into individual files named after applicants.
- Draft emails in Outlook’s Outbox with the correct PDF attached.
- Outlook remains offline during email drafting, mimicking your process.
Code Script:
import pandas as pd
import comtypes.client
import os
from PyPDF2 import PdfReader, PdfWriter
import win32com.client
import pythoncom
import uuid
from datetime import datetime
File paths (update these to your actual file paths)
EXCEL_FILE = "path/to/your/MasterFile.xlsx"
WORD_TEMPLATE = "path/to/your/MailMergeTemplate.docx"
OUTPUT_DIR = "path/to/output/folder"
MERGED_PDF = os.path.join(OUTPUT_DIR, "MergedLetters.pdf")
Ensure output directory exists
if not os.path.exists(OUTPUT_DIR):
os.makedirs(OUTPUT_DIR)
Step 1: Read Excel Master File
def read_master_file(excel_path):
df = pd.read_excel(excel_path)
# Ensure required columns exist (adjust column names to match your Excel)
required_columns = ['Name', 'Email', 'CC_Email', 'Status']
if not all(col in df.columns for col in required_columns):
raise ValueError("Excel file missing required columns: Name, Email, CC_Email, Status")
# Filter for successful applicants
return df[df['Status'] == 'Successful'][['Name', 'Email', 'CC_Email']]
def perform_mail_merge(template_path, data_source_path, output_doc):
pythoncom.CoInitialize()
try:
word = comtypes.client.CreateObject("Word.Application")
word.Visible = False # Run Word in background
doc = word.Documents.Open(template_path)
# Set up mail merge
doc.MailMerge.MainDocumentType = 0 # wdFormLetters
doc.MailMerge.OpenDataSource(Name=data_source_path)
doc.MailMerge.Execute()
# Save merged document
merged_doc = word.ActiveDocument
merged_doc.SaveAs(output_doc)
merged_doc.Close()
doc.Close()
word.Quit()
finally:
pythoncom.CoUninitialize()
Step 3: Convert Word to PDF
def convert_to_pdf(word_path, pdf_path):
pythoncom.CoInitialize()
try:
word = comtypes.client.CreateObject("Word.Application")
doc = word.Documents.Open(word_path)
doc.SaveAs(pdf_path, FileFormat=17) # 17 = wdFormatPDF
doc.Close()
word.Quit()
finally:
pythoncom.CoUninitialize()
Step 4: Split PDF into individual files
def split_pdf(pdf_path, output_dir, name_list):
pdf = PdfReader(pdf_path)
for i, page in enumerate(pdf.pages):
writer = PdfWriter()
writer.add_page(page)
# Use name from Excel for filename, sanitized
name = name_list[i].replace(" ", "_").replace("/", "_").replace("\", "_")
output_file = os.path.join(output_dir, f"{name}.pdf")
with open(output_file, 'wb') as f:
writer.write(f)
print(f"Created PDF: {output_file}")
Step 5: Send emails with attachments
def send_emails(df, pdf_dir):
pythoncom.CoInitialize()
try:
outlook = win32com.client.Dispatch("Outlook.Application")
namespace = outlook.GetNamespace("MAPI")
for index, row in df.iterrows():
name = row['Name']
email = row['Email']
cc_email = row['CC_Email'] if pd.notna(row['CC_Email']) else ""
pdf_name = name.replace(" ", "_").replace("/", "_").replace("\", "_")
pdf_path = os.path.join(pdf_dir, f"{pdf_name}.pdf")
if not os.path.exists(pdf_path):
print(f"PDF not found for {name}, skipping email.")
continue
# Create email
mail = outlook.CreateItem(0) # 0 = MailItem
mail.To = email
if cc_email:
mail.CC = cc_email
mail.Subject = f"Application Outcome for {name}"
mail.Body = f"Dear {name},\n\nPlease find attached your outcome letter.\n\nBest regards,\nYour Name"
mail.Attachments.Add(pdf_path)
mail.Save() # Save to Outbox (Outlook offline mode)
print(f"Email drafted for {name} to {email}")
finally:
pythoncom.CoUninitialize()
def main():
# Step 1: Read Excel data
df = read_master_file(EXCEL_FILE)
# Save temporary Excel for mail merge
temp_excel = os.path.join(OUTPUT_DIR, "TempDataSource.xlsx")
df.to_excel(temp_excel, index=False)
# Step 2: Perform mail merge
merged_doc = os.path.join(OUTPUT_DIR, "MergedDocument.docx")
perform_mail_merge(WORD_TEMPLATE, temp_excel, merged_doc)
# Step 3: Convert to PDF
convert_to_pdf(merged_doc, MERGED_PDF)
# Step 4: Split PDF
split_pdf(MERGED_PDF, OUTPUT_DIR, df['Name'].tolist())
# Step 5: Create emails
send_emails(df, OUTPUT_DIR)
# Clean up temporary files
for file in [temp_excel, merged_doc, MERGED_PDF]:
if os.path.exists(file):
os.remove(file)
print(f"Deleted temporary file: {file}")
print("Process completed. Check Outlook Outbox for drafted emails.")
if __name__ == "__main__":
main()
Final Steps:
- Open Outlook, review the drafted emails in the Outbox, and take Outlook online to send them.
- Handle any returned emails manually as you currently do.
Notes
- Error Handling: The script assumes your Excel file has the required columns and valid data. Add error handling as needed for your specific case (e.g., invalid email formats).
- PDF Extraction: If your input PDF forms are flattened, you’ll still need to manually extract data into the Excel file, as OCR or manual entry is required for images.
- Outlook Offline: The script saves emails to the Outbox, keeping Outlook offline. You can manually review before sending.
- Scalability: For large numbers of applicants, test the script with a small batch first to ensure performance.
- Security: Ensure your system allows COM automation (some IT policies restrict this).
This script significantly reduces manual steps by automating the mail merge, PDF splitting, renaming, and email drafting. If you have specific requirements (e.g., custom email body, additional fields), let me know, and I can adjust the script!
We truly appreciate your patience as we look into this matter. Thank you for choosing Microsoft and we value your support.
Best regards.
Sting-Ng - Microsoft Community Support Specialist.