Working with Templates
Leave feedback
On this page
Templates let you extract structured values (fields, tables, barcodes) from recurring documents. The Python API mirrors the .NET template classes; see groupdocs.parser.templates for the full list.
Use TemplateRegexPosition to locate values by pattern and extract only the matched group:
from groupdocs.parser import Parser
from groupdocs.parser.templates import Template, TemplateField, TemplateRegexPosition
template = Template([
TemplateField(
TemplateRegexPosition(r"Invoice Number\s+(?<value>[A-Z0-9\-]+)"),
"InvoiceNumber",
)
])
with Parser("./invoice.pdf") as parser:
data = parser.parse_by_template(template)
if data is not None:
fields = data.get_fields_by_name("InvoiceNumber")
if fields:
invoice_number = fields[0].text
print(f"Invoice number: {invoice_number}")
The following sample file is used in this example: invoice.pdf
Define table bounds and column separators with TemplateTableParameters when you need to extract line items:
from groupdocs.parser import Parser
from groupdocs.parser.data import Rectangle, Point, Size
from groupdocs.parser.templates import (
Template,
TemplateItem,
TemplateTable,
TemplateTableParameters,
)
table_area = Rectangle(Point(40.0, 300.0), Size(515.0, 98.0))
columns = [40.0, 120.0, 330.0, 390.0, 470.0, 555.0]
table = TemplateTable(
parameters=TemplateTableParameters(table_area, columns),
name="Details",
)
template = Template([table])
with Parser("./invoice.pdf") as parser:
data = parser.parse_by_template(template)
if data is not None:
fields = data.get_fields_by_name("Details")
if fields:
details = fields[0].page_area
print(f"Rows extracted: {details.row_count}")
The following sample file is used in this example: invoice.pdf
- Combine regex, fixed, and linked positions in one template to anchor values reliably.
- Keep field names unique (case-insensitive).
- Reuse
TemplateTableLayoutwhen the same table structure appears on multiple pages—see the API reference for layout helpers. - If a template item cannot be located, the corresponding field/table is empty; handle
Nonein your code.
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.