Search code examples
pythonpowerpointpython-pptx

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


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

Solution

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