Create, read, write PowerPoint files in Python
To use GemBox.Presentation in Python, you'll need to:
- Download and install GemBox.Presentation Setup.
- 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]
- 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.
import os import win32com.client as COM # Create ComHelper object. comHelper = COM.Dispatch("GemBox.Presentation.ComHelper") # If using 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")
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.