Create, read, write PowerPoint files in Python

GemBox.Presentation is a .NET library that enables you to process PowerPoint files from any .NET application. But it's also a COM accessible library that you can use in Python as well.

System Requirements

To use GemBox.Presentation in Python, you'll need to:

  1. Download and install GemBox.Presentation Setup.
  2. Expose GemBox.Presentation to COM Interop with Regasm.exe tool:
    :: Add GemBox.Presentation to COM registry for x86 (32-bit) applications.
    C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe [path to installed GemBox.Presentation.dll]
    
    :: Add GemBox.Presentation to COM registry for x64 (64-bit) applications.
    C:\Windows\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe [path to installed GemBox.Presentation.dll]
  3. Install Python for Windows extension:
    :: Install Python extension for Windows.
    pip install pywin32

Working with PowerPoint files in Python

The following example shows how you can read a PowerPoint file from Python, create new or update existing content, and write the modified presentation to output PowerPoint file.

Reading and writing slide content from PowerPoint files in Python
Screenshot of read and updated PowerPoint files
import os
import win32com.client as COM

# Create ComHelper object.
comHelper = COM.Dispatch("GemBox.Presentation.ComHelper")

# If using the Professional version, put your serial key below.
comHelper.ComSetLicense("FREE-LIMITED-KEY")

# Read input presentation.
presentation = comHelper.Load(os.getcwd() + "\\%#Input.pptx%")

# Get first slide.
slide = comHelper.GetCollectionItem(presentation.Slides, 0)

# Remove first drawing.
comHelper.RemoveCollectionItemAt(slide.Content.Drawings, 0)

# Get master slide.
masterSlide = comHelper.GetCollectionItem(presentation.MasterSlides, 0)

# Get layout slide.
layoutSlide = comHelper.GetCollectionItem(masterSlide.LayoutSlides, 0)

# Create new slide.
slide = comHelper.AddNewSlide(presentation.Slides, layoutSlide)

# Create new shape.
shape = slide.Content.AddShape(ShapeGeometryType.RoundedRectangle, 5, 5, 12, 6)

# Set shape fill to light blue color.
shape.Format.Fill.SetSolid(comHelper.CreateColor(91, 155, 213))

# Create new paragraph with text.
run = shape.Text.AddParagraph().AddRun("This is a new text box on a new slide.")

# Set text fill to white color.
run.Format.Fill.SetSolid(comHelper.CreateColor(255, 255, 255))

# Write output presentation.
presentation.Save(s.getcwd()  + "\\Output.pptx")

Wrapper Library

Not all members of GemBox.Presentation are accessible because of the COM limitations like unsupported static and overload methods. That is why you can use the ComHelper class which provides alternatives for some members that cannot be called with COM Interop.

However, if you need to use many GemBox.Presentation members from Python, a recommended approach is to create a .NET wrapper library instead. Your wrapper library should do all the work within and exposes a minimal set of classes and methods to the unmanaged code.

This will enable you to take advantage of GemBox.Presentation's full capabilities, avoid any COM limitations, and improve performance by reducing the number of COM Callable Wrappers created at runtime.

See also


Next steps

GemBox.Presentation is a .NET component that enables you to read, write, edit, convert, and print presentation files from your .NET applications using one simple API.

Download Buy