Skip to content
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

Feat: Add Alt text properties and setters #911

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
12 changes: 12 additions & 0 deletions pptx/oxml/shapes/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,17 @@ def shape_name(self):
"""
return self._nvXxPr.cNvPr.name

@property
def shape_alt_text(self):
"""
Alt text of this shape
"""
return self._nvXxPr.cNvPr.descr

@shape_alt_text.setter
def shape_alt_text(self, value):
self._nvXxPr.cNvPr.descr = value

@property
def txBody(self):
"""
Expand Down Expand Up @@ -304,6 +315,7 @@ class CT_NonVisualDrawingProps(BaseOxmlElement):
hlinkHover = ZeroOrOne("a:hlinkHover", successors=_tag_seq[2:])
id = RequiredAttribute("id", ST_DrawingElementId)
name = RequiredAttribute("name", XsdString)
descr = OptionalAttribute('descr', XsdString)
del _tag_seq


Expand Down
11 changes: 11 additions & 0 deletions pptx/shapes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,17 @@ def name(self):
def name(self, value):
self._element._nvXxPr.cNvPr.name = value

@property
def alt_text(self):
"""
Alternative text for the shape
"""
return self._element.shape_alt_text

@alt_text.setter
def alt_text(self, value):
self._element.shape_alt_text = value

@property
def part(self):
"""The package part containing this shape.
Expand Down
32 changes: 32 additions & 0 deletions tests/shapes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ def it_can_change_its_name(self, name_set_fixture):
shape.name = new_value
assert shape._element.xml == expected_xml

def it_knows_its_alt_text(self, alt_text_fixture):
shape, expected_value = alt_text_fixture
assert shape.alt_text == expected_value

def it_can_change_its_alt_text(self, alt_text_set_fixture):
shape, new_value, expected_xml = alt_text_set_fixture
shape.alt_text = new_value
assert shape._element.xml == expected_xml

def it_has_a_position(self, position_get_fixture):
shape, expected_left, expected_top = position_get_fixture
assert shape.left == expected_left
Expand Down Expand Up @@ -255,6 +264,29 @@ def name_set_fixture(self, request):
expected_xml = xml(expected_xSp_cxml)
return shape, new_value, expected_xml

@pytest.fixture
def alt_text_fixture(self):
shape_elm = element("p:sp/p:nvSpPr/p:cNvPr{id=1,name=foo,descr=foo}")
shape = BaseShape(shape_elm, None)
return shape, 'foo'

@pytest.fixture(
params=[
(
"p:sp/p:nvSpPr/p:cNvPr{id=1,name=foo,descr=foo}",
Shape,
"bar",
"p:sp/p:nvSpPr/p:cNvPr{id=1,name=foo,descr=bar}",
)
]
)
def alt_text_set_fixture(self, request):
xSp_cxml, ShapeCls, new_value, expected_xSp_cxml = request.param
shape = ShapeCls(element(xSp_cxml), None)
expected_xml = xml(expected_xSp_cxml)
expected_xml = xml(expected_xSp_cxml)
return shape, new_value, expected_xml

@pytest.fixture
def part_fixture(self, shapes_):
parent_ = shapes_
Expand Down