From 716fe834530d14ccdf8c512cd55794f08a20f4ca Mon Sep 17 00:00:00 2001 From: Antoine Montes Date: Fri, 4 Oct 2019 10:19:37 +0200 Subject: [PATCH 1/6] layout: fix when predicate fail --- pptx/text/layout.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/pptx/text/layout.py b/pptx/text/layout.py index 290b83a5a..5562c07df 100644 --- a/pptx/text/layout.py +++ b/pptx/text/layout.py @@ -79,9 +79,13 @@ def predicate(point_size): entirely within *extents* when rendered at *point_size* using the font defined in *font_file*. """ - text_lines = self._wrap_lines(self._line_source, point_size) - cy = _rendered_size("Ty", point_size, self._font_file)[1] - return (cy * len(text_lines)) <= self._height + try: + text_lines = self._wrap_lines(self._line_source, point_size) + cy = _rendered_size("Ty", point_size, self._font_file)[1] + return (cy * len(text_lines)) <= self._height + # If wrap fail, the point size is not good + except: + return False return predicate @@ -297,9 +301,7 @@ class _Fonts(object): @classmethod def font(cls, font_path, point_size): if (font_path, point_size) not in cls.fonts: - cls.fonts[(font_path, point_size)] = ImageFont.truetype( - font_path, point_size - ) + cls.fonts[(font_path, point_size)] = ImageFont.truetype(font_path, point_size) return cls.fonts[(font_path, point_size)] From 6ebb66d8ec75ff9ddf4bab03dc1908700cf8f5ee Mon Sep 17 00:00:00 2001 From: Antoine Montes Date: Fri, 4 Oct 2019 10:19:47 +0200 Subject: [PATCH 2/6] text: add some padding --- pptx/text/text.py | 37 ++++++++++--------------------------- 1 file changed, 10 insertions(+), 27 deletions(-) diff --git a/pptx/text/text.py b/pptx/text/text.py index b880cf4ec..8c29a8914 100644 --- a/pptx/text/text.py +++ b/pptx/text/text.py @@ -60,14 +60,7 @@ def clear(self): p = self.paragraphs[0] p.clear() - def fit_text( - self, - font_family="Calibri", - max_size=18, - bold=False, - italic=False, - font_file=None, - ): + def fit_text(self, font_family="Calibri", max_size=18, bold=False, italic=False, font_file=None): """Fit text-frame text entirely within bounds of its shape. Make the text in this text frame fit entirely within the bounds of @@ -85,9 +78,9 @@ def fit_text( if self.text == "": return - font_size = self._best_fit_font_size( - font_family, max_size, bold, italic, font_file - ) + font_size = ( + self._best_fit_font_size(font_family, max_size, bold, italic, font_file) - 1 + ) # -1 to add some padding self._apply_fit(font_family, font_size, bold, italic) @property @@ -202,23 +195,15 @@ def word_wrap(self): setting to be removed from the text frame, causing it to inherit this setting from its style hierarchy. """ - return { - ST_TextWrappingType.SQUARE: True, - ST_TextWrappingType.NONE: False, - None: None, - }[self._txBody.bodyPr.wrap] + return {ST_TextWrappingType.SQUARE: True, ST_TextWrappingType.NONE: False, None: None}[self._txBody.bodyPr.wrap] @word_wrap.setter def word_wrap(self, value): if value not in (True, False, None): - raise ValueError( - "assigned value must be True, False, or None, got %s" % value - ) - self._txBody.bodyPr.wrap = { - True: ST_TextWrappingType.SQUARE, - False: ST_TextWrappingType.NONE, - None: None, - }[value] + raise ValueError("assigned value must be True, False, or None, got %s" % value) + self._txBody.bodyPr.wrap = {True: ST_TextWrappingType.SQUARE, False: ST_TextWrappingType.NONE, None: None}[ + value + ] def _apply_fit(self, font_family, font_size, is_bold, is_italic): """ @@ -240,9 +225,7 @@ def _best_fit_font_size(self, family, max_size, bold, italic, font_file): """ if font_file is None: font_file = FontFiles.find(family, bold, italic) - return TextFitter.best_fit_font_size( - self.text, self._extents, max_size, font_file - ) + return TextFitter.best_fit_font_size(self.text, self._extents, max_size, font_file) @property def _bodyPr(self): From 45c80bc5428a4b88ffefa3ca1d261dd458d7b17e Mon Sep 17 00:00:00 2001 From: Antoine Montes Date: Fri, 4 Oct 2019 10:29:46 +0200 Subject: [PATCH 3/6] __init__: format file --- pptx/__init__.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/pptx/__init__.py b/pptx/__init__.py index 1005e8a2b..6a2d66286 100644 --- a/pptx/__init__.py +++ b/pptx/__init__.py @@ -20,13 +20,7 @@ from pptx.parts.image import ImagePart # noqa: E402 from pptx.parts.media import MediaPart # noqa: E402 from pptx.parts.presentation import PresentationPart # noqa: E402 -from pptx.parts.slide import ( # noqa: E402 - NotesMasterPart, - NotesSlidePart, - SlideLayoutPart, - SlideMasterPart, - SlidePart, -) +from pptx.parts.slide import NotesMasterPart, NotesSlidePart, SlideLayoutPart, SlideMasterPart, SlidePart # noqa: E402 content_type_to_part_class_map = { CT.PML_PRESENTATION_MAIN: PresentationPart, From bc910e6a3ad8e637d76d6c976507a70249f447cf Mon Sep 17 00:00:00 2001 From: Antoine Montes Date: Fri, 4 Oct 2019 10:29:58 +0200 Subject: [PATCH 4/6] setup: modify setup --- setup.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/setup.py b/setup.py index 481b17dda..2cac533ac 100755 --- a/setup.py +++ b/setup.py @@ -35,9 +35,9 @@ def ascii_bytes_from(path, *paths): VERSION = version DESCRIPTION = "Generate and manipulate Open XML PowerPoint (.pptx) files" KEYWORDS = "powerpoint ppt pptx office open xml" -AUTHOR = "Steve Canny" -AUTHOR_EMAIL = "python-pptx@googlegroups.com" -URL = "http://github.com/scanny/python-pptx" +AUTHOR = "Antoine Montes(Fork from Steve Canny)" +AUTHOR_EMAIL = "antoine.montes@epita.fr" +URL = "git@github.com:Zenor27/python-pptx.git" LICENSE = license PACKAGES = find_packages(exclude=["tests", "tests.*"]) PACKAGE_DATA = {"pptx": ["templates/*"]} From 5017bd0001b3dfcc85568d7a0dd36079035e4986 Mon Sep 17 00:00:00 2001 From: Antoine Montes Date: Fri, 4 Oct 2019 10:35:05 +0200 Subject: [PATCH 5/6] setup: modify URL --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 2cac533ac..b23cb0156 100755 --- a/setup.py +++ b/setup.py @@ -37,7 +37,7 @@ def ascii_bytes_from(path, *paths): KEYWORDS = "powerpoint ppt pptx office open xml" AUTHOR = "Antoine Montes(Fork from Steve Canny)" AUTHOR_EMAIL = "antoine.montes@epita.fr" -URL = "git@github.com:Zenor27/python-pptx.git" +URL = "https://github.com/Zenor27/python-pptx.git" LICENSE = license PACKAGES = find_packages(exclude=["tests", "tests.*"]) PACKAGE_DATA = {"pptx": ["templates/*"]} From eb419b72cef6fa9569fb33ee99c85e3f337d2114 Mon Sep 17 00:00:00 2001 From: Antoine Montes Date: Fri, 4 Oct 2019 17:42:19 +0200 Subject: [PATCH 6/6] shape: add alt_text getter --- pptx/oxml/shapes/shared.py | 8 ++++++++ pptx/shapes/base.py | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/pptx/oxml/shapes/shared.py b/pptx/oxml/shapes/shared.py index 74eb562d3..3d5c2854a 100644 --- a/pptx/oxml/shapes/shared.py +++ b/pptx/oxml/shapes/shared.py @@ -167,6 +167,13 @@ 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 + @property def txBody(self): """ @@ -304,6 +311,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..5c5569da0 100644 --- a/pptx/shapes/base.py +++ b/pptx/shapes/base.py @@ -221,6 +221,13 @@ def width(self): def width(self, value): self._element.cx = value + @property + def alt_text(self): + """ + Alternative text for the shape + """ + return self._element.shape_alt_text + class _PlaceholderFormat(ElementProxy): """