From e8f0bc4a851850b07b7641aea2a949418b15f632 Mon Sep 17 00:00:00 2001 From: YazzyYaz Date: Tue, 9 Feb 2016 22:03:33 +0000 Subject: [PATCH 1/2] feat: Add autofit image option --- pptx/shapes/placeholder.py | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/pptx/shapes/placeholder.py b/pptx/shapes/placeholder.py index 8d665a5ea..307dc4078 100644 --- a/pptx/shapes/placeholder.py +++ b/pptx/shapes/placeholder.py @@ -359,7 +359,7 @@ class PicturePlaceholder(_BaseSlidePlaceholder): """ Placeholder shape that can only accept a picture. """ - def insert_picture(self, image_file): + def insert_picture(self, image_file, method="crop"): """ Return a |PlaceholderPicture| object depicting the image in *image_file*, which may be either a path (string) or a file-like @@ -369,11 +369,12 @@ def insert_picture(self, image_file): :attr:`~._BaseSlidePlaceholder.shape_type` property is `MSO_SHAPE_TYPE.PLACEHOLDER` instead of `MSO_SHAPE_TYPE.PICTURE`. """ - pic = self._new_placeholder_pic(image_file) + pic = self._new_placeholder_pic(image_file, method) self._replace_placeholder_with(pic) return PlaceholderPicture(pic, self._parent) + - def _new_placeholder_pic(self, image_file): + def _new_placeholder_pic(self, image_file, method="crop"): """ Return a new `p:pic` element depicting the image in *image_file*, suitable for use as a placeholder. In particular this means not @@ -382,8 +383,25 @@ def _new_placeholder_pic(self, image_file): """ rId, desc, image_size = self._get_or_add_image(image_file) id_, name = self.id, self.name - pic = CT_Picture.new_ph_pic(id_, name, desc, rId) - pic.crop_to_fit(image_size, (self.width, self.height)) + + if method == "crop": + pic = CT_Picture.new_ph_pic(id_, name, desc, rId) + pic.crop_to_fit(image_size, (self.width, self.height)) + + else: + aspectImg = image_size[0]/image_size[1] + aspectPh = self.width / self.height + + if aspectPh > aspectImg: + h = self.height + w = int(aspectImg * self.height) + + else: + w = self.width + h = int(aspectImg * w) + + pic = CT_Picture.new_pic(id_, name, desc, rId, self.left, self.top, w, h) + return pic def _get_or_add_image(self, image_file): From 25e26e4ea6e9c2b2e461873418937afd88480a50 Mon Sep 17 00:00:00 2001 From: YazzyYaz Date: Thu, 28 Apr 2016 15:52:18 +0000 Subject: [PATCH 2/2] feat: Add del slide function --- pptx/parts/presentation.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pptx/parts/presentation.py b/pptx/parts/presentation.py index 35b99a8b6..262ca6d49 100644 --- a/pptx/parts/presentation.py +++ b/pptx/parts/presentation.py @@ -129,6 +129,11 @@ def add_slide(self, slidelayout): self._sldIdLst.add_sldId(rId) return slide + def del_slide(self, presentation, index): + xml_slides = presentation.slides._sldIdLst + slides = list(xml_slides) + xml_slides.remove(slides[index]) + def index(self, item): """ Map *item* to an integer representing its zero-based position in this