python插入图片到powerpoint,如何设置图片的宽高?
python inserts pictures to powerpoint, how to set the width and height of the picture?
python-pptx 包的新功能。
https://python-pptx.readthedocs.io/en/latest/
想插入图片到powerpoint。
如何设置图片的宽高?
我现在的代码:
from pptx import Presentation
from pptx.util import Inches
img_path = 'monty-truth.png'
prs = Presentation()
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)
left = top = Inches(1)
pic = slide.shapes.add_picture(img_path, left, top)
prs.save('test.pptx')
add_picture
的参数是:
add_picture(image_file, left, top, width=None, height=None)
设置图片的宽高,只需要定义宽高参数即可。在我的一个项目中,我通过以下设置获得了良好的图片高度和位置:
pic = slide.shapes.add_picture(img_path, pptx.util.Inches(0.5), pptx.util.Inches(1.75),
width=pptx.util.Inches(9), height=pptx.util.Inches(5))
丽莎,看起来文档中的详细信息不知何故,很高兴您指出了这一点。
宽度和高度没有出现在示例中的原因是它们是可选的。如果未提供,图片将以 "native"(完整)尺寸插入。
可能更常见的是只提供这两者之一,然后 python-pptx
会为您计算未指定的尺寸,例如保持纵横比。所以如果你有一张大图,比如 4 x 5 英寸,并且想将它缩小到 1 英寸宽,你可以调用:
from pptx.util import Inches
pic = shapes.add_picture(image_path, left, top, Inches(1))
并且图片的尺寸将变为 1 x 1.25(在本例中),而您无需确切知道原件的尺寸。保持纵横比可以防止图片看起来 "stretched",就像您将图片设为 1 x 1 英寸时的样子。
同样的事情只适用于指定高度,如果你知道你想要它显示多高并且不想大惊小怪地计算比例宽度。
如果同时指定宽度和高度,这就是您将获得的尺寸,即使这意味着图像在处理过程中会变形。
python-pptx 包的新功能。 https://python-pptx.readthedocs.io/en/latest/ 想插入图片到powerpoint。 如何设置图片的宽高?
我现在的代码:
from pptx import Presentation
from pptx.util import Inches
img_path = 'monty-truth.png'
prs = Presentation()
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)
left = top = Inches(1)
pic = slide.shapes.add_picture(img_path, left, top)
prs.save('test.pptx')
add_picture
的参数是:
add_picture(image_file, left, top, width=None, height=None)
设置图片的宽高,只需要定义宽高参数即可。在我的一个项目中,我通过以下设置获得了良好的图片高度和位置:
pic = slide.shapes.add_picture(img_path, pptx.util.Inches(0.5), pptx.util.Inches(1.75),
width=pptx.util.Inches(9), height=pptx.util.Inches(5))
丽莎,看起来文档中的详细信息不知何故,很高兴您指出了这一点。
宽度和高度没有出现在示例中的原因是它们是可选的。如果未提供,图片将以 "native"(完整)尺寸插入。
可能更常见的是只提供这两者之一,然后 python-pptx
会为您计算未指定的尺寸,例如保持纵横比。所以如果你有一张大图,比如 4 x 5 英寸,并且想将它缩小到 1 英寸宽,你可以调用:
from pptx.util import Inches
pic = shapes.add_picture(image_path, left, top, Inches(1))
并且图片的尺寸将变为 1 x 1.25(在本例中),而您无需确切知道原件的尺寸。保持纵横比可以防止图片看起来 "stretched",就像您将图片设为 1 x 1 英寸时的样子。
同样的事情只适用于指定高度,如果你知道你想要它显示多高并且不想大惊小怪地计算比例宽度。
如果同时指定宽度和高度,这就是您将获得的尺寸,即使这意味着图像在处理过程中会变形。