DocumentPart
paper-docx 0.2.0 API reference
Main document part of a WordprocessingML (WML) package, aka a .docx file.
Acts as broker to other parts such as image, core properties, and style parts. It
also acts as a convenient delegate when a mid-document object needs a service
involving a remote ancestor. The Parented.part property inherited by many content
objects provides access to this part object for that purpose.
Attributes
blob
attributeblobcomments
attributecommentsCommentsComments object providing access to the comments added to this document.
content_type
attributecontent_typeContent type of this part.
core_properties
attributecore_propertiesCorePropertiesA CoreProperties object providing read/write access to the core properties
of this document.
document
attributedocumentA Document object providing access to the content of this document.
element
attributeelementThe root XML element of this XML part.
inline_shapes
attributeinline_shapesThe InlineShapes instance containing the inline shapes in the document.
next_id
attributenext_idintNext available positive integer id value in this story XML document.
The value is determined by incrementing the maximum existing id value. Gaps in the existing id sequence are not filled. The id attribute value is unique in the document, without regard to the element type it appears on.
numbering_part
attributenumbering_partNumberingPartA NumberingPart object providing access to the numbering definitions for this document.
Creates an empty numbering part if one is not present.
package
attributepackageOpcPackage instance this part belongs to.
part
attributepartPart of the parent protocol, "children" of the document will not know the part that contains them so must ask their parent object.
That chain of delegation ends here for child objects.
partname
attributepartnamePackURI instance holding partname of this part, e.g.
'/ppt/slides/slide1.xml'.
related_parts
attributerelated_partsDictionary 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.
rels
attributerelsRelationships instance holding the relationships for this part.
settings
attributesettingsSettingsA Settings object providing access to the settings in the settings part of
this document.
styles
attributestylesA Styles object providing access to the styles in the styles part of this
document.
Functions
add_footer_part
funcadd_footer_part()Return (footer_part, rId) pair for newly-created footer part.
add_header_part
funcadd_header_part()Return (header_part, rId) pair for newly-created header part.
after_unmarshal
funcafter_unmarshal()Entry point for post-unmarshaling processing, for example to parse the part XML.
May be overridden by subclasses without forwarding call to super.
before_marshal
funcbefore_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.
drop_header_part
funcdrop_header_part(rId: str) -> NoneRemove related header part identified by rId.
paramrIdstrReturns
Nonedrop_rel
funcdrop_rel(rId: str)Remove the relationship identified by rId if its reference count is less
than 2.
Relationships with a reference count of 0 are implicit relationships.
paramrIdstrfooter_part
funcfooter_part(rId: str)Return FooterPart related by rId.
paramrIdstrget_or_add_image
funcget_or_add_image(image_descriptor: str | IO[bytes]) -> Tuple[str, Image]Return (rId, image) pair for image identified by image_descriptor.
rId is the str key (often like "rId7") for the relationship between this story
part and the image part, reused if already present, newly created if not.
image is an Image instance providing access to the properties of the image,
such as dimensions and image type.
paramimage_descriptorstr | IO[bytes]Returns
typing.Tuple[str, docx.image.image.Image]get_style
funcget_style(style_id: str | None, style_type: WD_STYLE_TYPE) -> BaseStyleReturn the style in this document matching style_id.
Returns the default style for style_type if style_id is None or does not
match a defined style of style_type.
paramstyle_idstr | Noneparamstyle_typeWD_STYLE_TYPEReturns
docx.styles.style.BaseStyleget_style_id
funcget_style_id(style_or_name, style_type)Return the style_id (str) of the style of style_type matching
style_or_name.
Returns None if the style resolves to the default style for style_type or if
style_or_name is itself None. Raises if style_or_name is a style of the
wrong type or names a style not present in the document.
paramstyle_or_nameparamstyle_typeheader_part
funcheader_part(rId: str)Return HeaderPart related by rId.
paramrIdstrload
funcload(partname: PackURI, content_type: str, blob: bytes, package: Package)parampartnamePackURIparamcontent_typestrparamblobbytesparampackagePackageload_rel
funcload_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.
paramreltypestrparamtargetPart | strparamrIdstrparamis_externalbool= Falsenew_pic_inline
funcnew_pic_inline(image_descriptor: str | IO[bytes], width: int | Length | None = None, height: int | Length | None = None) -> CT_InlineReturn a newly-created w:inline element.
The element contains the image specified by image_descriptor and is scaled
based on the values of width and height.
paramimage_descriptorstr | IO[bytes]paramwidthint | Length | None= Noneparamheightint | Length | None= NoneReturns
docx.oxml.shape.CT_Inlinepart_related_by
funcpart_related_by(reltype: str) -> PartReturn 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.
paramreltypestrReturns
docx.opc.part.Partrelate_to
funcrelate_to(target: Part | str, reltype: str, is_external: bool = False) -> strReturn 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.
paramtargetPart | strparamreltypestrparamis_externalbool= FalseReturns
strsave
funcsave(path_or_stream: str | IO[bytes])Save this document to path_or_stream, which can be either a path to a
filesystem location (a string) or a file-like object.
parampath_or_streamstr | IO[bytes]target_ref
functarget_ref(rId: str) -> strReturn URL contained in target ref of relationship identified by rId.
paramrIdstrReturns
str