Python: Merge Multiple PDFs into One
Merge several PDF files into one with Python in a few lines — keep the page order you choose.
Read a messy CSV with pandas, clean the data, build a short summary, and email the report automatically.
38 lines · Updated Nov 26, 2026
import pandas as pd
import smtplib
from email.message import EmailMessage
def build_report(csv_path):
df = pd.read_csv(csv_path)
df = df.dropna(how="all")
df["amount"] = pd.to_numeric(df["amount"], errors="coerce")
df = df.dropna(subset=["amount"])
total = df["amount"].sum()
count = len(df)
average = df["amount"].mean()
return (
f"Weekly report\n"
f"Rows: {count}\n"
f"Total: {total:,.2f}\n"
f"Average: {average:,.2f}\n"
f"Top item: {df.loc[df['amount'].idxmax(), 'item']}"
)
def send_report(report, to_address):
msg = EmailMessage()
msg["Subject"] = "Weekly CSV report"
msg["From"] = "reports@example.com"
msg["To"] = to_address
msg.set_content(report)
with smtplib.SMTP("localhost") as server:
server.send_message(msg)
if __name__ == "__main__":
report = build_report("sales.csv")
print(report)
send_report(report, "you@example.com")
Raw CSV exports from shops, CRMs and accounting tools are messy: empty rows, duplicate orders, inconsistent dates and numbers stored as text. This snippet uses pandas to load a CSV, clean it, calculate a short summary and email the result — turning a 20-minute weekly chore into a script you can schedule.
pip install pandas (add openpyxl for Excel output).encoding="latin-1" or "utf-8-sig".Can pandas handle large files? Hundreds of thousands of rows are fine on a normal laptop. For larger files, read in chunks.
Related: 10 Python automation projects · Retry with exponential backoff
Install pandas with pip install pandas. Change the column names (amount, item) to match your CSV, and set the SMTP host and addresses for your mail setup.
Share this snippet
Merge several PDF files into one with Python in a few lines — keep the page order you choose.
Read the text out of any PDF with Python — the first step for search, summaries and automation.
Send yourself a Telegram message from Python — free alerts for scripts, servers and price trackers.