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

python pptx:canI如何重新定位图片占位符?,python,image,insert,powerpoint,python-pptx,Python,Image,Insert,Powerpoint,Python Pptx,我对PythonPPTX软件包比较陌生,但我发现它非常有用 我添加了一张带有“标题、图片和标题”布局的幻灯片 我想知道如何重新定位图片占位符 类似于我在幻灯片上放置标题占位符的方式,我尝试使用: placeholder_pict.left = Inches(1.96) placeholder_pict.top = Inches(0.0) 但是,这会生成AttributeError 我还尝试过在“insert_picture()”方法中使用“left”和“top”参数: 这生成了一个“TypeE

我对PythonPPTX软件包比较陌生,但我发现它非常有用

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

我想知道如何重新定位图片占位符

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

placeholder_pict.left = Inches(1.96)
placeholder_pict.top = Inches(0.0)
但是,这会生成AttributeError

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

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

如何在此幻灯片上重新定位图片占位符?我在文档中遗漏了什么吗

更新:

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

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

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

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

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

我没有零钱。图片出现在幻灯片上,但它的位置与我根本不尝试设置placeholder_pict.left和placeholder_pict.top时的位置相同

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

我根本看不到照片。我试着缩小,看看这张照片是否被放在了“幻灯片外”,但仍然没有照片的迹象。据我所知,我甚至连图片容器都没有了

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

我得到:
占位符\u pict.placeholder\u format.type=图片(18)
占位符_pict.left 1792288占位符_pict.top=612775

这让我非常困惑,为什么我显然无法成功地设置占位符_pict.left和占位符_pict.top的值。

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

这是通过使用自己的起始模板PPTX,并使用PowerPoint手动编辑其布局来实现的

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

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

此处及其周围的文档对此进行了描述:

根据一些额外的观察,本案例似乎暴露了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)

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

谢谢!我看了那一部分,但我显然忽略了它的意义。我回去重读一遍。我也很欣赏关于占位符是形状容器的澄清——这也很有帮助。好的,当我尝试使用“picture.left=Inches(1.96)”和“picture.top=Inches(0.0)”时,我的图片消失了。对于picture.left和picture.top的任何值,它都会消失。有什么想法/建议吗?(很抱歉没有使用代码格式。我不知道如何在注释中这样做。)当您只做
形状时会发生什么。添加图片(左,上)
?您可以尝试的另一件事是在插入图片之前设置左上方占位符。您也可以尝试
picture.left=0;picture.top=0
,它应该放在幻灯片的左上角。如果看不到生成的.pptx文件,很难说更多。另外,您确定您得到的占位符是图片占位符吗?请尝试
print(placeholder.placeholder\u format.type)
print(placeholder.left,placeholder.top)
查看您得到了什么。再次感谢您的帮助以及开发和支持非常有用的Python包。我对你之前的评论做出了回应,更新了我原来的帖子。谢谢-这很有用!再次感谢您开发和支持一个非常有用的Python包。
picture_left = Inches(1.96)
picture_top = Inches(0.0)
picture = placeholder_pict.insert_picture(img_path, picture_left, picture_top)
picture_left = Inches(1.96)
picture_top = Inches(0.0)
shapes.add_picture(img_path, picture_left, picture_top)
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)
picture = placeholder_pict.insert_picture(img_path)
picture.left = Inches(0)
picture.top = Inches(0)
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)
picture = placeholder_pict.insert_picture(...)
picture.left = Inches(1.96)
picture.top = Inches(0.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)