PREDICTIVE TREND INSIGHT
Auto-generating executive PDF reports and dashboards with AI summaries Illustration

Auto-generating executive PDF reports and dashboards with AI summaries

Direct Summary:

An auto-generated executive PDF report is really three separate pieces glued together: pandas computes the actual numbers, matplotlib renders the charts, and an LLM writes a short narrative summary — but only from the already-computed figures, never by calculating them itself. A PDF library (WeasyPrint or ReportLab) then combines the charts and AI-written narrative into the final document. Keeping the arithmetic in pandas and the prose in the LLM is what keeps the numbers in the report trustworthy.

"Any sufficiently advanced technology is indistinguishable from magic."

— Arthur C. Clarke

Key Insights

  • Compute first, narrate second: pass the LLM your already-calculated totals and trends, and ask it to write about them — never ask it to derive the numbers itself from raw data in the prompt.
  • Charts come from matplotlib/plotting libraries, not the model: an LLM can't render a pixel-accurate chart — generate the visualization with a real plotting library and embed the image in the PDF.
  • The PDF assembly step is standard document tooling: WeasyPrint (HTML/CSS to PDF) or ReportLab (programmatic PDF generation) are the actual tools that lay out the final document — the AI's role stops at writing the summary text.

"AI-generated executive report" invites the assumption that the model is doing everything — crunching the data, making the charts, writing the summary. In a reliable pipeline, it does exactly one of those three things: writing prose about numbers someone else (pandas) already computed correctly. Splitting the pipeline this way means a factual error in the executive summary can only be a wording problem, never an arithmetic one.

Building the three-stage pipeline

1. Compute the real metrics with pandas. Aggregate whatever the report needs (monthly revenue, top categories, month-over-month change) using standard pandas operations — this is where the actual numbers in the report come from.

2. Render charts with matplotlib and save as images. Generate the visual elements as PNG/SVG files using a real charting library, then embed them in the report layout — don't ask an LLM to "draw" anything.

3. Feed the computed numbers (not raw data) to the LLM for a narrative summary. Give it the finished metrics — "revenue was $42,300, up 8% from last month" — and ask for 2-3 sentences of executive framing, then assemble everything into a PDF with WeasyPrint or ReportLab.

executive_report.py
# pip install pandas matplotlib anthropic weasyprint
import pandas as pd
import anthropic

df = pd.read_csv("monthly_sales.csv")
total_revenue = df["amount"].sum()          # real arithmetic, done in pandas
top_category = df.groupby("category")["amount"].sum().idxmax()

client = anthropic.Anthropic()
summary = client.messages.create(
    model="claude-sonnet-5",
    max_tokens=150,
    system="Write a 2-sentence executive summary from these pre-computed figures. "
           "Do not calculate or estimate any numbers yourself -- use only what's given.",
    messages=[{"role": "user",
               "content": f"Total revenue: ${total_revenue:,.2f}. Top category: {top_category}."}]
)
# Next: render a matplotlib chart and pass both to WeasyPrint/ReportLab
Report Component Generated By
Totals, aggregates, trend calculations pandas (deterministic arithmetic)
Charts and visualizations matplotlib (real rendering)
Narrative summary text LLM, working only from pre-computed figures

The pipeline is only as trustworthy as its weakest link — and the weakest link would be letting the LLM touch the raw numbers directly. Keep it constrained to writing about figures it's handed, and a reviewer only needs to check the prose reads accurately, not re-verify the math.

Practical Challenge

Build the pandas aggregation and LLM summary steps above for a sample sales CSV, then verify that the summary text's stated numbers exactly match what pandas actually computed.

Concept Check

In a reliable AI-generated executive report pipeline, what should the LLM be given to write its summary from?
Correct! Feeding pre-computed figures to the model confines its role to narrative writing, eliminating the risk of it silently miscalculating a total.
Incorrect. Try again! Giving the model raw data to calculate from reintroduces the exact arithmetic-reliability risk this pipeline is designed to avoid.

Sources & Further Reading

  • WeasyPrint — the open-source HTML/CSS-to-PDF rendering library referenced for final report assembly.
  • Matplotlib Documentation — reference for generating the chart images embedded in the report.
Previous Guide Dashboard Next Guide