ListCollection class

ListCollection class

Stores and manages formatting of bulleted and numbered lists used in a document. To learn more, visit the Working with Lists documentation article.

Remarks

A list in a Microsoft Word document is a set of list formatting properties. The formatting of the lists is stored in the ListCollection collection separately from the paragraphs of text.

You do not create objects of this class. There is always only one ListCollection object per document and it is accessible via the DocumentBase.lists property.

To create a new list based on a predefined list template or based on a list style, use the ListCollection.add() method.

To create a new list with formatting identical to an existing list, use the ListCollection.add_copy() method.

To make a paragraph bulleted or numbered, you need to apply list formatting to a paragraph by assigning a List object to the ListFormat.list property of ListFormat.

To remove list formatting from a paragraph, use the ListFormat.remove_numbers() method.

If you know a bit about WordprocessingML, then you might know it defines separate concepts for “list” and “list definition”. This exactly corresponds to how list formatting is stored in a Microsoft Word document at the low level. List definition is like a “schema” and list is like an instance of a list definition.

To simplify programming model, Aspose.Words hides the distinction between list and list definition in much the same way like Microsoft Word hides this in its user interface. This allows you to concentrate more on how you want your document to look like, rather than building low-level objects to satisfy requirements of the Microsoft Word file format.

It is not possible to delete lists once they are created in the current version of Aspose.Words. This is similar to Microsoft Word where user does not have explicit control over list definitions.

Indexers

NameDescription
__getitem__(index)Gets a list by index.

Properties

NameDescription
countGets the count of numbered and bulleted lists in the document.
documentGets the owner document.

Methods

NameDescription
add(list_template)Creates a new list based on a predefined template and adds it to the collection of lists in the document.
add(list_style)Creates a new list that references a list style and adds it to the collection of lists in the document.
add_copy(src_list)Creates a new list by copying the specified list and adding it to the collection of lists in the document.
get_list_by_list_id(list_id)Gets a list by a list identifier.

Examples

Shows how to work with list levels.

doc = aw.Document()
builder = aw.DocumentBuilder(doc=doc)
self.assertFalse(builder.list_format.is_list_item)
# A list allows us to organize and decorate sets of paragraphs with prefix symbols and indents.
# We can create nested lists by increasing the indent level.
# We can begin and end a list by using a document builder's "ListFormat" property.
# Each paragraph that we add between a list's start and the end will become an item in the list.
# Below are two types of lists that we can create using a document builder.
# 1 -  A numbered list:
# Numbered lists create a logical order for their paragraphs by numbering each item.
builder.list_format.list = doc.lists.add(list_template=aw.lists.ListTemplate.NUMBER_DEFAULT)
self.assertTrue(builder.list_format.is_list_item)
# By setting the "ListLevelNumber" property, we can increase the list level
# to begin a self-contained sub-list at the current list item.
# The Microsoft Word list template called "NumberDefault" uses numbers to create list levels for the first list level.
# Deeper list levels use letters and lowercase Roman numerals.
i = 0
while i < 9:
    builder.list_format.list_level_number = i
    builder.writeln('Level ' + str(i))
    i += 1
# 2 -  A bulleted list:
# This list will apply an indent and a bullet symbol ("•") before each paragraph.
# Deeper levels of this list will use different symbols, such as "■" and "○".
builder.list_format.list = doc.lists.add(list_template=aw.lists.ListTemplate.BULLET_DEFAULT)
i = 0
while i < 9:
    builder.list_format.list_level_number = i
    builder.writeln('Level ' + str(i))
    i += 1
# We can disable list formatting to not format any subsequent paragraphs as lists by un-setting the "List" flag.
builder.list_format.list = None
self.assertFalse(builder.list_format.is_list_item)
doc.save(file_name=ARTIFACTS_DIR + 'Lists.SpecifyListLevel.docx')

Shows how to restart numbering in a list by copying a list.

doc = aw.Document()
# A list allows us to organize and decorate sets of paragraphs with prefix symbols and indents.
# We can create nested lists by increasing the indent level.
# We can begin and end a list by using a document builder's "ListFormat" property.
# Each paragraph that we add between a list's start and the end will become an item in the list.
# Create a list from a Microsoft Word template, and customize its first list level.
list1 = doc.lists.add(list_template=aw.lists.ListTemplate.NUMBER_ARABIC_PARENTHESIS)
list1.list_levels[0].font.color = aspose.pydrawing.Color.red
list1.list_levels[0].alignment = aw.lists.ListLevelAlignment.RIGHT
# Apply our list to some paragraphs.
builder = aw.DocumentBuilder(doc=doc)
builder.writeln('List 1 starts below:')
builder.list_format.list = list1
builder.writeln('Item 1')
builder.writeln('Item 2')
builder.list_format.remove_numbers()
# We can add a copy of an existing list to the document's list collection
# to create a similar list without making changes to the original.
list2 = doc.lists.add_copy(list1)
list2.list_levels[0].font.color = aspose.pydrawing.Color.blue
list2.list_levels[0].start_at = 10
# Apply the second list to new paragraphs.
builder.writeln('List 2 starts below:')
builder.list_format.list = list2
builder.writeln('Item 1')
builder.writeln('Item 2')
builder.list_format.remove_numbers()
doc.save(file_name=ARTIFACTS_DIR + 'Lists.RestartNumberingUsingListCopy.docx')

Shows how to create a document with a sample of all the lists from another document.

def print_out_all_lists():
    src_doc = aw.Document(MY_DIR + 'Rendering.docx')
    dst_doc = aw.Document()
    builder = aw.DocumentBuilder(dst_doc)
    for src_list in src_doc.lists:
        dst_list = dst_doc.lists.add_copy(src_list)
        add_list_sample(builder, dst_list)
    dst_doc.save(ARTIFACTS_DIR + 'Lists.print_out_all_lists.docx')

def add_list_sample(builder: aw.DocumentBuilder, list: aw.lists.List):
    builder.writeln(f'Sample formatting of list with list_id: {list.list_id}')
    builder.list_format.list = list
    for i in range(list.list_levels.count):
        builder.list_format.list_level_number = i
        builder.writeln(f'Level {i}')
    builder.list_format.remove_numbers()
    builder.writeln()

See Also