Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ Here's an example of what |docx| can do:
'first item in ordered list', style='List Number'
)

document.add_picture('monty-truth.png', width=Inches(1.25))
try:
document.add_picture('monty-truth.png', width=Inches(1.25))
except FileNotFoundError:
# the image is optional; the example works without it
pass

records = (
(3, '101', 'Spam'),
Expand Down
4 changes: 4 additions & 0 deletions src/docx/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ def _get_by_sha1(self, sha1: str) -> ImagePart | None:
"""Return the image part in this collection having a SHA1 hash matching `sha1`,
or |None| if not found."""
for image_part in self._image_parts:
# ---skip unknown/unsupported image types, like SVG, which are
# ---stored as generic Parts without a sha1 attribute---
if not hasattr(image_part, "sha1"):
continue
if image_part.sha1 == sha1:
return image_part
return None
Expand Down