Generate Tables from Collection Data
Leave feedback
Tables are one of the most common ways to display structured data in documents. GroupDocs.Assembly for Python via .NET makes it easy to generate dynamic tables by iterating over collections and populating table rows with data.
The main classes involved are:
- DocumentAssembler - assembles documents
- DataSourceInfo - wraps data sources
Here are the steps to generate tables from collections:
- Create a table in your template document with a header row
- Add a data band row using
<<foreach>>syntax to iterate over your collection - Reference collection item properties in table cells
- Close the data band with
<</foreach>> - Assemble the document with your collection data
NoteTable-row data bands automatically repeat the table row for each item in the collection, creating a complete table with all data rows.
Create a table from a list of dicts:
from decimal import Decimal
from groupdocs.assembly import DataSourceInfo, DocumentAssembler
# Create sample data
products = [
{"Name": "Harbor Blend whole bean coffee", "Price": Decimal("18.40"), "Quantity": 5},
{"Name": "Aged harbour cheddar", "Price": Decimal("16.50"), "Quantity": 20},
{"Name": "Cold-pressed olive oil", "Price": Decimal("12.90"), "Quantity": 15},
]
# Assemble document
assembler = DocumentAssembler()
assembler.assemble_document("stock-table.docx", "stock-table-report.docx",
DataSourceInfo(products, "products"))
print("Table generated successfully.")
The following sample file is used in this example: stock-table.docx
Binary file (DOCX, 7 KB)
In your template document (stock-table.docx), create a table with this structure:
| Product Name | Price | Quantity |
|---|---|---|
<<foreach [in products]>><<[Name]>> | <<[Price]>> | <<[Quantity]>><</foreach>> |
The data band spans the entire data row: it opens in the row’s first cell and closes in its last cell. After assembly, you’ll get:
| Product Name | Price | Quantity |
|---|---|---|
| Harbor Blend whole bean coffee | 18.40 | 5 |
| Aged harbour cheddar | 16.50 | 20 |
| Cold-pressed olive oil | 12.90 | 15 |
You can format table cell values using format strings:
from decimal import Decimal
from groupdocs.assembly import DataSourceInfo, DocumentAssembler
products = [
{"Name": "Harbor Blend whole bean coffee", "Price": Decimal("18.40"), "Quantity": 5},
{"Name": "Aged harbour cheddar", "Price": Decimal("16.50"), "Quantity": 20},
]
assembler = DocumentAssembler()
assembler.assemble_document("stock-price-table.docx", "stock-price-table-report.docx",
DataSourceInfo(products, "products"))
The following sample file is used in this example: stock-price-table.docx
Binary file (DOCX, 7 KB)
Template with formatting:
| Product Name | Price | Quantity |
|---|---|---|
<<foreach [in products]>><<[Name]>> | <<[Price]:"C">> | <<[Quantity]>><</foreach>> |
The :"C" format string formats the price as currency.
ADO.NET’s DataTable has no Python counterpart. Rows kept in a JSON file can be passed as they are with JsonDataSource: the file holds an array of objects, and their property names are the member names the template reads.
from groupdocs.assembly import DataSourceInfo, DocumentAssembler
from groupdocs.assembly.data import JsonDataSource
products = JsonDataSource("stock-list.json")
assembler = DocumentAssembler()
assembler.assemble_document("stock-price-table.docx", "stock-price-table-from-json.docx",
DataSourceInfo(products, "products"))
print("Table generated from JSON file successfully.")
The following sample file is used in this example: stock-price-table.docx
The following sample file is used in this example: stock-list.json
Binary file (DOCX, 7 KB)
Add a total row after the data rows:
from decimal import Decimal
from groupdocs.assembly import DataSourceInfo, DocumentAssembler
products = [
{"Name": "Harbor Blend whole bean coffee", "Price": Decimal("18.40"), "Quantity": 5},
{"Name": "Aged harbour cheddar", "Price": Decimal("16.50"), "Quantity": 20},
{"Name": "Cold-pressed olive oil", "Price": Decimal("12.90"), "Quantity": 15},
]
assembler = DocumentAssembler()
assembler.assemble_document("stock-totals-table.docx", "stock-totals-table-report.docx",
DataSourceInfo(products, "products"))
print("Table with totals generated successfully.")
The following sample file is used in this example: stock-totals-table.docx
Binary file (DOCX, 8 KB)
Template structure:
| Product Name | Price | Quantity |
|---|---|---|
<<foreach [in products]>><<[Name]>> | <<[Price]:"C">> | <<[Quantity]>><</foreach>> |
| Total | <<[products.Sum(p => p.Price)]:"C">> | <<[products.Sum(p => p.Quantity)]>> |
Tables work similarly in spreadsheet templates:
from decimal import Decimal
from groupdocs.assembly import DataSourceInfo, DocumentAssembler
products = [
{"Name": "Harbor Blend whole bean coffee", "Price": Decimal("18.40"), "Quantity": 5},
{"Name": "Aged harbour cheddar", "Price": Decimal("16.50"), "Quantity": 20},
]
assembler = DocumentAssembler()
assembler.assemble_document("stock-table.xlsx", "stock-table-report.xlsx",
DataSourceInfo(products, "products"))
print("Table generated in spreadsheet successfully.")
The following sample file is used in this example: stock-table.xlsx
Binary file (XLSX, 7 KB)
The worksheet holds the same header row and data band row as the Word table, in plain cells.
WarningIn spreadsheet templates, the data band is a range of cells: the band opens in the first cell of the row and closes in its last cell, and the row is repeated for each item. The cells need no Excel table around them.
To learn more about filtering, sorting, grouping table data, conditional table rows, and advanced table formatting, 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.