A document that contains a relationship whose target part is not present in the package cannot be opened at all — the failure happens during package load, before any application code can inspect or skip the bad relationship.
Reproduction
Take the bundled default template, add one relationship pointing at a file that does not exist, and open it:
# word/_rels/document.xml.rels gains:
# <Relationship Id="rIdBogus"
# Type=".../image" Target="media/missing.png"/>
Document("broken.docx")
# KeyError: "There is no item named 'word/media/missing.png' in the archive"
A raw KeyError from zipfile escapes the OPC layer, so a caller cannot even distinguish this from a library bug without matching on the message.
Why it matters
This is one of the most reported failure modes upstream and it is not a synthetic case. Documents lose parts through incremental save, third-party generators, DRM/repair tools, and mail gateways that strip embedded media. Word itself opens these files without complaint — it treats a relationship with no target as absent.
Scope
Notes
Upstream has three open issues and two unmerged PRs on this, the oldest from 2019:
Related but distinct: python-openxml#1566 skips non-image parts when matching by sha1 in ImageParts, which is a second way a broken image relationship surfaces. Worth handling in the same pass.
A document that contains a relationship whose target part is not present in the package cannot be opened at all — the failure happens during package load, before any application code can inspect or skip the bad relationship.
Reproduction
Take the bundled default template, add one relationship pointing at a file that does not exist, and open it:
A raw
KeyErrorfromzipfileescapes the OPC layer, so a caller cannot even distinguish this from a library bug without matching on the message.Why it matters
This is one of the most reported failure modes upstream and it is not a synthetic case. Documents lose parts through incremental save, third-party generators, DRM/repair tools, and mail gateways that strip embedded media. Word itself opens these files without complaint — it treats a relationship with no target as absent.
Scope
docx/opc/pkgreader.py, and drop it rather than raisingRelationshipscollection stays consistent —rIdsmust not be dangling for laterrelated_part()lookupsInlineShape— the shape element remains in the XML with an rId that no longer resolvesNotes
Upstream has three open issues and two unmerged PRs on this, the oldest from 2019:
fix: accommodate NULL relationship (by skipping)KeyErrorThe image part with relationship rID8 was not foundRelated but distinct: python-openxml#1566 skips non-image parts when matching by sha1 in
ImageParts, which is a second way a broken image relationship surfaces. Worth handling in the same pass.