【问题标题】:python-pptx: How canI reposition the picture placeholder?python-pptx:如何重新定位图片占位符?
【发布时间】:2018-03-14 14:14:31
【问题描述】:

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

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

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() 接受 2 个位置参数,但给出了 4 个”

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

更新:

当我尝试时(这是 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() 接受 2 个位置参数,但给出了 4 个”

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

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)

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

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

picture = placeholder_pict.insert_picture(img_path)
picture.left = Inches(0)
picture.top = Inches(0)

我根本没有照片。我已经尝试缩小,看看图片是否可能已经“从幻灯片上”放置,但仍然没有图片的迹象。据我所知,我什至没有图片容器。

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

【问题讨论】:

    标签: python image insert powerpoint python-pptx


    【解决方案1】:

    根据一些额外的观察,这个案例似乎暴露了 PowerPoint 工作方式的一些“怪癖”。

    简短的回答是,如果要更改左侧和顶部,则需要明确设置宽度和高度,否则它们默认为 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)
    

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

    【讨论】:

    • 谢谢 - 这很好用!再次感谢您开发和支持一个非常有用的 Python 包。
    【解决方案2】:

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

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

    如果您只想在特定情况下更改单个形状的位置,则需要了解占位符是“空”形状或形状容器。您需要更改 .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

    【讨论】:

    • 谢谢!我看了那部分,但我显然错过了它的意义。我会回去重新阅读它。我也很欣赏关于占位符是形状容器的说明 - 这也很有帮助。
    • 好的,当我尝试使用“picture.left = Inches(1.96)”和“picture.top = Inches(0.0)”时,我的图片消失了。对于我尝试过的 picture.left 和 picture.top 的任何值,它也会消失。有什么想法/建议吗? (很抱歉没有使用代码格式。我无法在评论中弄清楚如何做到这一点。)
    • 当你只做shapes.add_picture(left, top) 时会发生什么?您可以尝试的另一件事是在占位符 before 插入图片时设置 left 和 top。你也可以试试picture.left = 0; picture.top = 0,它应该放在幻灯片的左上角。如果没有看到生成的 .pptx 文件,很难说更多。另外,你确定你得到的占位符是图片占位符吗?试试print(placeholder.placeholder_format.type)print(placeholder.left, placeholder.top) 看看你会得到什么。
    • 再次感谢您的帮助以及开发和支持一个非常有用的 Python 包。我已通过更新我的原始帖子来回应您之前的评论。
    猜你喜欢
    • 2012-02-17
    • 2022-01-27
    • 1970-01-01
    • 1970-01-01
    • 2015-06-27
    • 2021-09-18
    • 1970-01-01
    • 1970-01-01
    • 2020-09-07
    相关资源
    最近更新 更多