python-pptx:如何重新定位图片占位符?

标签 python image insert powerpoint python-pptx

我对 python-pptx 包比较陌生,但我发现它非常有用。

我添加了一张具有“标题、图片和标题”布局的幻灯片。

SLD_LAYOUT_TITLE_AND_PICTURE_AND_CAPTION = 8

prs = Presentation(output_pptx_fp)
slide_layout = prs.slide_layouts[SLD_LAYOUT_TITLE_AND_PICTURE_AND_CAPTION]

slide = prs.slides.add_slide(slide_layout)
shapes = slide.shapes

placeholder_pict = slide.placeholders[1]  # idx key, not position

placeholder_pict.insert_picture(img_path) 

我正在尝试找出如何重新定位图片占位符。

通过类比我在幻灯片上放置标题占位符的方式,我尝试使用:

placeholder_pict.left = Inches(1.96)
placeholder_pict.top = Inches(0.0)

但是,这生成了一个 AttributeError。

我还尝试在“insert_picture()”方法中使用“left”和“top”参数:

left = Inches(1.96)
top = Inches(0.0)
placeholder_pict.insert_picture(img_path, left, top)

这生成了一个“TypeError:insert_picture()需要2个位置参数,但给出了4个”

如何重新定位这张幻灯片上的图片占位符?我错过了文档中的某些内容吗?

更新:

当我尝试时(这是scanny没有建议的,但我很好奇):

picture_left = Inches(1.96)
picture_top = Inches(0.0)
picture = placeholder_pict.insert_picture(img_path, picture_left, picture_top)

我得到:“TypeError:insert_picture()需要2个位置参数,但给出了4个”

当我尝试时(我相信这是 scanny 的第一个建议):

picture_left = Inches(1.96)
picture_top = Inches(0.0)
shapes.add_picture(img_path, picture_left, picture_top)

图片位于我想要的位置,但它不在图片容器中(我可以接受),因此它的大小不适合图片容器(我可以通过添加代码来处理)

当我尝试时(我相信这是scanny的第二个建议):

picture_left = Inches(0.0)
picture_top = Inches(0.0)
placeholder_pict.left = picture_left
placeholder_pict.top = picture_top
picture = placeholder_pict.insert_picture(img_path)

我没有得到任何改变。图片出现在幻灯片上,但其位置与我根本不尝试设置 placeholder_pict.left 和 placeholder_pict.top 时的位置相同。

当我尝试时(我相信这是scanny的第三个建议):

picture = placeholder_pict.insert_picture(img_path)
picture.left = Inches(0)
picture.top = Inches(0)

我根本看不到照片。我尝试缩小,看看图片是否可能被放置在“幻灯片之外”,但仍然没有图片的迹象。据我所知,我什至不再拥有图片容器。

当我尝试时(根据 scanny 的请求):

placeholder_pict = slide.placeholders[1]  # idx key, not position
print("placeholder_pict.placeholder_format.type =", placeholder_pict.placeholder_format.type)
print("placeholder_pict.left", placeholder_pict.left, "placeholder_pict.top =", placeholder_pict.top)

我得到:
placeholder_pict.placeholder_format.type = 图片 (18)
placeholder_pict.left 1792288 placeholder_pict.top = 612775

这让我非常困惑为什么我显然无法成功设置 placeholder_pict.left 和 placeholder_pict.top 值。

最佳答案

根据一些额外的观察,此案例似乎暴露了 PowerPoint 工作方式的一些“怪癖”。

简短的回答是,如果要更改 left 和 top,则需要显式设置宽度和高度,否则它们默认为 0,这会导致形状“消失”。

这段代码应该能稍微说明一下情况:

prs = Presentation()
slide_layout = prs.slide_layouts[PICTURE_LAYOUT_IDX]
slide = prs.slides.add_slide(slide_layout)
shapes = slide.shapes

placeholder = slide.placeholders[1]
print(placeholder.left.inches, placeholder.top.inches)

# ---note that all these actions are *before* the image is inserted---

placeholder.left = Inches(3)
print(placeholder.left.inches)

print(placeholder.width)
placeholder.width = Inches(3)
print(placeholder.left.inches)

print(placeholder.height)
placeholder.height = Inches(3)
print(placeholder.height)

基本上,这表明形状从其布局占位符继承位置和大小是全有或全无的。一旦明确设置了其中一个,就需要设置所有这些。

关于python-pptx:如何重新定位图片占位符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49279998/

相关文章:

python - 在 python 中遍历并处理目录中的文件

python - gaussian_kde 具有偏态分布吗?

向文件中插入行的 Pythonic 方式

sql - 在单个查询中将行插入多个表,从涉及的表中选择

python - 使用 xml.etree.ElementTree 解析 XML 时获取空列表作为属性

python - 如何在 Django 中获取 Post 数据表单类别?

c# - 正确处理内存流(WPF 图像转换)

java - 使用布局管理器进行临时操作?最佳设计?

python - 如何将不同的图片正确插入到单个文件中

java - 错误的整数值 : '' for column 'Number' at row 1 - JDBC