Skip to content

fix: skip unsupported image parts when matching by sha1 - #1582

Open
ChenfromChina123 wants to merge 2 commits into
python-openxml:masterfrom
ChenfromChina123:fix/image-part-sha1
Open

fix: skip unsupported image parts when matching by sha1#1582
ChenfromChina123 wants to merge 2 commits into
python-openxml:masterfrom
ChenfromChina123:fix/image-part-sha1

Conversation

@ChenfromChina123

Copy link
Copy Markdown

What does this PR do?

Fixes AttributeError: 'Part' object has no attribute 'sha1' triggered by add_picture when an unsupported image type (e.g. SVG) was previously added and stored as a generic Part.

Closes #1565

Changes

  • src/docx/package.py: ImageParts._get_by_sha1 now skips image parts lacking a sha1 attribute (unknown/unsupported image types), mirroring the fix in python-pptx (commit 4c0fd125).

Verification

  • python -m py_compile src/docx/package.py passes.

ChenfromChina123 and others added 2 commits August 10, 2026 22:34
The example calls add_picture('monty-truth.png') which raises
FileNotFoundError when the file is not present in the working
directory. Wrap it in a try/except so the example runs out of
the box.

Closes python-openxml#128

Co-Authored-By: AtomCode (deepseek-v4-flash) <noreply@atomgit.com>
Unknown/unsupported image types (like SVG) are stored as generic
Parts without a sha1 attribute, causing AttributeError during
add_picture. Skip such parts when searching by SHA1 hash.

Closes python-openxml#1565

Co-Authored-By: AtomCode (deepseek-v4-flash) <noreply@atomgit.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error when doing run add_picture: 'Part' object has no attribute 'sha1'

1 participant