CustomPart class

CustomPart class

Represents a custom (arbitrary content) part, that is not defined by the ISO/IEC 29500 standard. To learn more, visit the Structured Document Tags or Content Control documentation article.

Remarks

This class represents an OOXML part that is a target of an “unknown relationship”. All relationships not defined within ISO/IEC 29500 are considered “unknown relationships”. Unknown relationships are permitted within an Office Open XML document provided that they conform to relationship markup guidelines.

Microsoft Word preserves custom parts during open/save cycles. Some additional info can be found here http://blogs.msdn.com/dmahugh/archive/2006/11/25/arbitrary-content-in-an-opc-package.aspx

Aspose.Words also roundtrips custom parts and in addition, allows to programmatically access such parts via the CustomPart and CustomPartCollection objects.

Do not confuse custom parts with Custom XML Data. Use CustomXmlPart if you need to access Custom XML Data.

Constructors

NameDescription
CustomPart()The default constructor.

Properties

NameDescription
content_typeSpecifies the content type of this custom part.
dataContains the data of this custom part.
is_externalFalse if this custom part is stored inside the OOXML package. True if this custom part is an external target.
nameGets or sets this part’s absolute name within the OOXML package or the target URL.
relationship_typeGets or sets the relationship type from the parent part to this custom part.

Methods

NameDescription
clone()Makes a “deep enough” copy of the object. Does not duplicate the bytes of the CustomPart.data value.

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