我需要将图像添加到pptx幻灯片中,并希望将其放置在幻灯片的中心,而无需手动计算尺寸和对齐方式,
我发现了一个关于使用文字执行此操作的问题: question about center aligning text
关于使用文本执行此操作的文档: documentation about center aligning text
但无法为图像找到方法,
想法?
答案 0 :(得分:5)
It's not going to work the same way as text; there's no center justification or alignment property on an image. You'll need to use a formula.
image.left = (prs.slide_width - image.width) / 2
答案 1 :(得分:1)
答案2020年8月13日
这对我有用:
from pptx import Presentation
from pptx.util import Inches
from PIL import Image
# instantiate presentation
prs = Presentation()
# change slide sizes to Widescreen
slide_size = (16, 9)
prs.slide_width, prs.slide_height = Inches(slide_size[0]), Inches(slide_size[1])
# convert pixels to inches
def px_to_inches(path):
im = Image.open(path)
width = im.width / im.info['dpi'][0]
height = im.height / im.info['dpi'][1]
return (width, height)
img = px_to_inches('logo.png')
# insert logo image
left = Inches(slide_size[0] - img[0]) / 2
top = Inches(slide_size[1] - img[1]) / 2
pic = slide.shapes.add_picture('logo.png', left, top)