-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add full alt-text (title and description) support for pictures and inline shapes #1530
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
...ingProps Expose the `<wp:docPr>` attributes used for image title and alt text in Word. These correspond to the "Alt Text (Title)" and "Alt Text (Description)" fields shown in the Word UI. Based on the approach from PR python-openxml#227 ("Add support for image title and alt text"), limited here to adding OptionalAttribute definitions in the OXML layer.
- Extend StoryPart.new_pic_inline() with optional `title` and `descr` parameters. - Pass these through to CT_Inline.new_pic_inline() for setting <wp:docPr> attributes. - Update Run.add_picture() to accept and forward `title` and `descr` to StoryPart. Part of the effort to enable setting image alternative text (accessibility title/description) when adding pictures.
Expose read/write access to image alternative text stored in <wp:docPr> via new `InlineShape.alt_text` (description) and `InlineShape.alt_title` (title) properties.
EwoutH
commented
Nov 10, 2025
While adding alt-text, I ran into an API/compatibility issue.
I’d like to let users set alt text at insertion time via:
Document.add_picture(...)Run.add_picture(...)
These are public APIs. Internally they call:
StoryPart.new_pic_inline(...)CT_Inline.new_pic_inline(...)
However, the existing API and tests assume a 3-arg positional call chain:
document.add_picture(path, width, height) run.add_picture(path, width, height) part.new_pic_inline(image, width, height)
If we extend this with positional title / descr, we either:
- break those expectations and risk user code,
- or start passing extra
Nonearguments around, which is brittle and obscures intent.
I think there are three options:
-
Keep
add_pictureas-is; only add:InlineShape.alt_textInlineShape.alt_title
Users set alt text after insertion:
shape = doc.add_picture("img.png") shape.alt_text = "..."
-
Extend via keyword-only args (backwards compatible):
add_picture(..., *, title=None, descr=None)- Plumb these through internally as keyword args only.
-
Internal-only clean break (future-proof core, public stays stable)
- Keep public APIs as-is (users can optionally pass
title/descras kwargs). - Make internal constructors keyword-only and clearer:
StoryPart.new_inline_picture(..., *, width=None, height=None, title=None, descr=None)
(optionally keepnew_pic_inline(...)as a shim that callsnew_inline_picture(...)and deprecate it)CT_Inline.new_pic_inline(..., *, title=None, descr=None)andCT_Inline.new(..., *, title=None, descr=None)
Run.add_picture(...)forwards only provided kwargs tonew_inline_picture(...).- This gives us a clean, extensible internal surface (ready for future options like wrapping/hyperlink) without changing public behavior. Guidance requested on whether to include the shim + deprecation, or go straight to the new names.
- Keep public APIs as-is (users can optionally pass
@scanny Which option would you prefer?
Uh oh!
There was an error while loading. Please reload this page.
Builds on #227 and #317.
Summary
This PR introduces first-class support for Word Alt Text in
python-docx. Users can now set and read both the Alt Text Title and Alt Text Description (as shown in Word’s Accessibility pane) directly through the API when inserting or editing pictures.Motive
Previously,
python-docxdid not expose a clean API for working with image alternative text. Users had to manually manipulate XML to set or retrieve<wp:docPr>attributes (title,descr). This enhancement improves accessibility workflows and enables automated tools (e.g., alt-text generators) to embed compliant descriptions directly into Word documents.Implementation
CT_NonVisualDrawingProps(oxml/shape.py): Added optional attributestitleanddescr.CT_Inline.new()/new_pic_inline(): Accept and applytitle/descrto<wp:docPr>.StoryPart.new_pic_inline(): Addedtitleanddescrparameters and forwarded them toCT_Inline.new_pic_inline().Run.add_picture()andDocument.add_picture(): Updated to accept and pass throughtitleanddescrfor inline pictures.InlineShape(shape.py): Added newalt_textandalt_titleproperties for convenient read/write access todocPr.descranddocPr.title.Usage Examples
Additional Notes
alt_titleand alt_text persisted correctly.titleanddescrare optional.