3

I have developed a small code in Python in order to generate PPTX. But I would like also to generate a picture in png or jpeg of this slide.

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)

left = Inches(5)
height = Inches(5.5)
pic = slide.shapes.add_picture(img_path, left, top, height=height)

prs.save('test.pptx')

Is there a way to transform a PPTX file (including just one slide) into a PNG or JPEG picture ?

Alex Andre
  • 51
  • 1
  • 2
  • 7
  • No. And it's unlikely it ever will. Doing this would require a rendering engine, something outside the scope of python-pptx's vision. Only way I've heard of folks doing this is by using the win32 API on Windows to have the built-in PowerPoint application save as images and work from there. I've heard talk about folks doing something similar with the OpenOffice library but don't know anything more about that. – Alex Andre May 16 '20 at 23:45
  • I think maybe converting the pptx file into a pdf and then turning it into image(s) will be the best approach. – Nimrod Mpandari Jul 08 '22 at 18:17

3 Answers3

2

You can use the following code :

Application = win32com.client.Dispatch("PowerPoint.Application")
Presentation = Application.Presentations.Open(r"your_path")
Presentation.Slides[0].Export(r"the_new_path-file.jpg", "JPG")
Application.Quit()
Presentation =  None
Application = None

With this line, you can change the number of the slide you want to export (for example below, for slide number 6) :

Presentation.Slides[5].Export(r"the_new_path-file.jpg", "JPG")
Alex Dana
  • 1,076
  • 3
  • 14
  • 30
  • 1
    That's on Windows. With Mac it would have to be AppleScript. – Martin Packer May 28 '20 at 07:26
  • 1
    I got the below while exporting to JPG. File ">", line 2, in Export pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft PowerPoint', "Slide.Export : PowerPoint can't save ^0 to ^1.", '', 0, -2147467259), None) – Naren Babu R Mar 31 '21 at 09:03
  • 1
    its not python code. how to run this code? is it shell script or what? – Adnan Ali Jul 16 '22 at 12:51
1

As a detour, you can first convert PPT to PDF, and then convert PDF to images. To convert PPT to PDF, you can use libreoffice:

soffice --headless --convert-to pdf test.pptx

To convert PDF to image, you can use poppler.

pdftoppm -singlefile -f 4 -r 72 -jpeg -jpegopt quality=90 test.pdf test_poppler

This is also a python package pdf2image which is basically a wrapper around poppler. You may also be interested in it.

For more info: see convert ppt slide to image and Converting PDF Pages to Images with Poppler.

jdhao
  • 24,001
  • 18
  • 134
  • 273
0

There's a python package called Aspose.Slides which could convert pptx to png.

Do:

pip install Aspose.Slides

This can also be used for converting pptx to pdf, as well as for creating a pptx file from scratch.

Pavin Raj
  • 1
  • 2
  • Your answer could be improved with additional supporting information. E.g. on how to use that package in the OPs case. You can find more information on how to write good answers in the [help center](https://stackoverflow.com/help/how-to-answer). – MagnusO_O Oct 09 '22 at 13:15
  • 1
    Aspose is not a good alternative as it adds watermark over the generated images – Sumant Agnihotri Dec 11 '22 at 16:58