Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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')
like image 937
Lisa Avatar asked May 31 '17 04:05

Lisa


People also ask

How do you change the width and height of a picture in PowerPoint?

Click the picture, shape, text box, or WordArt that you want to resize. Do one of the following: To resize a picture, under Picture Tools, on the Format tab, in the Size group, enter the measurements that you want into the Height and Width boxes.

When inserting pictures in PowerPoint How do you avoid distorting the picture if it is resized later?

When inserting pictures, how can you avoid distorting the pictureif it is resized later? Use best scale for slide show. Lock the aspect ratio. Compress the picture.

How do I change the default picture size in PowerPoint?

Click File > Options > Advanced. Under Image Size and Quality, select High fidelity in the Default resolution list.


1 Answers

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))
like image 186
Jarad Avatar answered Oct 27 '22 23:10

Jarad