首页 > 解决方案 > 在 PPT 占位符中适合图片

问题描述

我想在 PPT 占位符中放置一张图片,并查看了以下两个链接:

类似问题

类似查询

从类似的问题,我已经应用了解决方案,但它仍然在 ppt 中显示裁剪图像。

我的代码相同:

prs = Presentation(path)
title_slide_layout = prs.slide_layouts[4] ## Changed the content to picture as I need two pictures in one slide
pic_placeholder = slide.placeholders[15] ## The two picture placeholders
pic_placeholder1 = slide.placeholders[16]

print(pic_placeholder.left.inches, pic_placeholder.top.inches, pic_placeholder.width.inches, pic_placeholder.height.inches)
## 0.5 2.4461811023622047 4.4184033245844265 4.253472222222222

"""Below solution from the question posted above """
pic_placeholder.left = Inches(0.1)
pic_placeholder.width = Inches(3)
pic_placeholder.height = Inches(3)

 ##Similarly I need to do for 2nd picture also, but it is not working for this picture also

prs.save(path)

至于Similar Query链接,我无法理解,最终解决方案是什么,如何实现。

问题陈述:(你想达到什么目的?

想要在幻灯片中并排获得两个图像,它们应该适合占位符并保留纵横比

你尝试了什么?

上面的代码是我试过的!

它在哪些具体方面不起作用?

在实施该解决方案时,我仍然从一侧得到裁剪的图像。

我想要的只是一张幻灯片上的两张不同的图像,而且没有一张被裁剪!!

标签: pythonpython-pptx

解决方案


图片占位符没有“缩小以适应”选项。无论您想对插入的图片进行何种额外调整,您都需要自己进行。

您没有显示用于插入图片的代码,因此我无法帮助您提供具体细节。但是,请注意返回的对象picture_placeholder.insert_picture()不是对象picture_placeholder本身。该对象在此过程中被删除(当您在那里执行此操作时,它在 PowerPoint 本身中)并被图片对象替换。因此,您对现在孤立的占位符元素所做的任何调整都将无效。你需要更多这样的东西:

inserted_picture = picture_placeholder.insert_picture(...)
inserted_picture.left = ...
inserted_picture.width = ...

推荐阅读