Group Excel Sheets by Name and Save Them as Separate Workbooks
Grouping the worksheets of several Excel files by sheet name, with one new workbook per sheet name. The xlwings script loops over a folder of monthly files and copies every Sales sheet into Sales.xlsx, every Profit sheet into Profit.xlsx, and so on, naming each copied sheet after the file it came from.
The data
The source folder holds one workbook per month, and most months have the same sheets. The goal is one output workbook per sheet name, with one tab per month.
- January: Sales, Profit, Net_Working_Capital
- February: Sales, Profit, Net_Working_Capital, Headcount
- March: Sales, Profit, Net_Working_Capital
- April: Sales, Profit, Net_Working_Capital
- May: Sales, Profit, Net_Working_Capital, Headcount

Install xlwings
The script uses xlwings, which drives Excel itself, so copied sheets keep their formatting. That also means it needs Excel installed, on Windows or macOS.
pip install xlwings
Set the folders
The script needs two imports and two folder names: where the monthly files are and where the grouped workbooks go. A folder name alone works when the script sits next to the folders; otherwise use the full path.
from pathlib import Path
import xlwings as xw
SOURCE_DIR = "Month_End_Data"
OUTPUT_DIR = "Output"
Then it collects the Excel files in the source folder, and builds a dictionary of the workbooks that already exist in the output folder, keyed by file name. That dictionary decides whether a sheet goes into a new workbook or gets appended to an existing one.
excel_files = list(Path(SOURCE_DIR).glob("*.xlsx"))
output_paths = list(Path(OUTPUT_DIR).glob("*.xlsx"))
output_names = [file.stem for file in output_paths]
excel_outputs = dict(zip(output_names, output_paths))
Copy each sheet
For every sheet of every file, the script creates a new workbook, copies the sheet into it, renames the copy after the source file and deletes the blank sheet the new workbook started with. The workbook is saved under the sheet’s name, so the Sales sheet of January.xlsx becomes Output/Sales.xlsx.
for excel_file in excel_files:
wb = xw.Book(excel_file)
for sheet in wb.sheets:
# Create a new wb and copy the sheet to this new wb
# Afterwards, add the new file & path to the dict 'excel_outputs'
wb_tmp = xw.Book()
sheet.copy(after=wb_tmp.sheets[0])
sht = xw.sheets.active
sht.name = wb.name
wb_tmp.sheets[0].delete()
output_path = Path(OUTPUT_DIR, sheet.name + ".xlsx")
wb_tmp.save(output_path)
excel_outputs[sheet.name] = wb_tmp.fullname
wb_tmp.close()
# Only quit the excel instance if no other wb is open
if len(wb.app.books) == 1:
wb.app.quit()
else:
wb.close()
The new path goes into excel_outputs, so the Sales sheet of the next month finds that file. At the end of each file, the script closes it and quits Excel only if no other workbook is open.
Append to existing workbooks
When a workbook for the sheet name already exists, the script opens it and appends the sheet instead of overwriting the file. It skips the sheet if that workbook already has a tab from the same source file, so you can rerun the script when a new month arrives. This is the complete script from the GitHub repository, with both branches:
from pathlib import Path # Standard Python Module
import xlwings as xw # pip install xlwings
"""
Iterate over all excel files in a given source directory.
For each worksheet within the excel files -> create a new workbook.
The name of the new workbook will be equal to the worksheet's name.
If the workbook already exists, append the worksheet to the existing workbook.
"""
SOURCE_DIR = "Month_End_Data"
OUTPUT_DIR = "Output"
excel_files = list(Path(SOURCE_DIR).glob("*.xlsx"))
# Create a dict with the excel file name & path:
# excel_outputs = {'filename1':filepath1,
# 'filename2':filepath2}
output_paths = list(Path(OUTPUT_DIR).glob("*.xlsx"))
output_names = [file.stem for file in output_paths]
excel_outputs = dict(zip(output_names, output_paths))
for excel_file in excel_files:
wb = xw.Book(excel_file)
for sheet in wb.sheets:
# Check, if the sheet name already exists in the output folder (as a separate wb)
if sheet.name in excel_outputs:
# Check, if that wb also contains our sheet (the wb's name)
# If not, we will copy/append the sheet to the existing wb
wb_tmp = xw.Book(excel_outputs[sheet.name])
wb_tmp_sheets = [sheet.name for sheet in wb_tmp.sheets]
if wb.name not in wb_tmp_sheets:
sheet.copy(after=wb_tmp.sheets[0])
sht = xw.sheets.active
sht.name = wb.name
wb_tmp.save()
wb_tmp.close()
else:
# Create a new wb and copy the sheet to this new wb
# Afterwards, add the new file & path to the dict 'excel_outputs'
wb_tmp = xw.Book()
sheet.copy(after=wb_tmp.sheets[0])
sht = xw.sheets.active
sht.name = wb.name
wb_tmp.sheets[0].delete()
output_path = Path(OUTPUT_DIR, sheet.name + ".xlsx")
wb_tmp.save(output_path)
excel_outputs[sheet.name] = wb_tmp.fullname
wb_tmp.close()
# Only quit the excel instance if no other wb is open
if len(wb.app.books) == 1:
wb.app.quit()
else:
wb.close()
Run the script
Run the script, and the output folder holds one workbook per sheet name. Headcount.xlsx has only February and May, because only those months have a Headcount sheet.

If you have one workbook and want every sheet in its own file, the shorter script in split each Excel sheet into separate files is enough. For the opposite direction, see how to combine all Excel files in a folder into one workbook.
