Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating a new pptx with python-pptx, and setting dimensions

Tags:

python-pptx

I have a series of images that I want to consistently embed in a powerpoint document at the same position on the page.

I can do the image import piece, but I need the powerpoint dimensions to be specifiable by the user.

I think I can use the slide_height and slide_width. I'm trying code like this, to get a page 300mm high, but it isn't working:

pack = Presentation(pptx=None) pack.slide_height(300) pack.save('test.pptx')

It throws a "TypeError: 'Emu' object is not callable" error.

I'm sure this is really obvious, but I can't see what the syntax should be, and the documentation isn't helping me. Would anyone be able to correct my code?

Many thanks,

Jeff.

like image 492
Jeff Avatar asked May 23 '26 02:05

Jeff


1 Answers

I think what you're looking for is this:

from pptx import Presentation
from pptx.util import Mm

prs = Presentation()
prs.slide_height = Mm(300)
prs.save('test.pptx')

In the API documentation for Presentation.slide_height you'll notice that slide_height is not followed by any parentheses (in contrast to save() just above it, for example). This indicates that slide_height is a property rather than a method, and is used by accessing it directly or assigning to it, like any other attribute.

The native unit of PowerPoint is the English Metric Unit (EMU), which is 1/914400th of an inch. These are described in the documentation here. 300 of these is a very short length indeed, which is what you would get if you assigned 300 to slide_height directly.

To allow you to skip the arithmetic, there are a variety of utility objects (like Mm() above) that allow EMU to be specified in common units, like centimeters and inches. Those are described in the documentation here.

like image 163
scanny Avatar answered May 27 '26 14:05

scanny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!