MCPcopy Index your code
hub / github.com/python-openxml/python-docx / get_or_add_image_part

Method get_or_add_image_part

src/docx/package.py:68–78  ·  view source on GitHub ↗

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.

(self, image_descriptor: str | IO[bytes])

Source from the content-addressed store, hash-verified

66 self._image_parts.append(item)
67
68 def get_or_add_image_part(self, image_descriptor: str | IO[bytes]) -> ImagePart:
69 """Return |ImagePart| object containing image identified by `image_descriptor`.
70
71 The image-part is newly created if a matching one is not present in the
72 collection.
73 """
74 image = Image.from_file(image_descriptor)
75 matching_image_part = self._get_by_sha1(image.sha1)
76 if matching_image_part is not None:
77 return matching_image_part
78 return self._add_image_part(image)
79
80 def _add_image_part(self, image: Image):
81 """Return |ImagePart| instance newly created from `image` and appended to the collection."""

Calls 3

_get_by_sha1Method · 0.95
_add_image_partMethod · 0.95
from_fileMethod · 0.45