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.
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:
fromgroupdocs.assemblyimportDataSourceInfo,DocumentAssembler# Create sample datatasks=[{"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 documentassembler=DocumentAssembler()assembler.assemble_document("numbered-tasks.docx","numbered-tasks-report.docx",DataSourceInfo(tasks,"tasks"))print("Numbered list generated successfully.")
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:
fromgroupdocs.assemblyimportDataSourceInfo,DocumentAssembleritems=["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.")
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.
fromgroupdocs.assemblyimportDataSourceInfo,DocumentAssemblerfromgroupdocs.assembly.dataimportCsvDataLoadOptions,CsvDataSource# The first line of the file holds the column namestasks=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.")
For spreadsheet templates, you can create numbered lists in cells:
fromgroupdocs.assemblyimportDataSourceInfo,DocumentAssemblertasks=[{"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.")
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:
fromgroupdocs.assemblyimportDataSourceInfo,DocumentAssemblertasks=[{"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.")
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: