Generate Tables from Collection Data

Overview

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:

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
Note
Table-row data bands automatically repeat the table row for each item in the collection, creating a complete table with all data rows.

Generate Table from a Python List

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)

Download full output

Template Structure for Tables

In your template document (stock-table.docx), create a table with this structure:

Product NamePriceQuantity
<<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 NamePriceQuantity
Harbor Blend whole bean coffee18.405
Aged harbour cheddar16.5020
Cold-pressed olive oil12.9015

Generate Table with Formatting

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)

Download full output

Template with formatting:

Product NamePriceQuantity
<<foreach [in products]>><<[Name]>><<[Price]:"C">><<[Quantity]>><</foreach>>

The :"C" format string formats the price as currency.

Generate Table from a JSON File

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)

Download full output

Generate Table with Totals

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)

Download full output

Template structure:

Product NamePriceQuantity
<<foreach [in products]>><<[Name]>><<[Price]:"C">><<[Quantity]>><</foreach>>
Total<<[products.Sum(p => p.Price)]:"C">><<[products.Sum(p => p.Quantity)]>>

Generate Table in Spreadsheet

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)

Download full output

The worksheet holds the same header row and data band row as the Word table, in plain cells.

Warning
In 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.

Advanced Usage Topics

To learn more about filtering, sorting, grouping table data, conditional table rows, and advanced table formatting, 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:

Free Online Apps

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.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.