【发布时间】: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