Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docx/oxml/shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def _inline_xml(cls):
return (
'<wp:inline %s>\n'
' <wp:extent cx="914400" cy="914400"/>\n'
' <wp:docPr id="666" name="unnamed"/>\n'
' <wp:docPr id="666" name="unnamed" descr=""/>\n'
' <wp:cNvGraphicFramePr>\n'
' <a:graphicFrameLocks noChangeAspect="1"/>\n'
' </wp:cNvGraphicFramePr>\n'
Expand Down
11 changes: 11 additions & 0 deletions docx/shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,14 @@ def width(self):
def width(self, cx):
self._inline.extent.cx = cx
self._inline.graphic.graphicData.pic.spPr.cx = cx

@property
def alt_text(self):
"""
Read/write. The alt-text associated with this shape.
"""
return self._inline.docPr.attrib["descr"]

@alt_text.setter
def alt_text(self, text):
self._inline.docPr.attrib["descr"] = text
2 changes: 1 addition & 1 deletion tests/test_files/snippets/inline.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<wp:inline xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
<wp:extent cx="444" cy="888"/>
<wp:docPr id="24" name="Picture 24"/>
<wp:docPr id="24" name="Picture 24" descr=""/>
<wp:cNvGraphicFramePr>
<a:graphicFrameLocks noChangeAspect="1"/>
</wp:cNvGraphicFramePr>
Expand Down
28 changes: 28 additions & 0 deletions tests/test_shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from docx.enum.shape import WD_INLINE_SHAPE
from docx.oxml.ns import nsmap
from docx.shape import InlineShape, InlineShapes
from docx.oxml.shape import CT_Inline
from docx.shared import Length

from .oxml.unitdata.dml import (
Expand Down Expand Up @@ -99,6 +100,17 @@ def it_can_change_its_display_dimensions(self, dimensions_set_fixture):
inline_shape.height = cy
assert inline_shape._inline.xml == expected_xml

def it_has_blank_alt_text(self, alt_text_blank_get_fixture):
inline_shape, expected_alt_text = alt_text_blank_get_fixture
alt_text = inline_shape.alt_text
assert alt_text == expected_alt_text

def it_can_change_its_alt_text(self, alt_text_set_fixture):
inline_shape, expected_alt_text = alt_text_set_fixture
inline_shape.alt_text = "new alt text"
alt_text = inline_shape._inline.docPr.attrib["descr"]
assert alt_text == expected_alt_text

# fixtures -------------------------------------------------------

@pytest.fixture
Expand All @@ -122,6 +134,22 @@ def dimensions_set_fixture(self):
expected_xml = xml(expected_cxml)
return inline_shape, new_cx, new_cy, expected_xml

@pytest.fixture
def alt_text_blank_get_fixture(self):
expected_alt_text = ""
image = "tests/test_files/monty-truth.png"
inline_shape = CT_Inline.new_pic_inline(0, "", image, 333, 666)
inline_shape = InlineShape(inline_shape)
return inline_shape, expected_alt_text

@pytest.fixture
def alt_text_set_fixture(self):
expected_alt_text = "new alt text"
image = "tests/test_files/monty-truth.png"
inline_shape = CT_Inline.new_pic_inline(0, "", image, 333, 666)
inline_shape = InlineShape(inline_shape)
return inline_shape, expected_alt_text

@pytest.fixture(params=[
'embed pic', 'link pic', 'link+embed pic', 'chart', 'smart art',
'not implemented'
Expand Down