From 9286c3cdc7d4f27a339e85480d3ecf06eb8eb9c9 Mon Sep 17 00:00:00 2001 From: pa-t Date: Wed, 13 Sep 2023 11:36:14 -0500 Subject: [PATCH 1/2] add alt text propterties and setters --- pptx/oxml/shapes/shared.py | 12 ++++++++++++ pptx/shapes/base.py | 11 +++++++++++ 2 files changed, 23 insertions(+) diff --git a/pptx/oxml/shapes/shared.py b/pptx/oxml/shapes/shared.py index 74eb562d3..7f98413dd 100644 --- a/pptx/oxml/shapes/shared.py +++ b/pptx/oxml/shapes/shared.py @@ -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): """ @@ -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 diff --git a/pptx/shapes/base.py b/pptx/shapes/base.py index c9472434d..711b7517b 100644 --- a/pptx/shapes/base.py +++ b/pptx/shapes/base.py @@ -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. From 9ee8d29f147a9677e5b4f233715251ad1ad1d609 Mon Sep 17 00:00:00 2001 From: pa-t Date: Wed, 13 Sep 2023 14:30:55 -0500 Subject: [PATCH 2/2] add test for get and set --- tests/shapes/test_base.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/shapes/test_base.py b/tests/shapes/test_base.py index 8182323de..d3ac92dd2 100644 --- a/tests/shapes/test_base.py +++ b/tests/shapes/test_base.py @@ -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 @@ -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_