Skip to content

package

package

WordprocessingML Package class and related objects.

Package

Bases: OpcPackage

Customizations specific to a WordprocessingML package.

after_unmarshal

after_unmarshal()

Called by loading code after all parts and relationships have been loaded.

This method affords the opportunity for any required post-processing.

Source code in src/docx/package.py
def after_unmarshal(self):
    """Called by loading code after all parts and relationships have been loaded.

    This method affords the opportunity for any required post-processing.
    """
    self._gather_image_parts()

get_or_add_image_part

get_or_add_image_part(
    image_descriptor: str | PathLike[str] | IO[bytes],
) -> ImagePart

Return ImagePart containing image specified by image_descriptor.

The image-part is newly created if a matching one is not already present in the collection.

Source code in src/docx/package.py
def get_or_add_image_part(
    self, image_descriptor: str | os.PathLike[str] | IO[bytes]
) -> ImagePart:
    """Return |ImagePart| containing image specified by `image_descriptor`.

    The image-part is newly created if a matching one is not already present in the
    collection.
    """
    return self.image_parts.get_or_add_image_part(image_descriptor)

image_parts

image_parts() -> ImageParts

ImageParts collection object for this package.

Source code in src/docx/package.py
@lazyproperty
def image_parts(self) -> ImageParts:
    """|ImageParts| collection object for this package."""
    return ImageParts()

ImageParts

ImageParts()

Collection of ImagePart objects corresponding to images in the package.

Source code in src/docx/package.py
def __init__(self):
    self._image_parts: list[ImagePart] = []

get_or_add_image_part

get_or_add_image_part(
    image_descriptor: str | PathLike[str] | IO[bytes],
) -> ImagePart

Return ImagePart object containing image identified by image_descriptor.

The image-part is newly created if a matching one is not present in the collection.

Source code in src/docx/package.py
def get_or_add_image_part(
    self, image_descriptor: str | os.PathLike[str] | IO[bytes]
) -> ImagePart:
    """Return |ImagePart| object containing image identified by `image_descriptor`.

    The image-part is newly created if a matching one is not present in the
    collection.
    """
    image = Image.from_file(image_descriptor)
    matching_image_part = self._get_by_sha1(image.sha1)
    if matching_image_part is not None:
        return matching_image_part
    return self._add_image_part(image)