The open blogging platform. Say no to algorithms and paywalls.

Convert PPT to PNG or JPG in Python

How to convert PPT to PNG or JPG in Python using the Spire.Presentation for Python library.

Nowadays, PowerPoint presentations play a significant role in various spheres of life, from business meetings and educational seminars to creative projects and personal showcases. However, there are instances when we may need to convert PowerPoint files into different formats, such as PNG or JPG. Whether it’s for sharing on social media platforms, embedding into websites, or simply for convenience purposes, the ability to convert PPT to images can be highly beneficial. This article introduces how to convert PPT to PNG or JPG in Python using Spire.Presentation for Python library.

Install Dependency

This solution requires Spire.Presentation for Python to be installed as the dependency, which is a Python library for reading, creating and manipulating PowerPoint documents in a Python program. You can install Spire.Presentation for Python by executing the following pip command.

pip install Spire.Presentation

Convert PPT to PNG in Python

Spire.Presentation for Python provides the ISlide.SaveAsImage method that converts a given slide to an image stream. The image stream can then be saved as an image file with a popular image file extension like PNG, JPG, or BMP. The following code shows you how to convert PPT to PNG in Python using this library.

from spire.presentation.common import *  
from spire.presentation import *  
  
# Create a Presentation object  
presentation = Presentation()  
  
# Load a PPT or PPTX file  
presentation.LoadFromFile("C:/Users/Administrator/Desktop/input.pptx")  
  
# Loop through the slides in the presentation  
for i, slide in enumerate(presentation.Slides):  
  
    # Specify the output file name  
    fileName ="Output/ToImage_ + str(i) + ".png"  
    # Save each slide as a PNG image  
    image = slide.SaveAsImage()  
    image.Save(fileName)  
    image.Dispose()  
  
presentation.Dispose()

Convert PPT to JPG in Python

The following code snippet is almost identical to the one above, except that you need to specify the format of the generated image as JPG.

from spire.presentation.common import *  
from spire.presentation import *  
  
# Create a Presentation object  
presentation = Presentation()  
  
# Load a PPT or PPTX file  
presentation.LoadFromFile("C:/Users/Administrator/Desktop/input.pptx")  
  
# Loop through the slides in the presentation  
for i, slide in enumerate(presentation.Slides):  
  
    # Specify the output file name  
    fileName ="Output/ToImage_ + str(i) + ".jpg"  
    # Save each slide as a PNG image  
    image = slide.SaveAsImage()  
    image.Save(fileName)  
    image.Dispose()  
  
presentation.Dispose()

Convert PPT to Customized PNG or JPG Files in Python

Converting PPT to images of specified size is a useful feature. Make sure that the aspect ratio of the picture should be consistent with that of the original slide, otherwise the converted picture will be distorted. The following code snippet demonstrates how to convert PPT to PNG with customized size using Spire.Presentation for Python.

from spire.presentation.common import *  
from spire.presentation import *  
  
# Create a Presentation object  
presentation = Presentation()  
  
# Load a PPT or PPTX file  
presentation.LoadFromFile("C:/Users/Administrator/Desktop/input.pptx ")  
  
# Loop through the slides in the presentation  
for i, slide in enumerate(presentation.Slides):  
  
    # Specify the output file name  
fileName ="Output/ToImage_" + str(i) + ".png"  
  
    # Save each slide to a PNG image with a size of 800 * 450 pixels  
    image = slide.SaveAsImageByWH(800, 450)  
    image.Save(fileName)  
    image.Dispose()  
  
presentation.Dispose()



Continue Learning