Create and Distribute Excel Files Using Python and Outlook
One financial workbook split into a separate Excel report per country, each then emailed to the right recipient through Outlook. Python builds the files with pandas and hands them to Outlook over COM, so the mail goes from your own account with no SMTP setup. It needs classic Outlook on Windows.
The example
The workbook Financial_Data.xlsx holds financial data for five countries on a Data sheet and a distribution list on an Email_List sheet. The script creates one report per country with only the 2021 rows and emails each report to the person responsible for that country.

Requirements
You need Windows with the classic desktop Outlook and three Python packages: pandas to filter the data, openpyxl so pandas can read and write Excel files, and pywin32 to control Outlook.
pip install pandas openpyxl pywin32
The new Outlook app for Windows does not offer the COM interface that pywin32 talks to, so the email step only works with classic Outlook.
Load the data
The first block sets the path to the workbook, creates an Attachments folder for the reports, loads the Data sheet into a DataFrame and collects the unique country names.
from pathlib import Path
import pandas as pd # pip install pandas openpyxl
import win32com.client as win32 # pip install pywin32
# Locate examples files & create output directory
EXCEL_FILE_PATH = Path.cwd() / "Financial_Data.xlsx"
ATTACHMENT_DIR = Path.cwd() / "Attachments"
ATTACHMENT_DIR.mkdir(exist_ok=True)
# Load financial data into dataframe
data = pd.read_excel(EXCEL_FILE_PATH, sheet_name="Data")
# Get unique values from any particular column
column_name = "Country"
unique_values = data[column_name].unique()
Create one file per country
For each country, data.query keeps the rows of that country for 2021, and to_excel saves them as Country_2021.xlsx in the Attachments folder. For a single country the filter looks like this:
japan_data = data.query('Country == "Japan" and Year == 2021')
The loop does the same for every country, using @unique_value to refer to the Python variable inside the query string:
# Query/Filter the dataframe and export the filtered dataframe as an Excel file
for unique_value in unique_values:
data_output = data.query(f"{column_name} == @unique_value & Year==2021")
output_path = ATTACHMENT_DIR / f"{unique_value}_2021.xlsx"
data_output.to_excel(output_path, sheet_name=unique_value, index=False)

I go deeper into this step, including messy values and characters that are not allowed in file names, in split Excel data into multiple files by column values.
Send the emails through Outlook
The script loads the Email_List sheet, starts Outlook with win32.Dispatch, and creates one email per row with the recipient, CC, a subject with the country name, an HTML body and the matching report attached.
# Load email distribution list into dataframe
email_list = pd.read_excel(EXCEL_FILE_PATH, sheet_name="Email_List")
# Iterate over email distribution list & send emails via Outlook App
outlook = win32.Dispatch("outlook.application")
for index, row in email_list.iterrows():
mail = outlook.CreateItem(0)
mail.To = row["Email"]
mail.CC = row["CC"]
mail.Subject = f"Financial Report for: {row['Country']}"
# mail.Body = "Message body"
mail.HTMLBody = f"""
<b>Hi {row['Name']}</b>,<br><br>
Please find attached the report for {row['Country']}.<br><br>
Best Regards,<br>
Sven
"""
attachment_path = str(ATTACHMENT_DIR / f"{row['Country']}_2021.xlsx")
mail.Attachments.Add(Source=attachment_path)
mail.Display()
# Uncomment to send email
# mail.Send()
mail.Display() opens each email for review instead of sending it. Once the drafts look right, uncomment mail.Send() and remove mail.Display().
Limits and troubleshooting
The example data is clean. Real data often needs cleaning first, and pandas exports plain values, so the reports lose the formatting of the original workbook.
- Empty CC cells: pandas reads an empty cell as NaN, which would end up in the CC field. Add
email_list = email_list.fillna("")after loading the list. - Long country names: each report uses the country as its sheet name. Excel allows at most 31 characters, so shorten longer names, for example with
sheet_name=unique_value[:31]. - Error at win32.Dispatch: classic Outlook must be installed and set up with an account. The new Outlook and Outlook on the web do not work.
- Formatted reports: to keep colors, column widths and charts, write into a template with xlwings or openpyxl instead, as in automating Excel reporting with Python.
If each recipient should get a personal email with their own attachment without writing code, my SheetCourier app sends a mail merge from an Excel list through your own Gmail or Outlook account. My guide on mail merge from Excel to Outlook or Gmail shows how that works.
