DEV Community

 AI Automation Enterenpeur
AI Automation Enterenpeur

Posted on

Build a Self-Writing Weekly KPI Report in Python

Every Monday someone on a small team opens four tabs, copies numbers into a spreadsheet, and writes a summary. Let's replace that with a short Python script that runs on a schedule, compares this week to last week, flags big changes, and posts a summary to Slack.

What we're building

  • A CSV (or Google Sheet export) with one row per week and one column per metric
  • A script that reads the last two rows and calculates the change for each metric
  • A rule that flags anything that moved more than 10%
  • A Slack message with a short summary
  • A cron job that runs it every Monday morning

Step 1: Store the history

Keep a file called metrics.csv that looks like this:

week,leads,booked_calls,revenue,tickets
2026-11-09,42,18,12400,31
2026-11-16,47,14,13150,29
Enter fullscreen mode Exit fullscreen mode

In production you'd fill each row automatically from your tools' APIs (Stripe, your CRM, analytics). For this post we'll assume the row is already there so we can focus on the report logic.

Step 2: The script

import csv
import os
import requests

WEBHOOK = os.environ["SLACK_WEBHOOK_URL"]
THRESHOLD = 0.10  # flag changes bigger than 10%

def load_last_two(path="metrics.csv"):
    with open(path, newline="") as f:
        rows = list(csv.DictReader(f))
    return rows[-2], rows[-1]

def build_report(prev, curr):
    lines = [f"Weekly report for {curr['week']}"]
    for key in curr:
        if key == "week":
            continue
        old, new = float(prev[key]), float(curr[key])
        change = (new - old) / old if old else 0
        flag = " ⚠️" if abs(change) > THRESHOLD else ""
        lines.append(f"{key}: {new:g} ({change:+.0%} vs last week){flag}")
    return "\n".join(lines)

if __name__ == "__main__":
    prev, curr = load_last_two()
    report = build_report(prev, curr)
    requests.post(WEBHOOK, json={"text": report}, timeout=10)
    print(report)
Enter fullscreen mode Exit fullscreen mode

With the sample data above, the output looks like:

Weekly report for 2026-11-16
leads: 47 (+12% vs last week) ⚠️
booked_calls: 14 (-22% vs last week) ⚠️
revenue: 13150 (+6% vs last week)
tickets: 29 (-6% vs last week)
Enter fullscreen mode Exit fullscreen mode

Step 3: Schedule it

On any Linux box, add a cron entry that runs every Monday at 7 AM:

0 7 * * 1 cd /opt/kpi && SLACK_WEBHOOK_URL=... /usr/bin/python3 report.py
Enter fullscreen mode Exit fullscreen mode

You can also run it as a scheduled GitHub Action or a serverless cron job if you don't want to manage a server.

Ideas to extend it

  • Compare against a 4-week average instead of just last week, so one odd week doesn't trigger noise
  • Pull each metric from its API in a fetch step before the report runs
  • Send the same summary by email for people who don't live in Slack
  • Write the flags to a separate sheet so you can see which metrics swing most often

Why this is worth 40 lines

The script saves an hour or two a week, but the bigger payoff is consistency. The report arrives in the same format every Monday, trends are easy to spot, and the team's first conversation of the week is about decisions instead of data gathering.

If you want a reporting automation like this built and wired into your own tools, I do this kind of work at https://ai-automation-entrepreneur.vercel.app/

Top comments (0)