Make a worksheet table PDF with Python
Render the first 100 worksheet rows as a landscape table with repeated headers.
View code and setup
Before you start
- Install Python 3. Create the environment below, save the Python example as task.py, then run .venv/bin/python task.py in the folder containing input.xlsx. On Windows, use py -m venv .venv and .venv\Scripts\python.exe in place of .venv/bin/python. Replace Inventory with your worksheet name.
python3 -m venv .venv
.venv/bin/python -m pip install openpyxl reportlabfrom reportlab.lib import colors
from reportlab.lib.pagesizes import A4, landscape
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle
from openpyxl import load_workbook
workbook = load_workbook("input.xlsx", read_only=True, data_only=True)
sheet = workbook["Inventory"]
rows = [["" if value is None else str(value) for value in row]
for row in sheet.iter_rows(max_row=min(sheet.max_row, 100), values_only=True)]
workbook.close()
if not rows:
raise ValueError("The worksheet is empty")
table = Table(rows, repeatRows=1)
table.setStyle(TableStyle([("GRID", (0, 0), (-1, -1), 0.5, colors.grey),
("BACKGROUND", (0, 0), (-1, 0), colors.lightgrey)]))
SimpleDocTemplate("output.pdf", pagesize=landscape(A4)).build([table])Limits and output
- These data recipes use saved cell results, not recalculated formulas. They do not copy charts, styles, macros or workbook relationships. Keep the original file.
- This is a bounded 100-row example. It does not reproduce Excel print areas, charts or styling. Wide tables and long cell text need explicit column sizing or wrapping before use.
Documentation: openpyxl tutorial and workbook loading options; ReportLab table guide