Python Intermediate Lesson 10: Project – A Monthly Report Pipeline

Python Intermediate Lesson 10: Project - A Monthly Report Pipeline 1

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

⏱ 2 min read

πŸ“˜ Python Intermediate Course Β· Lesson 10 of 10

Advertisement
In this article
  1. The shape of a good pipeline
  2. Make it production-ready
  3. Practice

The whole course in one script: drop next month’s CSV into monthly/, run, and the report is ready.

The shape of a good pipeline

def load(folder):      # 1. read every file, remember where each row came from
    frames = [pd.read_csv(f).assign(source=f.name) for f in sorted(folder.glob("sales-*.csv"))]
    return pd.concat(frames, ignore_index=True)

def enrich(df):        # 2. join products and customers, add amount, GST, month
    ...

df = enrich(load(HERE / "monthly"))
kpi = ...              # 3. summarise
with pd.ExcelWriter(out / "monthly-report.xlsx") as xw:   # 4. write
    kpi.to_frame("value").to_excel(xw, sheet_name="KPIs")
    by_month.to_excel(xw, sheet_name="By month")

Small functions with one job each are easy to test and easy to change: a new data source only touches load.

Make it production-ready

  • Logging (lesson 3): the row count per file catches a missing or half-downloaded month.
  • Paths from HERE (lesson 1) so Task Scheduler can run it.
  • Options (lesson 9): --month to rebuild a past report.
  • Formatting (lesson 7): open the written file with openpyxl afterwards to add styles and a chart.
πŸ’‘ Add a check at the end: if this month’s total differs from last month’s by more than 50%, log a WARNING. Most data problems show up as a strange total long before anyone spots the cause.

Next: read straight from a database with pd.read_sql (see the SQL Intermediate Course) instead of CSV exports.

Practice

Unzip the practice data into a folder, put this lesson’s script in the same folder and run it (python pyi-NN.py). Compare with the expected output file. Then change one thing (a filter, a column, a format) and predict the new output before you run it.

πŸ“Ž Practice files for this article

⬇ Download all 3 files (ZIP Β· 5 KB)

  • 🐍
    Lesson 10 scriptThe complete script from this lesson, ready to run.
    ⬇ PY Β· 2 KB
  • πŸ“„
    Expected outputWhat the script printed when we ran it, to compare with yours.
    ⬇ TXT Β· 477 B
  • πŸ—‚οΈ
    Practice data (zip)sales.csv (120 invoices, 2 deliberately broken rows), customers.csv, products.json, statement.txt, rates.json and 3 monthly CSVs.
    ⬇ ZIP Β· 4 KB

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

Leave a Reply

Your email address will not be published. Required fields are marked *