在 PowerPoint 中编辑形状的位置

editing the position of a shape in PowerPoint

from pptx import Presentation


prs = Presentation(my_file)
print(prs.slides[1].shape[0])
#out:
#<pptx.shapes.picture.Picture at 0x2295816cf98>

我需要遍历我的形状并指定自定义高度、宽度和垂直位置:

height = 7002000
width = 12193200

我有我的高度 + 宽度值,我可以通过分配设置 prs.slides[1].shape[0].height = height

用一个简单的循环。

我找不到的一件事是设置形状在页面上的位置的属性,主要是 Vertical Position

我的正确值设置为 -0.16cm,我正在尝试复制它。

我认为它可能在 lefttop 之下,但我的正确呈现 returns 值为 0

终于找到了答案 - 我不得不在形状属性上使用 topleft 的组合。

在我的例子中,我必须将我的变量设置为

top = -57600
left = 0 

然后我访问形状方法

for slide in prs.slides:
   for shape in slide.shapes:
       shape.left = left
       shape.top = top

请注意,您可以像这样使用提供的便利测量值:

from pptx.util import Cm

shape.left = Cm(5.5)

这样就省去了您自己计算英制单位 (EMU) 的时间。