Turn Any Python Script into an Automated PDF Report — Emailed on Schedule
Most reporting workflows end the same way: someone runs a query, formats the output, and emails it to a stakeholder. It's a five-minute task that somehow always happens at the worst possible time — right before a meeting, right after someone's already asked twice.
This tutorial shows how to generate a clean PDF report from your data using WeasyPrint, then email it automatically with smtplib. Then you'll see how to schedule the whole thing on CyberOak so it just shows up in the right inbox, on time, every time.
What This Automation Does
The script takes tabular data, renders it into a styled HTML report, converts that HTML to a PDF with WeasyPrint, and emails the PDF as an attachment.
For every run, it:
- Builds an HTML report from your data (a simple table, but easy to style further with real CSS)
- Converts the HTML to a pixel-accurate PDF using WeasyPrint
- Sends the PDF as an email attachment via
smtplib - Logs success or failure, so you know if the report actually went out
Why WeasyPrint?
WeasyPrint renders HTML and CSS using an actual browser-grade layout engine, which means it handles modern CSS — flexbox, grid, @page rules, custom fonts, page headers/footers — far more faithfully than older HTML-to-PDF converters. If you want a report that actually looks designed rather than like a plain table dump, WeasyPrint gives you much more control over the final output.
Why Build This?
A report that sits in a dashboard only helps the people who remember to check the dashboard. A report that lands in someone's inbox gets read.
This pattern covers a lot of common recurring needs:
- Weekly sales or KPI summaries to a manager
- Daily error/exception digests to an engineering team
- Monthly finance close reports to stakeholders
- Client-facing status reports for an agency
The report generation and the delivery are both simple — the part people actually skip is doing it reliably, on schedule, without a human in the loop.
Why Run It on CyberOak?
Sending email from a script usually means hardcoding SMTP credentials somewhere you'd rather not, and remembering to actually trigger the script.
CyberOak removes both problems:
- Upload the script — WeasyPrint and
smtplibrun out of the box - Store your SMTP host, port, username, password, and recipient as Key-Value Parameters — encrypted at rest, injected only at runtime, never hardcoded
- Schedule it — daily, weekly, monthly, or anything from every 10 minutes to once a year
- Get alerted — a dashboard and email alert if the automation itself fails, separate from the report email it sends
- Review logs and outputs per run — if a report didn't go out, you'll know exactly why
How the Script Works
- Reads SMTP and recipient details from CyberOak's Key-Value Parameters.
- Builds a styled HTML report from the report data.
- Converts that HTML into a PDF file using WeasyPrint.
- Attaches the PDF to an email message.
- Sends it via
smtplibover a secure connection. - Logs the outcome — success, or the specific error if something failed.
Output Example
Each run produces a styled PDF like this, attached directly to an email:
| Metric | Value | |--------|-------| | New Signups | 128 | | Active Trials | 342 | | Churned Accounts | 7 |
Swap the data source for a database query, an API call, or a CSV upload, and the same script becomes almost any recurring report.
Customizing the Report
This script is intentionally simple so it's easy to extend:
- Pull
report_datafrom PostgreSQL, an API, or an uploaded Excel file instead of hardcoding it - Add a real logo, custom fonts, or a multi-page layout using WeasyPrint's
@pageand CSS support - Send to multiple recipients, or a different recipient per report type
- Attach the raw CSV alongside the PDF for recipients who want the underlying data
- Add conditional logic — only send the email if a metric crosses a threshold
Scheduling Your Report Emails
This is the step that turns a "nice script" into something your team actually relies on.
Schedule it on CyberOak to run:
- Every Monday morning, before your team's weekly sync
- Daily, for an overnight error digest
- Monthly, for a recurring stakeholder update
- On-demand, whenever someone needs a fresh copy immediately
If the SMTP connection fails or the PDF generation breaks, you'll get a CyberOak dashboard and email alert about the automation — separate from the report it's supposed to send — so a silent failure never happens.
Who Can Use This?
This automation fits anyone who owns a recurring "send this report" task:
- Founders and managers who want a weekly metrics digest without asking for one
- Ops and finance teams sending recurring stakeholder updates
- Agencies sending client status reports
- Engineering teams wanting a daily error/exception summary
- Anyone tired of manually attaching a file to an email every week
Conclusion
Generating and emailing a report doesn't need a reporting platform or a scheduled task on someone's laptop. WeasyPrint handles the PDF with real CSS control, smtplib handles the delivery, and together they cover most "send this report" needs in under 100 lines.
Upload it to CyberOak, store your SMTP credentials once, set a schedule, and the report starts showing up in the right inbox — on its own, from now on.