Split Excel Data Into Multiple Files by Column Values With Python
To split an Excel file by column values with Python, load it with pandas, loop over the unique values of the column, and save the matching rows of each value to its own workbook, for example one file per country. Clean the values first, so stray spaces, mixed case and characters like / or ? do not break the file names.

Install the libraries
The script needs pandas, plus openpyxl, which pandas uses to read and write .xlsx files. pathlib comes with Python and handles the file paths. Start a new Python file next to the workbook and import them:
from pathlib import Path
import pandas as pd
Install pandas and openpyxl if they are missing:
pip install pandas
pip install openpyxl
Load the workbook
Read the workbook into a DataFrame, name the column to split by, and collect its unique values. My sample workbook is Financial_Sample.xlsx, saved in the same folder as the script:
excel_file = Path(__file__).parent / "Financial_Sample.xlsx"
df = pd.read_excel(excel_file)
Path(__file__).parent is the folder that holds the script, so the code finds the workbook no matter where you start it from. If your workbook lives elsewhere, put its full path there instead.
column_name = 'Country'
unique_values = df[column_name].unique()
Save one file per value
The loop goes through the unique values, keeps the rows that match each one, and writes them to their own workbook in an output folder. The script creates that folder if it does not exist yet.
str.fullmatch() keeps only rows whose value matches exactly. The sheet name is cut to 31 characters with [:31], because Excel allows no longer sheet names. Here is the complete script:
from pathlib import Path
import pandas as pd # pip install pandas
# Define & create output directory
output_dir = Path(__file__).parent / "output"
output_dir.mkdir(parents=True, exist_ok=True)
# Define Excel file path
excel_file = Path(__file__).parent / "Financial_Sample.xlsx"
df = pd.read_excel(excel_file)
column_name = 'Country'
unique_values = df[column_name].unique()
for unique_value in unique_values:
df_output = df[df[column_name].str.fullmatch(unique_value)]
output_path = output_dir / f"{unique_value}.xlsx"
df_output.to_excel(output_path, sheet_name=unique_value[:31], index=False)
Run it from a command prompt in the script’s folder. The output folder then holds one workbook per country.

fullmatch treats the value as a regular expression. That is fine for country names, but if your values contain characters like brackets, dots or plus signs, compare with df[df[column_name] == unique_value] instead.
Clean inconsistent values
Real data is rarely this tidy. “Germany”, “germany” and “Germany ” end up in three separate files, because pandas sees three different values. One line removes leading and trailing spaces and converts the names to title case before the split:
df['Country'] = df['Country'].str.strip().str.title()
For files that are messy in other ways, like extra header rows, see how to read messy Excel files with pandas.
Remove characters not allowed in file names
Windows does not allow characters such as / : ? * in file names, so a value like “Bosnia/Herzegovina” would stop the script. Remove them from the column before you build the file names. This list holds the characters to strip:
replace_symbols = ['>', '<', ':', '"', '/', r'\\', r'\|', r'\?', r'\*']
The patterns are regular expressions, so the backslash escapes characters that have a special meaning there. The r prefix keeps Python from reading those backslashes itself; without it, Python 3.12 and later print a SyntaxWarning for ‘\|’.
The final script removes those characters, trims the spaces and fixes the case in one step, then splits the file as before:
from pathlib import Path
import pandas as pd # pip install pandas
# Define & create output directory
output_dir = Path(__file__).parent / "output"
output_dir.mkdir(parents=True, exist_ok=True)
# Define Excel file path
excel_file = Path(__file__).parent / "Financial_Sample.xlsx"
df = pd.read_excel(excel_file)
column_name = 'Country'
replace_symbols = ['>', '<', ':', '"', '/', r'\\', r'\|', r'\?', r'\*']
df[column_name] = (
df[column_name].replace(replace_symbols, '', regex=True).str.strip().str.title()
)
unique_values = df[column_name].unique()
for unique_value in unique_values:
df_output = df[df[column_name].str.fullmatch(unique_value)]
output_path = output_dir / f"{unique_value}.xlsx"
df_output.to_excel(output_path, sheet_name=unique_value[:31], index=False)
If you want every worksheet as its own file instead, see how to split each Excel sheet into separate files. To email each file to the right person afterwards, follow create and distribute Excel files with Python and Outlook.
