Q2Report is a lightweight Python report builder for creating formatted reports in PDF, XLSX, DOCX, and HTML.
It combines a report layout (pages, columns, rows, and cells) with named data sets, expressions, styles, images, tables, grouping, headers, and footers.
Python 3.8.1 or newer is required.
pip install q2reportPDF output requires a supported Qt binding: PySide or PyQt. If none is installed, generating a PDF raises an ImportError. Q2Report detects an installed binding automatically; you do not need to configure which one to use.
For example, install Q2Report with PySide6:
pip install q2report pyside6The Qt binding is only needed for PDF output. HTML, XLSX, and DOCX output do not require PySide or PyQt.
from q2report import Q2Report
report = Q2Report()
report.set_style(report.make_style(border_width="0 0 1 0"))
report.add_page(page_width=21, page_height=29.7)
report.add_column(width="20%")
report.add_column()
report.set_cell(0, 0, "First Name", style={"font-weight": "bold"})
report.set_cell(0, 1, "{firstname}")
report.set_cell(1, 0, "Last Name", style={"text-align": "right"})
report.set_cell(1, 1, "{lastname}")
data = {"firstname": "Ada", "lastname": "Lovelace"}
rep = report.run("temp/report.docx", data=data)This example opens the docx automatically after rendering. Set open_output_file=False to suppress that behavior.
from q2report import Q2Report, Q2Report_rows
report = Q2Report()
report.add_page()
report.add_column(width=3)
report.add_column(width=3)
num_style = {"text-align": "right"}
table = Q2Report_rows(
role="table",
data_source="items",
style=report.make_style(border_width="0 0 1 0"),
)
table.set_cell(0, 0, "{name}")
table.set_cell(0, 1, "{num(price)*num(qt)}", format="F2",
name="line_total", style=num_style)
header = Q2Report_rows(style={"text-align": "center", "font-size": 8})
header.set_cell(0, 0, "Item")
header.set_cell(0, 1, "Total")
table.set_table_header(header)
footer = Q2Report_rows(style=num_style)
footer.set_cell(0, 0, "Grand total")
footer.set_cell(0, 1, "{sum:line_total}", format="F2",
style={"font-weight": "bold"})
table.set_table_footer(footer)
report.add_rows(rows=table)
data = {
"items": [
{"name": "Pen", "price": "2.50", "qt": 10},
{"name": "Notebook", "price": "4.00", "qt": 3},
]
}
report.run("temp/report.xlsx", data=data)The output format is inferred from the file extension:
.html.pdf.xlsx.docx
You can pass output_type explicitly if needed.
By default, report.run() opens the generated file using the system's default application (HTML opens in a browser tab). Use open_output_file=False to disable this, for example in server-side generation. If an existing output file is locked by a viewer, Q2Report may choose a numbered filename; the exact behavior depends on the operating system. run() returns the actual output path. For applications generating many files, consider an application-specific directory under the system temporary directory, keeping in mind that temporary paths can be less convenient for users.
The report structure does not have to be built in Python code. It can be stored in JSON and loaded with report.load(...). Programmatic layout creation with Q2Report and Q2Report_rows is also available, but JSON is useful for reusable templates, report editors, database storage, and generated layouts.
from q2report import Q2Report
report = Q2Report()
report.load("test_data/test-report-01.json")
with open("test_data/test-data-01.json", encoding="utf-8") as file:
import json
data = json.load(file)
report.run("temp/report.pdf", data=data)load() accepts a JSON file path, a JSON string, or a Python dictionary. A JSON report defines the report style and nested pages, column sections, rows, and cells. It can also describe table headers and footers, group headers and footers, data sources, aggregates, styles, row heights, and column widths.
See Report JSON format for the complete structure and examples.
Report layouts can be created by writing Python or JSON, or by using a visual editor.
Desktop/offline editor: Q2Report includes a visual report editor in q2rad, an offline desktop platform for developing Python desktop applications. Q2Rad can build and edit report layouts and uses the same report structure described in this documentation.
Browser editor: A browser-based SaaS report editor is coming soon at q2report.com.
python demo/demo.pyMost individual demo modules expose a demo(type="pdf", open_output_file=True) function.

