In modern office automation scenarios, it is often necessary to embed objects from external applications into PowerPoint presentations, such as Excel spreadsheets, Word documents, or ZIP archives. OLE (Object Linking and Embedding) technology allows these objects to be directly embedded into slides while maintaining their original editing capabilities. Automating OLE object handling with Python enables batch creation of presentations containing complex data objects, meeting the needs of enterprise reporting, data analysis, and document integration.
This article demonstrates how to embed, manage, and manipulate OLE objects in PowerPoint using Python, including embedding Excel files and ZIP archives, as well as extracting and modifying existing OLE objects.
Environment Setup
First, install the required library:
pip install Spire.Presentation
Core Implementation
Embedding Excel Files as OLE Objects
Embedding an Excel file in PowerPoint allows it to remain editable within the presentation. Double-clicking the embedded Excel object launches the Excel application for editing.
from spire.presentation.common import *
from spire.presentation import *
inputFile = "./Data/EmbedExcelAsOLE.xlsx"
outputFile = "EmbedExcelAsOLE.pptx"
# Create a PowerPoint document
ppt = Presentation()
# Load an image file as the preview image for the OLE object
stream = Stream("Data/EmbedExcelAsOLE.png")
oleImage = ppt.Images.AppendStream(stream)
stream.Close()
# Define the position and size of the OLE object
rec = RectangleF.FromLTRB(80, 60, oleImage.Width+80, oleImage.Height+60)
# Insert the OLE object based on Excel data
oleStream = Stream(inputFile)
oleObject = ppt.Slides[0].Shapes.AppendOleObject("excel", oleStream, rec)
oleObject.SubstituteImagePictureFillFormat.Picture.EmbedImage = oleImage
oleObject.ProgId = "Excel.Sheet.12"
oleStream.Close()
# Save the document
ppt.SaveToFile(outputFile, FileFormat.Pptx2010)
ppt.Dispose()
Code explanation:
- First, load the Excel file and preview image
- Define the rectangular area for the OLE object on the slide
- Use the AppendOleObject method to add the Excel file as an OLE object to the slide
- Set the preview image and ProgID (which identifies the application type) for the OLE object
ProgID is a key parameter that identifies the application used to open the object:
- Excel.Sheet.8 or Excel.Sheet.12 - Excel 97-2003 workbook or Excel 2007+ workbook
- Word.Document.8 or Word.Document.12 - Word 97-2003 document or Word 2007+ document
- PowerPoint.Show.8 or PowerPoint.Show.12 - PowerPoint 97-2003 presentation or PowerPoint 2007+ presentation
Embedding Other Types of OLE Objects
In addition to Excel files, you can also embed ZIP archives or other types of files:
from spire.presentation.common import *
from spire.presentation import *
inputFile = "./Data/EmbedZipIntoPPT.pptx"
inputFile_z = "./Data/test.zip"
inputFile_i = "./Data/icon.png"
outputFile = "EmbedZipIntoPPT.pptx"
# Create a PowerPoint document
ppt = Presentation()
ppt.LoadFromFile(inputFile)
# Load the ZIP file stream
stream = Stream(inputFile_z)
# Define the position and size of the OLE object
rec = RectangleF.FromLTRB(80, 60, 180, 160)
# Insert the ZIP object into the presentation
ole = ppt.Slides[0].Shapes.AppendOleObject(inputFile_z, stream, rec)
ole.ProgId = "Package" # Package ProgID is used for ZIP and other compressed files
# Load and set the icon image
image = Stream(inputFile_i)
oleImage = ppt.Images.AppendStream(image)
ole.SubstituteImagePictureFillFormat.Picture.EmbedImage = oleImage
# Save the document
ppt.SaveToFile(outputFile, FileFormat.Pptx2010)
ppt.Dispose()
Extracting OLE Object Data
You can extract embedded OLE object data from PowerPoint presentations:
from spire.presentation.common import *
from spire.presentation import *
inputFile = "./Data/ExtractOLEObject.pptx"
outputFile_xls = "ExtractOLEObject.xls"
outputFile_xlsx = "ExtractOLEObject.xlsx"
outputFile_doc = "ExtractOLEObject.doc"
outputFile_docx = "ExtractOLEObject.docx"
# Create a PowerPoint document
presentation = Presentation()
# Load the document from disk
presentation.LoadFromFile(inputFile)
# Iterate through slides and shapes
for slide in presentation.Slides:
for shape in slide.Shapes:
if isinstance(shape, IOleObject):
# Find the OLE object
oleObject = shape if isinstance(shape, IOleObject) else None
# Get its data and write to file
stream = oleObject.Data
if oleObject.ProgId == "Excel.Sheet.8":
stream.Save(outputFile_xls)
elif oleObject.ProgId == "Excel.Sheet.12":
stream.Save(outputFile_xlsx)
elif oleObject.ProgId == "Word.Document.8":
stream.Save(outputFile_doc)
elif oleObject.ProgId == "Word.Document.12":
stream.Save(outputFile_docx)
stream.Dispose()
presentation.Dispose()
Modifying OLE Object Content
You can modify the content of existing OLE objects in PowerPoint:
from spire.presentation.common import *
from spire.presentation import *
inputFile = "./Data/ModifyOLEData.pptx"
outputFile = "ModifyOLEData.pptx"
# Create a PowerPoint document
presentation = Presentation()
# Load the document from disk
presentation.LoadFromFile(inputFile)
# Iterate through slides and shapes
for slide in presentation.Slides:
for shape in slide.Shapes:
if isinstance(shape, IOleObject):
# Find the OLE object
oleObject = shape if isinstance(shape, IOleObject) else None
# Get its data
stream = oleObject.Data
stream2 = Stream()
if oleObject.ProgId == "PowerPoint.Show.12":
# Load the PowerPoint stream
ppt = Presentation()
ppt.LoadFromStream(stream, FileFormat.Auto)
# Append an image to the slide
ppt.Slides[0].Shapes.AppendEmbedImageByPath(
ShapeType.Rectangle, "Data/Logo.png", RectangleF.FromLTRB(50, 50, 150, 150))
# Save to a new stream
ppt.SaveToFile(stream2, FileFormat.Pptx2013)
stream2.Position = 0
# Modify the data
oleObject.Data = stream2
# Save the document
presentation.SaveToFile(outputFile, FileFormat.Pptx2013)
presentation.Dispose()
Practical Tips
Choose appropriate preview images: Set meaningful preview images for OLE objects to make presentations more intuitive.
Set ProgID correctly: Ensure the OLE object's ProgID matches its actual content type; otherwise, it may not open properly.
Resource management: Always remember to call the Dispose() method to release resources after completing operations.
Error handling: In production environments, appropriate exception handling mechanisms should be added.
Summary
This article demonstrated how to embed and manage OLE objects in PowerPoint using Python, including embedding different types of files, extracting OLE object data, and modifying existing OLE object content. Through these techniques, powerful automation tools can be built to handle complex presentations containing multiple types of data. These methods are particularly useful for business scenarios that require integrating multiple documents into a single presentation, such as creating enterprise reports with real-time data.

Top comments (0)