python inserts pictures to powerpoint, how to set the width and height of the picture?

Lisa picture Lisa · May 31, 2017 · Viewed 11.6k times · Source

New to python-pptx package. https://python-pptx.readthedocs.io/en/latest/ Would like to insert pictures to powerpoint. How can I set the width and height of the picture?

Code I have now:

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')

Answer

Jarad picture Jarad · May 31, 2017

Parameters of add_picture are:

add_picture(image_file, left, top, width=None, height=None)

To set the picture width and height, you just need to define the width and height parameters. In one of my projects, I got good picture height and placement with the following settings:

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))