part
part
¶
Open Packaging Convention (OPC) objects related to package parts.
Part
¶
Part(
partname: PackURI,
content_type: str,
blob: bytes | None = None,
package: Package | None = None,
)
Base class for package parts.
Provides common properties and methods, but intended to be subclassed in client code to implement specific part behaviors.
Source code in src/docx/opc/part.py
blob
property
¶
Contents of this package part as a sequence of bytes.
May be text or binary. Intended to be overridden by subclasses. Default behavior is to return load blob.
content_type
property
writable
¶
Content type of this part.
Writable, because a part can legitimately change what it is without its bytes changing — a Word document and a Word template hold identical markup and differ only here.
partname
property
writable
¶
PackURI instance holding partname of this part, e.g. '/ppt/slides/slide1.xml'.
related_parts
property
¶
Dictionary mapping related parts by rId, so child objects can resolve
explicit relationships present in the part XML, e.g. sldIdLst to a specific
Slide instance.
after_unmarshal
¶
Entry point for post-unmarshaling processing, for example to parse the part XML.
May be overridden by subclasses without forwarding call to super.
Source code in src/docx/opc/part.py
before_marshal
¶
Entry point for pre-serialization processing, for example to finalize part naming if necessary.
May be overridden by subclasses without forwarding call to super.
Source code in src/docx/opc/part.py
drop_rel
¶
Remove the relationship identified by rId if its reference count is less
than 2.
Relationships with a reference count of 0 are implicit relationships.
Source code in src/docx/opc/part.py
load_rel
¶
load_rel(
reltype: str,
target: Part | str,
rId: str,
is_external: bool = False,
)
Return newly added _Relationship instance of reltype.
The new relationship relates the target part to this part with key rId.
Target mode is set to RTM.EXTERNAL if is_external is True. Intended for
use during load from a serialized package, where the rId is well-known. Other
methods exist for adding a new relationship to a part when manipulating a part.
Source code in src/docx/opc/part.py
part_related_by
¶
part_related_by(reltype: str) -> Part
Return part to which this part has a relationship of reltype.
Raises KeyError if no such relationship is found and ValueError if more than
one such relationship is found. Provides ability to resolve implicitly related
part, such as Slide -> SlideLayout.
Source code in src/docx/opc/part.py
relate_to
¶
relate_to(
target: Part | str,
reltype: str,
is_external: bool = False,
) -> str
Return rId key of relationship of reltype to target.
The returned rId is from an existing relationship if there is one, otherwise a
new relationship is created.
Source code in src/docx/opc/part.py
rels
¶
Relationships instance holding the relationships for this part.
Source code in src/docx/opc/part.py
target_ref
¶
PartFactory
¶
Provides a way for client code to specify a subclass of Part to be constructed by Unmarshaller based on its content type and/or a custom callable.
Setting PartFactory.part_class_selector to a callable object will cause that
object to be called with the parameters content_type, reltype, once for each
part in the package. If the callable returns an object, it is used as the class for
that part. If it returns None, part class selection falls back to the content type
map defined in PartFactory.part_type_for. If no class is returned from either of
these, the class contained in PartFactory.default_part_type is used to construct
the part, which is by default opc.package.Part.
XmlPart
¶
XmlPart(
partname: PackURI,
content_type: str,
element: BaseOxmlElement,
package: Package,
)