FormulaSurge

Free tool · runs in your browser

Python Excel Script Generator

Free Python script generator for Excel automation - combine a folder of workbooks, split a sheet by column, build a summary pivot, or clean and dedupe data. Copy or download a ready-to-run script.

Four Excel chores that take a morning by hand and seconds in Python: stacking a folder of monthly files, splitting one sheet into a file per branch, building a summary with totals, and cleaning a messy export. Pick one, type your names, and copy a script that runs as-is. New to Python and Excel? Read how to automate Excel with Python first.

Script Generator

Pick a job, get a Python script

Fill in your folder, sheet and column names. The script updates as you type.

report.py
# Generated by the FormulaSurge Python Excel Script Generator
# Setup once:  pip install pandas openpyxl
# Run:         python report.py   (close the output file in Excel first)
import re
from pathlib import Path

import pandas as pd
from openpyxl.styles import Font


def safe(name) -> str:
    """Make a value usable as a sheet or file name (Excel allows 31 characters, no []:*?/\\)."""
    return re.sub(r'[\\/:*?"<>|\[\]]', "_", str(name)).strip()[:31] or "Blank"


def save(sheets: dict, out) -> None:
    """Write each DataFrame to its own sheet: bold frozen header and readable column widths."""
    with pd.ExcelWriter(out, engine="openpyxl") as xl:
        for name, df in sheets.items():
            df.to_excel(xl, sheet_name=safe(name), index=False)
            ws = xl.sheets[safe(name)]
            ws.freeze_panes = "A2"
            for cell in ws[1]:
                cell.font = Font(bold=True)
            for col in ws.columns:
                width = max(len(str(c.value or "")) for c in col)
                ws.column_dimensions[col[0].column_letter].width = min(width + 2, 50)
    print(f"Saved {out}")


FOLDER = Path(r"C:\Reports\Monthly")
OUT = Path(r"C:\Reports\Combined.xlsx")

frames = []
for file in sorted(FOLDER.glob("*.xlsx")):
    if file.name.startswith("~$") or file.resolve() == OUT.resolve():
        continue  # skip Excel lock files and last run's output
    df = pd.read_excel(file, sheet_name=0)
    df["Source file"] = file.name
    frames.append(df)
    print(f"Read {len(df):,} rows from {file.name}")

if not frames:
    raise SystemExit(f"No .xlsx files found in {FOLDER}")

combined = pd.concat(frames, ignore_index=True)
save({"Combined": combined}, OUT)

Test it on a copy of your files first. Need it to run on a schedule, email the result or handle messy source files?

Get the whole report automated

Python or Power Query?

Not sure which tool should run your report?

If everyone who runs the report lives in Excel, Power Query is usually the safer choice. Send a short description of the report and I will say which fits, in writing.

Self-serve

No signup

Direct channels

Score it, then
build it.

Free tools
16
Time to result
<2m
Forms to fill
0
  1. 1Run the tool
  2. 2Send the output
  3. 3Get the build
Mutual NDA availableCalls scheduled in your timezone

Self-serve standards

Six rules on every free tool on this site.

Self-serve tools, not signup-walled demos. The same six rules on every calculator, audit and quiz we publish.