From 95f28451b94d7586b8ba5385efbd63c138ac658b Mon Sep 17 00:00:00 2001 From: Gabriel Tessarolli Date: Sat, 21 Mar 2020 01:50:39 -0300 Subject: [PATCH] implementing text font strikethrough --- docs/user/text.rst | 1 + pptx/oxml/simpletypes.py | 12 ++++++++++++ pptx/oxml/text.py | 4 +++- pptx/text/text.py | 23 ++++++++++++++++++++++- 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/docs/user/text.rst b/docs/user/text.rst index 1f59c8fa3..660bc4b9b 100644 --- a/docs/user/text.rst +++ b/docs/user/text.rst @@ -168,6 +168,7 @@ the theme color Accent 1. font.bold = True font.italic = None # cause value to be inherited from theme font.color.theme_color = MSO_THEME_COLOR.ACCENT_1 + font.strikethrough = True If you prefer, you can set the font color to an absolute RGB value. Note that this will not change color when the theme is changed:: diff --git a/pptx/oxml/simpletypes.py b/pptx/oxml/simpletypes.py index d77d9a494..009d4be79 100644 --- a/pptx/oxml/simpletypes.py +++ b/pptx/oxml/simpletypes.py @@ -727,6 +727,18 @@ class ST_TextWrappingType(XsdTokenEnumeration): _members = (NONE, SQUARE) +class ST_TextFontStrike(XsdStringEnumeration): + """ + Valid values for attribute + """ + + NO_STRIKE = "noStrike" + SINGLE_STRIKE = "sngStrike" + DOUBLE_STRIKE = "dblStrike" + + _members = (NO_STRIKE, SINGLE_STRIKE, DOUBLE_STRIKE) + + class ST_UniversalMeasure(BaseSimpleType): @classmethod def convert_from_xml(cls, str_value): diff --git a/pptx/oxml/text.py b/pptx/oxml/text.py index cf99dd0da..426c41c84 100644 --- a/pptx/oxml/text.py +++ b/pptx/oxml/text.py @@ -27,6 +27,7 @@ ST_TextSpacingPoint, ST_TextTypeface, ST_TextWrappingType, + ST_TextFontStrike, XsdBoolean, ) from pptx.oxml.xmlchemy import ( @@ -306,7 +307,8 @@ class CT_TextCharacterProperties(BaseOxmlElement): b = OptionalAttribute("b", XsdBoolean) i = OptionalAttribute("i", XsdBoolean) u = OptionalAttribute("u", MSO_TEXT_UNDERLINE_TYPE) - + strike = OptionalAttribute("strike", ST_TextFontStrike) + def _new_gradFill(self): return CT_GradientFillProperties.new_gradFill() diff --git a/pptx/text/text.py b/pptx/text/text.py index b880cf4ec..d8306c224 100644 --- a/pptx/text/text.py +++ b/pptx/text/text.py @@ -10,7 +10,7 @@ from pptx.enum.lang import MSO_LANGUAGE_ID from pptx.enum.text import MSO_AUTO_SIZE, MSO_UNDERLINE from pptx.opc.constants import RELATIONSHIP_TYPE as RT -from pptx.oxml.simpletypes import ST_TextWrappingType +from pptx.oxml.simpletypes import ST_TextWrappingType, ST_TextFontStrike from pptx.shapes import Subshape from pptx.text.fonts import FontFiles from pptx.text.layout import TextFitter @@ -309,6 +309,27 @@ def bold(self): def bold(self, value): self._rPr.b = value + @property + def strikethrough(self): + return { + ST_TextFontStrike.SINGLE_STRIKE: True, + ST_TextFontStrike.DOUBLE_STRIKE: True, + ST_TextFontStrike.NO_STRIKE: False, + None: None, + }[self._rPr.strike] + + @strikethrough.setter + def strikethrough(self, value): + if value not in (True, False, None): + raise ValueError( + "assigned value must be True, False, or None, got %s" % value + ) + self._rPr.strike = { + True: ST_TextFontStrike.SINGLE_STRIKE, + False: ST_TextFontStrike.NO_STRIKE, + None: None, + }[value] + @lazyproperty def color(self): """