from pathlib import Path
import pandas as pd
SRC = Path("monthly")
DST = Path("combined.xlsx")
frames = []
for path in sorted(SRC.glob("*.xlsx")):
df = pd.read_excel(path)
df["source_file"] = path.name
frames.append(df)
print(f" loaded {path.name}: {len(df):,} rows")
combined = pd.concat(frames, ignore_index=True)
combined.to_excel(DST, index=False, sheet_name="All")
print(f"Wrote {len(combined):,} rows to {DST}")
def read_any(p):
if p.suffix == ".csv":
return pd.read_csv(p)
return pd.read_excel(p)
frames = [read_any(p) for p in SRC.iterdir() if p.suffix in (".csv", ".xlsx")]
frames = []
errors = []
for path in SRC.glob("*"):
try:
df = read_any(path)
df["source_file"] = path.name
frames.append(df)
except Exception as e:
errors.append((path.name, str(e)))
print(f"Processed: {len(frames)}, Errors: {len(errors)}")
for name, msg in errors:
print(f" {name}: {msg}")
folder.glob("*.xlsx") + pd.concat() is the merge skeleton.source_file — you'll always want it later.Put 3-5 small CSVs in a folder. Write a script that merges them into one Excel file with a "source" column.