Generate Numbered Lists from Data Source

Overview

Numbered lists provide a sequential, ordered way to display collections of items in documents. GroupDocs.Assembly for Python via .NET enables you to generate numbered lists by iterating over data collections using foreach data bands within numbered list structures.

The main classes involved are:

Here are the steps to generate numbered lists:

  • Create a numbered list in your template document
  • Add a foreach data band within the list to iterate over your collection
  • Reference collection item properties in list items
  • Optionally use NumberOf() to display item numbers
  • Close the foreach data band
  • Assemble the document with your collection data
Note
The foreach data band automatically repeats numbered list items for each element in the collection, maintaining proper numbering sequence.

Generate Numbered List from a Python List

Create a numbered list from a list of dicts:

from groupdocs.assembly import DataSourceInfo, DocumentAssembler

# Create sample data
tasks = [
    {"Title": "Receive", "Description": "Check the morning delivery against the purchase order"},
    {"Title": "Pack", "Description": "Pack the orders for Quayside Bistro and Saltmarsh Deli"},
    {"Title": "Ship", "Description": "Load the van for the Port Ellery round"},
]

# Assemble document
assembler = DocumentAssembler()
assembler.assemble_document("numbered-tasks.docx", "numbered-tasks-report.docx",
                            DataSourceInfo(tasks, "tasks"))

print("Numbered list generated successfully.")

The following sample file is used in this example: numbered-tasks.docx

Binary file (DOCX, 8 KB)

Download full output

Template Structure for Numbered Lists

In your template document (numbered-tasks.docx), create a numbered list. The data band opens inside the first numbered paragraph and closes at the start of the next one, so each task repeats the whole list paragraph:

Tasks to complete:

1. <<foreach [in tasks]>><<[Title]>> - <<[Description]>>
<</foreach>>

After assembly, you’ll get:

Tasks to complete:

1. Receive - Check the morning delivery against the purchase order
2. Pack - Pack the orders for Quayside Bistro and Saltmarsh Deli
3. Ship - Load the van for the Port Ellery round

Using NumberOf() Function

You can explicitly display item numbers using the NumberOf() function:

from groupdocs.assembly import DataSourceInfo, DocumentAssembler

items = [
    "Harbor Blend whole bean coffee",
    "Lighthouse Espresso beans",
    "Chestnut honey",
]

assembler = DocumentAssembler()
assembler.assemble_document("numbered-items.docx", "numbered-items-report.docx",
                            DataSourceInfo(items, "items"))

print("Numbered list with explicit numbers generated successfully.")

The following sample file is used in this example: numbered-items.docx

Binary file (DOCX, 7 KB)

Download full output

Template with NumberOf():

Items:
<<foreach [item in items]>><<[item.NumberOf()]>>. <<[item]>>
<</foreach>>

The items are strings, so the data band names its iteration variable and calls NumberOf() on it. When the items are dicts, <<[NumberOf()]>> alone works inside the band, as the spreadsheet and email examples below show.

Generate Numbered List from a CSV File

ADO.NET’s DataTable has no Python counterpart. Tabular data kept in a CSV file can be passed as it is with CsvDataSource: each line is an item and the header names its members.

from groupdocs.assembly import DataSourceInfo, DocumentAssembler
from groupdocs.assembly.data import CsvDataLoadOptions, CsvDataSource

# The first line of the file holds the column names
tasks = CsvDataSource("delivery-tasks.csv", CsvDataLoadOptions(True))

assembler = DocumentAssembler()
assembler.assemble_document("numbered-tasks.docx", "numbered-tasks-from-csv.docx",
                            DataSourceInfo(tasks, "tasks"))

print("Numbered list generated from CSV file successfully.")

The following sample file is used in this example: numbered-tasks.docx

The following sample file is used in this example: delivery-tasks.csv

Binary file (DOCX, 8 KB)

Download full output

Numbered List in Spreadsheet

For spreadsheet templates, you can create numbered lists in cells:

from groupdocs.assembly import DataSourceInfo, DocumentAssembler

tasks = [
    {"Title": "Receive", "Description": "Check the morning delivery against the purchase order"},
    {"Title": "Pack", "Description": "Pack the orders for Quayside Bistro and Saltmarsh Deli"},
]

assembler = DocumentAssembler()
assembler.assemble_document("numbered-tasks.xlsx", "numbered-tasks-report.xlsx",
                            DataSourceInfo(tasks, "tasks"))

print("Numbered list generated in spreadsheet successfully.")

The following sample file is used in this example: numbered-tasks.xlsx

Binary file (XLSX, 7 KB)

Download full output

Template for spreadsheet (in-cell, the whole band in cell A1):

Tasks to complete:<<foreach [in tasks]>> <<[NumberOf()]>>. <<[Title]>>;<</foreach>>

Numbered List in Email

Generate numbered lists in email messages. This template is an EML file whose plain-text body holds the data band:

from groupdocs.assembly import DataSourceInfo, DocumentAssembler

tasks = [
    {"Title": "Receive", "Description": "Check the morning delivery against the purchase order"},
    {"Title": "Pack", "Description": "Pack the orders for Quayside Bistro and Saltmarsh Deli"},
]

assembler = DocumentAssembler()
assembler.assemble_document("numbered-tasks.eml", "numbered-tasks-report.eml",
                            DataSourceInfo(tasks, "tasks"))

print("Numbered list generated in email successfully.")

The following sample file is used in this example: numbered-tasks.eml

Binary file (EML, 875 bytes)

Download full output

The message body:

Tasks to complete:

<<foreach [in tasks]>><<[NumberOf()]>>. <<[Title]>> - <<[Description]>>
<</foreach>>
Harbor Lane Provisions
Warning
In Word documents, ensure the numbered list formatting is applied in the template. The foreach data band will repeat the list item format and maintain proper numbering for each collection element.

Advanced Usage Topics

To learn more about custom numbering formats, nested numbered lists, and advanced list formatting options, 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.