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

python-pptx: How canI reposition the picture placeholder?

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

我添加了 "title and picture and caption" 布局的幻灯片。

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() takes 2 positional arguments but 4 were given"

如何重新定位这张幻灯片上的图片占位符?我是否遗漏了文档中的内容?

更新:

当我尝试时(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() takes 2 positional arguments but 4 were given"

当我尝试时(我相信这是 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)

我根本看不到照片。我试过缩小,看看图片是否已放置 "off the slide" 但仍然没有图片的迹象。据我所知,我连图片容器都没有了。

当我尝试时(根据 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 值。

占位符的(初始)位置由其在幻灯片 布局 中的位置决定。因此,如果您想更改 所有 使用该布局创建的幻灯片的占位符位置,您需要在布局上更改它。

这是通过使用您自己的起始模板 PPTX 并使用 PowerPoint 手动编辑其布局以适应来完成的。

如果您只想在特定情况下更改单个形状的位置,您需要了解占位符是 "empty" 形状或形状容器。您需要更改由 .insert_picture() 调用产生的 shape 的位置:

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

这在此处及其附近的文档中有描述:http://python-pptx.readthedocs.io/en/latest/user/placeholders-using.html#insert-content-into-a-placeholder

根据一些额外的观察,这个案例似乎揭示了一些 "quirks" PowerPoint 的工作原理。

简短的回答是,如果你想改变左边和顶部,你需要明确设置宽度和高度,否则它们默认为 0,这会导致形状 "disappear"。

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

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)

基本上这表明形状从其布局占位符继承 position-and-size 是 all-or-nothing。一旦明确设置了其中一个,就需要设置所有这些。