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

I'm relatively new to the python-pptx package but I'm finding it very useful.

I've added a slide with the "title and picture and caption" layout.

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)

I'm trying to figure out how to reposition the picture placeholder.

By analogy to how I position the title placeholder on the slide, I've tried using:

placeholder_pict.left = Inches(1.96)

placeholder_pict.top = Inches(0.0)

However, this generated an AttributeError.

I've also tried using "left" and "top" arguments to the "insert_picture()" method:

left = Inches(1.96)

top = Inches(0.0)

placeholder_pict.insert_picture(img_path, left, top)

This generated a "TypeError: insert_picture() takes 2 positional arguments but 4 were given"

How do I reposition the picture placeholder on this slide? Did I miss something in the documentation?

UPDATE:

When I try (which is something scanny didn't suggest but I was curious):

picture_left = Inches(1.96)

picture_top = Inches(0.0)

picture = placeholder_pict.insert_picture(img_path, picture_left, picture_top)

I get: "TypeError: insert_picture() takes 2 positional arguments but 4 were given"

When I try (which I believe was scanny's first suggestion):

picture_left = Inches(1.96)

picture_top = Inches(0.0)

shapes.add_picture(img_path, picture_left, picture_top)

the picture is positioned where I want it but it's not in the picture container (which I can live with) and so it's not sized to fit in the picture container (which I can deal with by adding code)

When I try (which I believe was scanny's second suggestion):

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)

I get no change. The picture appears on the slide but it's in the same position as when I don't try setting placeholder_pict.left and placeholder_pict.top at all.

When I try (which I believe was scanny's third suggestion):

picture = placeholder_pict.insert_picture(img_path)

picture.left = Inches(0)

picture.top = Inches(0)

I get no picture at all. I've tried zooming out, to see if the picture might have been placed "off the slide" but still no sign of a picture. As far as I can tell, I no longer even have the picture container.

When I try (per scanny's request):

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)

I get:

placeholder_pict.placeholder_format.type = PICTURE (18)

placeholder_pict.left 1792288 placeholder_pict.top = 612775

This makes me very puzzled about why I apparently can't successfuly set the placeholder_pict.left and placeholder_pict.top values.

解决方案

In light of some additional observations, it appears this case exposes some "quirks" in how PowerPoint works.

The short answer is that you need to set width and height explicitly if you want to change left and top, otherwise they default to 0, which causes the shape to "disappear".

This code should illuminate the situation a bit:

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)

Basically what this indicates is that a shape's inheritance of position-and-size from its layout placeholder is all-or-nothing. As soon as you set one of those explicitly, you need to set all of them.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值