CustomPartCollection class

CustomPartCollection class

Represents a collection of CustomPart objects. To learn more, visit the Structured Document Tags or Content Control documentation article.

Remarks

You do not normally need to create instances of this class. You access custom parts related to the OOXML package via the Document.package_custom_parts property.

Constructors

NameDescription
CustomPartCollection()The default constructor.

Indexers

NameDescription
__getitem__(index)Gets or sets an item at the specified index.

Properties

NameDescription
countGets the number of elements contained in the collection.

Methods

NameDescription
add(part)Adds an item to the collection.
clear()Removes all elements from the collection.
clone()Makes a deep copy of this collection and its items.
remove_at(index)Removes an item at the specified index.

Examples

Shows how to access a document’s arbitrary custom parts collection.

doc = aw.Document(MY_DIR + 'Custom parts OOXML package.docx')
self.assertEqual(2, doc.package_custom_parts.count)
# Clone the second part, then add the clone to the collection.
cloned_part = doc.package_custom_parts[1].clone()
doc.package_custom_parts.add(cloned_part)
self.assertEqual(3, doc.package_custom_parts.count)
# Enumerate over the collection and print every part.
for index, part in enumerate(doc.package_custom_parts):
    print(f'Part index {index}:')
    print(f'\tName:\t\t\t\t{part.name}')
    print(f'\tContent type:\t\t{part.content_type}')
    print(f'\tRelationship type:\t{part.relationship_type}')
    if part.is_external:
        print('\tSourced from outside the document')
    else:
        print(f'\tStored within the document, length: {len(part.data)} bytes')
# We can remove elements from this collection individually, or all at once.
doc.package_custom_parts.remove_at(2)
self.assertEqual(2, doc.package_custom_parts.count)
doc.package_custom_parts.clear()
self.assertEqual(0, doc.package_custom_parts.count)

See Also