Use Simple Aggregates (Sum, Count, Average)
Leave feedback
GroupDocs.Assembly for Python via .NET supports LINQ-based aggregate functions that allow you to calculate sums, counts, averages, and other aggregate values directly in templates. These functions work with collections and enable you to display totals, summaries, and statistical information in your documents.
The main aggregate functions are:
Sum()- calculate sum of numeric valuesCount()- count number of itemsAverage()- calculate average of numeric valuesMin()- find minimum valueMax()- find maximum value
Here are the steps to use aggregates:
- Create a template with aggregate expressions
- Use LINQ aggregate methods on collections
- Reference aggregate results in your template
- Assemble the document with your data
NoteAggregate functions use LINQ syntax and work with any collection the template sees: a Python list, or the items of a JSON, XML or CSV data source. Keep money inDecimalvalues: their sums and averages keep exact cents.
Calculate the sum of values in a collection:
from decimal import Decimal
from groupdocs.assembly import DataSourceInfo, DocumentAssembler
items = [
{"ProductName": "Harbor Blend whole bean coffee", "Price": Decimal("18.40"), "Quantity": 1},
{"ProductName": "Oat and honey biscuits", "Price": Decimal("3.20"), "Quantity": 2},
{"ProductName": "Chestnut honey", "Price": Decimal("7.80"), "Quantity": 1},
]
assembler = DocumentAssembler()
assembler.assemble_document("aggregate-sum.docx", "aggregate-sum-report.docx",
DataSourceInfo(items, "items"))
print("Sum calculated successfully.")
The following sample file is used in this example: aggregate-sum.docx
Binary file (DOCX, 7 KB)
In your template (aggregate-sum.docx):
Items:
<<foreach [in items]>><<[ProductName]>> - <<[Price]:"C">> x <<[Quantity]>>
<</foreach>>
Total: <<[items.Sum(i => i.Price * i.Quantity)]:"C">>
Count the number of items in a collection:
from groupdocs.assembly import DataSourceInfo, DocumentAssembler
products = [
"Harbor Blend whole bean coffee",
"Aged harbour cheddar",
"Cold-pressed olive oil",
"Smoked sea salt",
]
assembler = DocumentAssembler()
assembler.assemble_document("aggregate-count.docx", "aggregate-count-report.docx",
DataSourceInfo(products, "products"))
print("Count calculated successfully.")
The following sample file is used in this example: aggregate-count.docx
Binary file (DOCX, 7 KB)
Template:
Products:
<<foreach [p in products]>>- <<[p]>>
<</foreach>>
Total Products: <<[products.Count()]>>
The products are strings, so the data band names its iteration variable (p) and outputs it with <<[p]>>.
Calculate the average of numeric values:
from decimal import Decimal
from groupdocs.assembly import DataSourceInfo, DocumentAssembler
sales = [
{"Amount": Decimal("1000.00")},
{"Amount": Decimal("1500.00")},
{"Amount": Decimal("800.00")},
{"Amount": Decimal("1200.00")},
]
assembler = DocumentAssembler()
assembler.assemble_document("aggregate-average.docx", "aggregate-average-report.docx",
DataSourceInfo(sales, "sales"))
print("Average calculated successfully.")
The following sample file is used in this example: aggregate-average.docx
Binary file (DOCX, 7 KB)
Template:
Sales:
<<foreach [in sales]>><<[Amount]:"C">>
<</foreach>>
Average Sale: <<[sales.Average(s => s.Amount)]:"C">>
Find minimum and maximum values:
from decimal import Decimal
from groupdocs.assembly import DataSourceInfo, DocumentAssembler
sales = [
{"Amount": Decimal("1000.00")},
{"Amount": Decimal("1500.00")},
{"Amount": Decimal("800.00")},
]
assembler = DocumentAssembler()
assembler.assemble_document("aggregate-min-max.docx", "aggregate-min-max-report.docx",
DataSourceInfo(sales, "sales"))
print("Min and max calculated successfully.")
The following sample file is used in this example: aggregate-min-max.docx
Binary file (DOCX, 7 KB)
Template:
Minimum Sale: <<[sales.Min(s => s.Amount)]:"C">>
Maximum Sale: <<[sales.Max(s => s.Amount)]:"C">>
Use multiple aggregates in the same template:
from decimal import Decimal
from groupdocs.assembly import DataSourceInfo, DocumentAssembler
items = [
{"ProductName": "Harbor Blend whole bean coffee", "Price": Decimal("18.40"), "Quantity": 1},
{"ProductName": "Oat and honey biscuits", "Price": Decimal("3.20"), "Quantity": 2},
{"ProductName": "Chestnut honey", "Price": Decimal("7.80"), "Quantity": 1},
]
assembler = DocumentAssembler()
assembler.assemble_document("aggregate-summary.docx", "aggregate-summary-report.docx",
DataSourceInfo(items, "items"))
print("Multiple aggregates calculated successfully.")
The following sample file is used in this example: aggregate-summary.docx
Binary file (DOCX, 7 KB)
Template:
Items:
<<foreach [in items]>><<[ProductName]>> - <<[Price]:"C">> x <<[Quantity]>>
<</foreach>>
Summary:
Total Items: <<[items.Count()]>>
Total Quantity: <<[items.Sum(i => i.Quantity)]>>
Total Amount: <<[items.Sum(i => i.Price * i.Quantity)]:"C">>
Average Price: <<[items.Average(i => i.Price)]:"C">>
Use aggregates with filtered data:
from decimal import Decimal
from groupdocs.assembly import DataSourceInfo, DocumentAssembler
items = [
{"ProductName": "Harbor Blend whole bean coffee", "Price": Decimal("18.40"), "Quantity": 1},
{"ProductName": "Oat and honey biscuits", "Price": Decimal("3.20"), "Quantity": 2},
{"ProductName": "Lighthouse Espresso beans", "Price": Decimal("21.75"), "Quantity": 1},
]
assembler = DocumentAssembler()
assembler.assemble_document("aggregate-filtered.docx", "aggregate-filtered-report.docx",
DataSourceInfo(items, "items"))
print("Conditional aggregates calculated successfully.")
The following sample file is used in this example: aggregate-filtered.docx
Binary file (DOCX, 7 KB)
Template:
High-value items (Price > 15):
<<foreach [in items.Where(i => i.Price > 15)]>><<[ProductName]>> - <<[Price]:"C">>
<</foreach>>
Total High-value: <<[items.Where(i => i.Price > 15).Sum(i => i.Price)]:"C">>
Calculate aggregates for detail records within master records:
from decimal import Decimal
from groupdocs.assembly import DataSourceInfo, DocumentAssembler
orders = [
{
"OrderId": 1001,
"Items": [
{"ProductName": "Harbor Blend whole bean coffee", "Price": Decimal("18.40"), "Quantity": 1},
{"ProductName": "Oat and honey biscuits", "Price": Decimal("3.20"), "Quantity": 2},
],
},
]
assembler = DocumentAssembler()
assembler.assemble_document("aggregate-master-detail.docx", "aggregate-master-detail-report.docx",
DataSourceInfo(orders, "orders"))
print("Master-detail aggregates calculated successfully.")
The following sample file is used in this example: aggregate-master-detail.docx
Binary file (DOCX, 7 KB)
Template:
<<foreach [in orders]>>Order #<<[OrderId]>>
Items:
<<foreach [in Items]>> <<[ProductName]>> - <<[Price]:"C">> x <<[Quantity]>>
<</foreach>>
Order Total: <<[Items.Sum(i => i.Price * i.Quantity)]:"C">>
<</foreach>>
WarningAggregate functions require collections. Ensure your data source is a collection (a Python list, or the items of a JSON, XML or CSV data source) when using aggregates.
To learn more about grouping, filtering with aggregates, custom aggregate functions, and performance optimization, please refer to the advanced usage section.
You may easily run the code above and see the feature in action in our GitHub examples:
- GroupDocs.Assembly for .NET examples, plugins, and showcase
- GroupDocs.Assembly for Java examples, plugins, and showcase
- GroupDocs.Assembly for Python via .NET examples, plugins, and showcase
Along with the full-featured library, we provide simple but powerful free online apps.
To assemble documents from templates and data sources, you can use the online apps from the GroupDocs.Assembly App Product Family.
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.