Add full alt-text (title and description) support for pictures and inline shapes #1530
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.