Files
docx/src/embedded-files
DolanandClaude Fable 5.1 19087e8b79 fix: name images in the package as Word does, image1.png and so on (#3872)
* fix: name images in the package as Word does, image1.png and so on

Images were written to word/media under a SHA-1 hash of their data,
such as 4bf7547c9322fd2143bd3e8dfa84109d9d3fed14.png. Word for the web
has shown an empty box in place of an image named like that, and shown
it once the file was renamed image1.png as Word names its own, as
https://github.com/dolanmiu/docx/issues/496 reports.

The hash is still the key an image is added to the media under, so an
image used twice is stored once and its placeholders are unchanged, but
the file is written as image1.png, image2.jpg and so on, numbered in the
order images are added. The numbers come from the document's
PackageParts, which an ImagePatch's parts already take theirs from, so
an image never takes a part's name, nor a part an image's, and in
patchDocument new images are numbered after the template's own.

Closes #496
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QzDw6ZwCkrdX4ZsWzeVQNC

* docs: say images are numbered by type, image1.png and image1.jpg

A document's images take their names from PackageParts.takePath,
which numbers by the whole file name, so a PNG and a JPEG are
image1.png and image1.jpg, not image2.jpg as the docs and the Media
comment said. Word for the web shows images named this way, as the
images demo showed, and an ImagePatch's images were already numbered
so. The docs now say that, and a Media without a path taker numbers
its images by type too, so it names them as a document does.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QzDw6ZwCkrdX4ZsWzeVQNC

* test: a document numbers its images by type, in the order they are added

Covers a PNG, a JPEG and a second PNG written through File, which
takePath names image1.png, image1.jpg and image2.png.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QzDw6ZwCkrdX4ZsWzeVQNC

* test: check the relationships to a document's images, numbered by type

The mixed-extension test checked the media files and their bytes, but
not the relationships that point the drawings at them, which is the
XML the names matter in.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QzDw6ZwCkrdX4ZsWzeVQNC

* test: check each of a document's images is written under its own number

Swapping the two PNGs' bytes passed the mixed-extension test, though
its name says images are numbered in the order they are added.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QzDw6ZwCkrdX4ZsWzeVQNC

---------

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-31 14:10:01 +00:00
..