复制带有图像的幻灯片python pptx

编程入门 行业动态 更新时间:2024-10-11 11:14:49
本文介绍了复制带有图像的幻灯片python pptx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的最终目标是更改演示文稿的主题.为此,我创建了一个源模板和一个新模板(具有正确的主题).我遍历源模板中的每张幻灯片,然后使用以下代码将新幻灯片添加到具有源内容的新模板中-源.如果有更好的方法可以做到这一点,我很想听听.

My end goal is to change the theme of a presentation. To do this, I have created a source template and new template (with the correct theme). I iterate over each slide in the source template then add a new slide to the new template with the contents of the source using the code below - source. If there is a better way to do this I'd love to hear it.

这对于文本和文本框非常有用,但是测试图像无法显示在新的Powerpoint中(如下图所示):

This works great for text and text boxes, however the test image cannot be displayed in the new powerpoint (show in the image below):

def copy_slide_from_external_prs(self, src, idx, newPrs): # specify the slide you want to copy the contents from src_slide = src.slides[idx] # Define the layout you want to use from your generated pptx slide_layout = newPrs.slide_layouts[2] # create now slide, to copy contents to curr_slide = newPrs.slides.add_slide(slide_layout) # remove placeholders for p in [s.element for s in curr_slide.shapes if 'Text Placeholder' in s.name or 'Title' in s.name]: p.getparent().remove(p) # now copy contents from external slide, but do not copy slide properties # e.g. slide layouts, etc., because these would produce errors, as diplicate # entries might be generated for shp in src_slide.shapes: el = shp.element newel = copy.deepcopy(el) curr_slide.shapes._spTree.insert_element_before(newel, 'p:extLst') return newPrs

我尝试了许多不同的解决方案,并尝试使用源图像中的image.blob属性创建新的图片.但是,该图像没有元素.我是否需要将Blob转换为PNG,保存它,然后使用保存的PNG创建新图像?

I was trying many different solutions and tried creating a new Picture using the image.blob property in the source image. However, then the image does not have an element. Do I need to convert the blob to a PNG, save it, then create a new image using that saved PNG?

必须有一种更好的方法来执行此操作.同样,我只想更改主题.

There must be a better way to do this. Again, I just want to change the theme.

提前谢谢!

推荐答案

这是我开发的解决方法.我首先检查形状是否为图像,如果是,则将图像写入本地目录.然后,我使用保存的图像将照片添加到幻灯片中.最后,我删除了本地保存的图像.

Here is the workaround I developed. I first check if the shape is an image, and if it is, I write the image to a local directory. Then I add a picture to the slide using that saved image. Finally, I delete the locally saved image.

现在,此copy_slide函数适用于图像:

Now this copy_slide function works for images:

def copy_slide_from_external_prs(src, idx, newPrs): # specify the slide you want to copy the contents from src_slide = src.slides[idx] # Define the layout you want to use from your generated pptx SLD_LAYOUT = 5 slide_layout = prs.slide_layouts[SLD_LAYOUT] # create now slide, to copy contents to curr_slide = newPrs.slides.add_slide(slide_layout) # create images dict imgDict = {} # now copy contents from external slide, but do not copy slide properties # e.g. slide layouts, etc., because these would produce errors, as diplicate # entries might be generated for shp in src_slide.shapes: if 'Picture' in shp.name: # save image with open(shp.name+'.jpg', 'wb') as f: f.write(shp.image.blob) # add image to dict imgDict[shp.name+'.jpg'] = [shp.left, shp.top, shp.width, shp.height] else: # create copy of elem el = shp.element newel = copy.deepcopy(el) # add elem to shape tree curr_slide.shapes._spTree.insert_element_before(newel, 'p:extLst') # add pictures for k, v in imgDict.items(): curr_slide.shapes.add_picture(k, v[0], v[1], v[2], v[3]) os.remove(k)

更多推荐

复制带有图像的幻灯片python pptx

本文发布于:2023-10-31 10:04:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545761.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:幻灯片   图像   pptx   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!