简体   繁体   English

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

[英]python-pptx: How canI reposition the picture placeholder?

I'm relatively new to the python-pptx package but I'm finding it very useful. 我是python-pptx软件包的新手,但我发现它非常有用。

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. 但是,这生成了AttributeError。

I've also tried using "left" and "top" arguments to the "insert_picture()" method: 我还尝试过对“ insert_picture()”方法使用“ left”和“ top”参数:

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" 这生成了一个“ TypeError:insert_picture()接受2个位置参数,但给出了4个”

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" 我得到:“ TypeError:insert_picture()接受2个位置参数,但给出了4个”

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. 图片出现在幻灯片上,但与我完全不尝试设置placeholder_pict.left和placeholder_pict.top的位置相同。

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): 当我尝试时(按照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)

I get: 我得到:
placeholder_pict.placeholder_format.type = PICTURE (18) placeholder_pict.placeholder_format.type =图片(18)
placeholder_pict.left 1792288 placeholder_pict.top = 612775 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. 这让我很困惑,为什么我显然不能成功设置placeholder_pict.left和placeholder_pict.top值。

The (initial) location of a placeholder is determined by its location on the slide layout it comes from. 占位符的(初始)位置取决于它在其来自的幻灯片布局中的位置。 So if you want to change the location of a placeholder for all the slides created with that layout, you need to change it on the layout. 因此,如果要为使用该布局创建的所有幻灯片更改占位符的位置,则需要在布局上进行更改。

This is accomplished by using a starting template PPTX of your own, and editing its layouts to suit by hand, using PowerPoint. 这是通过使用您自己的启动模板PPTX,并使用PowerPoint编辑其布局以适合于手工完成的。

If all you want to do is change the position of a single shape in a particular case, you need to understand that a placeholder is an "empty" shape, or shape container. 如果您要做的只是在特定情况下更改单个形状的位置,则需要了解占位符是“空”形状或形状容器。 You need to change the location of the shape that results from the .insert_picture() call: 您需要更改由.insert_picture()调用产生的形状的位置:

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

This is described in the documentation at and around here: http://python-pptx.readthedocs.io/en/latest/user/placeholders-using.html#insert-content-into-a-placeholder 在以下位置的文档中对此进行了描述: http : //python-pptx.readthedocs.io/en/latest/user/placeholders-using.html#insert-content-into-a-placeholder

In light of some additional observations, it appears this case exposes some "quirks" in how PowerPoint works. 根据其他一些观察,这种情况似乎暴露了PowerPoint工作方式的一些“怪癖”。

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". 简短的答案是,如果要更改左侧和顶部,则需要显式设置宽度和高度,否则默认设置为0,这将导致形状“消失”。

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. 一旦您明确设置了其中之一,就需要设置所有这些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM