Python Lesson 12: Project β€” Automate a Monthly Report

πŸ“Ž This article includes 3 downloadable practice files ↓

⏱ 3 min read

πŸ“˜ Python Beginner Course Β· Lesson 12 of 12

In this article
  1. What the script does
  2. Combine a folder of files
  3. KPIs and summary
  4. Write the report
  5. Run it automatically
  6. Ideas to extend it
  7. You’ve finished the course
  8. Practice

Here’s the job that makes Python pay for itself: every month, files land in a folder; someone copies them into one sheet, builds the same pivots and emails a report. This project automates all of it.

What the script does

  1. Finds every sales_*.csv in an incoming folder.
  2. Combines them, noting which file each row came from.
  3. Calculates KPIs and a month Γ— region table.
  4. Writes one Excel report with three sheets, named with the date.

Combine a folder of files

import pandas as pd, pathlib
src = pathlib.Path("incoming")
files = sorted(src.glob("sales_*.csv"))
df = pd.concat([pd.read_csv(f, parse_dates=["date"]).assign(source=f.name) for f in files],
               ignore_index=True)
df["amount"] = df["qty"] * df["rate"]

(The practice script first splits sales.csv into monthly files to simulate them arriving.)

KPIs and summary

kpis = pd.DataFrame({"metric": ["Files", "Orders", "Total sales", "Average order"],
                     "value": [len(files), len(df), df["amount"].sum(), round(df["amount"].mean())]})
by_month = df.pivot_table(values="amount", index=df["date"].dt.strftime("%Y-%m"),
                          columns="region", aggfunc="sum", fill_value=0)

Write the report

import datetime as dt
out = f"sales_report_{dt.date.today():%Y%m%d}.xlsx"
with pd.ExcelWriter(out) as xw:
    kpis.to_excel(xw, sheet_name="KPIs", index=False)
    by_month.to_excel(xw, sheet_name="By month")
    df.to_excel(xw, sheet_name="All orders", index=False)

Add the openpyxl formatting from Lesson 11 to make it presentable.

Run it automatically

  1. Open Task Scheduler β€Ί Create Basic Task.
  2. Trigger: Monthly, day 2, 9:00 am.
  3. Action: Start a program β€Ί Program: python (or the full path to python.exe) β€Ί Arguments: lesson-12.py β€Ί Start in: the script’s folder.
πŸ’‘ Log what happened: append a line with the date, file count and total to a log.txt at the end of the script. When something goes wrong in month seven, you’ll know when it started.
⚠️ Before emailing reports automatically, run the script by hand for a couple of months and compare its numbers with the manual report. Automation repeats mistakes perfectly too.

Ideas to extend it

  • Email the report with Outlook (win32com) or smtplib; see sending email from Excel for the Outlook side.
  • Move processed files to an archive folder so they aren’t counted twice.
  • Read from a database with SQL (the SQL course) instead of CSVs: pd.read_sql(query, connection).

You’ve finished the course

You can install Python, write variables, decisions, loops and functions, handle files, clean and summarise data with pandas, produce formatted Excel files and automate a recurring report. Next: more Python automation, and Power Query for the no-code route.

Practice

Download the script (and data file, if listed) below into one folder. Open a terminal in that folder and run python lesson-NN.py. Compare with the expected output, then change something and run it again: that’s how Python is learned.

πŸ“Ž Practice files for this article

Free to use for learning. Files with macros (.bas) are plain text β€” import them with Alt+F11 β†’ File β†’ Import File, and always test on a copy.

✨ Ask AI about this article

Stuck on a step? Ask a question and the AI answers using this article.

Free Β· AI can be wrong