Python · Industrial Automation

Generate PDF Reports from SQL Server Using Python

Generate PDF reports from SQL Server using Python, pyodbc, pandas and ReportLab. Build plant production summaries, tables, timestamps and reusable PDF outputs.

Practical Python Automation Context Copy-Paste Examples Code Architecture

Learning Overview

Python Learning SeriesPython for Industrial AutomationPractical

Learning Goal

  • Generate PDF reports from SQL Server using Python, pyodbc, pandas and ReportLab. Build plant production summaries, tables, timestamps and reusable PDF outputs.
  • Run the examples in VS Code using the project environment prepared in earlier topics.
  • Understand where this topic fits in the complete SQL, reporting and Industry 4.0 workflow.
Quick answer

Generate PDF reports from SQL Server using Python, pyodbc, pandas and ReportLab. Build plant production summaries, tables, timestamps and reusable PDF outputs.

Architecture: SQL Server → PDF

Code-Based Architecture

SQL Server → PDF

1

SQL Server

Source plant records

dbo.tblEvent
2

pyodbc

Connect and query

SELECT ...
3

pandas

Prepare report data

DataFrame
4

ReportLab

Build PDF document

SimpleDocTemplate
5

Tables

Add headers and rows

Table(...)
6

PDF File

Save/share report

SQF_Report.pdf
SQL ServerpyodbcpandasReportLabTablesPDF File

1. Install the PDF library

python -m pip install reportlab

2. Prepare report data

# Assume df contains SQL Server records
report_df = df[["DT", "SQF_No", "ChargeNo", "Temp_Act"]].copy()
report_df["DT"] = report_df["DT"].astype(str)

3. Create a simple PDF

from reportlab.lib.pagesizes import A4
from reportlab.platypus import SimpleDocTemplate, Paragraph
from reportlab.lib.styles import getSampleStyleSheet pdf = SimpleDocTemplate("SQF_Report.pdf", pagesize=A4)
styles = getSampleStyleSheet()
story = [Paragraph("SQF Production Report", styles["Title"])]
pdf.build(story)

4. Add a DataFrame table

from reportlab.platypus import Table, TableStyle
from reportlab.lib import colors rows = [report_df.columns.tolist()] + report_df.astype(str).values.tolist()
table = Table(rows, repeatRows=1)
table.setStyle(TableStyle([ ("BACKGROUND", (0, 0), (-1, 0), colors.lightgrey), ("GRID", (0, 0), (-1, -1), 0.5, colors.grey),
]))
story.append(table)

5. Use dated filenames

from datetime import datetime
name = datetime.now().strftime("SQF_Report_%Y%m%d_%H%M%S.pdf")
print(name)

6. PDF report checklist

  • Title and generation timestamp
  • Source database/report filter
  • Readable column widths and page layout
  • Output folder exists and is writable

Frequently Asked Questions

Which Python library can create PDF reports?

ReportLab is a widely used Python library for programmatic PDF generation.

Can pandas write PDF directly?

pandas does not provide a general DataFrame-to-PDF writer; use a PDF library or another document/reporting layer.

Should PDF reports use the same cleaned DataFrame as Excel?

Yes. Reusing one validated DataFrame helps keep Excel and PDF outputs consistent.

Reviewed by Bhawesh Kumar SinghIndustrial Automation Trainer and Industry 4.0 Consultant · Softwell Automation · 21+ years industry experience
Complete Python for Industrial Automation Learning Path

Use Previous / Next for sequential training, or open any topic directly.

Complete

Continue with the next topic in the Python for Industrial Automation learning path.

Open Learning Path
Python for Industrial Automation

SQL Server → PDF

Generate PDF reports from SQL Server using Python, pyodbc, pandas and ReportLab. Build plant production summaries, tables, timestamps and reusable PDF outputs.

Content reviewed: 14 September 2026

☎ Call WhatsApp ✉ Email Enquire Now