Basic Template Syntax - Expression Tags and Fields
Leave feedback
Overview
GroupDocs.Assembly for Python via .NET uses a simple, LINQ-based template syntax that allows you to embed expressions directly in your documents. Expression tags are the foundation of template syntax, enabling you to output data values and perform calculations.
Create a template document in Word, Excel, PowerPoint, or HTML format
Insert expression tags using <<[expression]>> syntax
Reference the keys of your Python dicts (and the methods of their values) in expressions
Assemble the document with your data source
Note
Template syntax is case-sensitive, and the expressions inside tags follow C# language rules whichever language calls the assembler: a Python program’s templates still write &&, == and "text". A dict key is a member name: person.Name reads the "Name" key of the dict passed as person. Expression tags are evaluated at runtime and replaced with their computed values.
Basic Expression Tags
Expression tags are the simplest way to output data in templates. They use the syntax <<[expression]>>:
fromgroupdocs.assemblyimportDataSourceInfo,DocumentAssembler# Sample data: the keys are the member names the template readsperson={"Name":"Amara Osei","Age":41,"Email":"amara.osei@harborlane.example"}# Assemble documentassembler=DocumentAssembler()assembler.assemble_document("person-details.docx","person-details-report.docx",DataSourceInfo(person,"person"))print("Document assembled with expression tags.")
The price is a Decimal, so it reaches the template as a .NET decimal and keeps its two decimal places: 18.40.
Simple Calculations in Expressions
You can perform calculations directly in expression tags:
fromdecimalimportDecimalfromgroupdocs.assemblyimportDataSourceInfo,DocumentAssemblerorder={"Quantity":5,"UnitPrice":Decimal("18.40")}assembler=DocumentAssembler()assembler.assemble_document("order-calculation.docx","order-calculation-report.docx",DataSourceInfo(order,"order"))print("Document assembled with calculations.")
Quantity: <<[order.Quantity]>>
Unit Price: <<[order.UnitPrice]>>
Total: <<[order.Quantity * order.UnitPrice]>>
The total is 92.00: an int times a Decimal is a decimal in the template, as in Python.
Using String Methods
You can use .NET string methods in expressions: a Python str reaches the template as a .NET string.
fromgroupdocs.assemblyimportDataSourceInfo,DocumentAssemblercompany={"Name":"Harbor Lane Provisions","Website":"HTTPS://HarborLane.example/"}assembler=DocumentAssembler()assembler.assemble_document("company-header.docx","company-header-report.docx",DataSourceInfo(company,"company"))print("Document assembled with string methods.")
You can format expression results using format strings:
fromdatetimeimportdatetimefromdecimalimportDecimalfromgroupdocs.assemblyimportDataSourceInfo,DocumentAssemblerinvoice={"Amount":Decimal("1500.50"),"Date":datetime(2015,1,15),"Percentage":0.15,}assembler=DocumentAssembler()assembler.assemble_document("invoice-format-strings.docx","invoice-format-strings-report.docx",DataSourceInfo(invoice,"invoice"))print("Document assembled with formatting.")
A date format needs a datetime (or a date) in the data. A string is written as it is: <<[invoice.Date]:"MMMM dd, yyyy">> gives January 15, 2015 for datetime(2015, 1, 15) but 2015-01-15 for the text "2015-01-15".
Warning
Format strings follow .NET formatting conventions, not Python’s: use “C” for currency, “P” for percentage, and .NET date/time format strings such as “yyyy-MM-dd” for dates (not %Y-%m-%d). “C” and “P” follow the culture of the process: under en-US the amount above is written $1,500.50.
Advanced Usage Topics
To learn more about data bands, conditional blocks, LINQ expressions, and advanced template syntax features, please refer to the advanced usage section.
More resources
GitHub Examples
You may easily run the code above and see the feature in action in our GitHub examples: