Manage OLE in Presentations Using Python
Introduction
Note
OLE (Object Linking & Embedding) is a Microsoft technology that allows data and objects created in one application to be placed in another application through linking or embedding.Consider a chart created in MS Excel. The chart is then placed inside a PowerPoint slide. That Excel chart is considered an OLE object.
- An OLE object may appear as an icon. In this case, when you double-click the icon, the chart gets opened in its associated application (Excel), or you are asked to select an application for opening or editing the object.
- An OLE object may display its actual contents, such as the contents of a chart. In this case, the chart is activated in PowerPoint, the chart interface loads, and you get to modify the chart’s data within PowerPoint.
Aspose.Slides for Python via Java allows you to insert OLE objects into slides as OLE object frames (OleObjectFrame).
Add OLE Object Frames to Slides
Assuming you have already created a chart in Microsoft Excel and want to embed it in a slide as an OLE object frame using Aspose.Slides for Python via Java, you can do it this way:
- Create an instance of the Presentation class.
- Get a reference to a slide by its index.
- Read the Excel file as a byte array.
- Add the OleObjectFrame to the slide containing the byte array and other information about the OLE object.
- Write the modified presentation as a PPTX file.
In the example below, we added a chart from an Excel file to a slide as an OLE object frame using Aspose.Slides for Python via Java. Note that the OleEmbeddedDataInfo constructor takes an embeddable object extension as its second parameter. This extension allows PowerPoint to correctly interpret the file type and choose the right application to open this OLE object.
from pathlib import Path
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import OleEmbeddedDataInfo, Presentation, SaveFormat
presentation = Presentation()
try:
slide_size = presentation.getSlideSize().getSize()
slide = presentation.getSlides().get_Item(0)
# Prepare data for the OLE object.
file_data = Path("book.xlsx").read_bytes()
file_data = jpype.JArray(jpype.JByte)(file_data)
data_info = OleEmbeddedDataInfo(file_data, "xlsx")
# Add the OLE object frame to the slide.
frame_width = jpype.JFloat(slide_size.getWidth())
frame_height = jpype.JFloat(slide_size.getHeight())
slide.getShapes().addOleObjectFrame(0, 0, frame_width, frame_height, data_info)
presentation.save("output.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Add Linked OLE Object Frames
Aspose.Slides for Python via Java allows you to add an OleObjectFrame with a link to the file instead of embedded data.
This Python code shows you how to add an OleObjectFrame with a linked Excel file to a slide:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
# Add an OLE object frame with a linked Excel file.
slide.getShapes().addOleObjectFrame(20, 20, 200, 150, "Excel.Sheet.12", "book.xlsx")
presentation.save("output.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Access OLE Object Frames
If an OLE object is already embedded in a slide, you can easily find or access it this way:
- Load a presentation with the embedded OLE object by creating an instance of the Presentation class.
- Get a reference to the slide by its index.
- Access the OleObjectFrame shape. In our example, we used the previously created PPTX that has only one shape on the first slide. We then checked that the object was an OleObjectFrame. This was the desired OLE object frame to be accessed.
- Once the OLE object frame is accessed, you can perform any operation on it.
In the example below, an OLE object frame (an Excel chart object embedded in a slide) and its file data are accessed.
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import OleObjectFrame, Presentation
presentation = Presentation("sample.pptx")
try:
slide = presentation.getSlides().get_Item(0)
shape = slide.getShapes().get_Item(0)
if isinstance(shape, OleObjectFrame):
ole_frame = shape
# Get the embedded file data.
file_data = ole_frame.getEmbeddedData().getEmbeddedFileData()
# Get the extension of the embedded file.
file_extension = ole_frame.getEmbeddedData().getEmbeddedFileExtension()
# ...
finally:
presentation.dispose()
Access Linked OLE Object Frame Properties
Aspose.Slides allows you to access linked OLE object frame properties.
This Python code shows you how to check if an OLE object is linked and then obtain the path to the linked file:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import OleObjectFrame, Presentation
presentation = Presentation("sample.ppt")
try:
slide = presentation.getSlides().get_Item(0)
shape = slide.getShapes().get_Item(0)
if isinstance(shape, OleObjectFrame):
ole_frame = shape
# Check if the OLE object is linked.
if ole_frame.isObjectLink():
# Print the full path to the linked file.
print("OLE object frame is linked to: " + str(ole_frame.getLinkPathLong()))
# Print the relative path to the linked file if present.
# Only the PPT presentations can contain the relative path.
relative_path = ole_frame.getLinkPathRelative()
if relative_path is not None and not relative_path.isEmpty():
print("OLE object frame relative path: " + str(relative_path))
finally:
presentation.dispose()
Change OLE Object Data
Note
In this section, the code example below uses Aspose.Cells for Python via Java.If an OLE object is already embedded in a slide, you can easily access that object and modify its data this way:
- Load a presentation with the embedded OLE object by creating an instance of the Presentation class.
- Get a reference to the slide by its index.
- Access the OLE object frame shape. In our example, we used the previously created PPTX that has one shape on the first slide. We then checked that the object was an OleObjectFrame. This was the desired OLE object frame to be accessed.
- Once the OLE object frame is accessed, you can perform any operation on it.
- Create a Workbook object and access the OLE data.
- Access the desired Worksheet and amend the data.
- Save the updated Workbook in a stream.
- Change the OLE object data from the stream.
In the example below, an OLE object frame (an Excel chart object embedded in a slide) is accessed, and its file data is modified to update the chart data.
import jpype
import asposeslides
import asposecells
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import OleEmbeddedDataInfo, OleObjectFrame, Presentation, SaveFormat
from asposecells.api import Workbook, OoxmlSaveOptions
from asposecells.api import SaveFormat as CellsSaveFormat
from java.io import ByteArrayInputStream, ByteArrayOutputStream
presentation = Presentation("sample.pptx")
try:
slide = presentation.getSlides().get_Item(0)
shape = slide.getShapes().get_Item(0)
if isinstance(shape, OleObjectFrame):
ole_frame = shape
file_data = ole_frame.getEmbeddedData().getEmbeddedFileData()
ole_stream = ByteArrayInputStream(file_data)
# Read the OLE object data as a Workbook object.
workbook = Workbook(ole_stream)
new_ole_stream = ByteArrayOutputStream()
# Modify the workbook data.
cells = workbook.getWorksheets().get(0).getCells()
cells.get(0, 4).putValue("E")
cells.get(1, 4).putValue(jpype.JInt(12))
cells.get(2, 4).putValue(jpype.JInt(14))
cells.get(3, 4).putValue(jpype.JInt(15))
file_options = OoxmlSaveOptions(CellsSaveFormat.XLSX)
workbook.save(new_ole_stream, file_options)
# Change the OLE frame object data.
new_file_data = new_ole_stream.toByteArray()
new_data = OleEmbeddedDataInfo(new_file_data, ole_frame.getEmbeddedData().getEmbeddedFileExtension())
ole_frame.setEmbeddedData(new_data)
presentation.save("output.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Embed Other File Types in Slides
Besides Excel charts, Aspose.Slides for Python via Java allows you to embed other types of files into slides. For example, you can insert HTML, PDF, and ZIP files as objects. When a user double-clicks the inserted object, it automatically opens in the relevant program, or the user is prompted to select an appropriate program to open it.
This Python code shows you how to embed HTML and ZIP into a slide:
from pathlib import Path
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import OleEmbeddedDataInfo, Presentation, SaveFormat
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
html_data = Path("sample.html").read_bytes()
html_data = jpype.JArray(jpype.JByte)(html_data)
html_data_info = OleEmbeddedDataInfo(html_data, "html")
html_ole_frame = slide.getShapes().addOleObjectFrame(150, 120, 50, 50, html_data_info)
html_ole_frame.setObjectIcon(True)
zip_data = Path("sample.zip").read_bytes()
zip_data = jpype.JArray(jpype.JByte)(zip_data)
zip_data_info = OleEmbeddedDataInfo(zip_data, "zip")
zip_ole_frame = slide.getShapes().addOleObjectFrame(150, 220, 50, 50, zip_data_info)
zip_ole_frame.setObjectIcon(True)
presentation.save("output.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Set File Types for Embedded Objects
When working with presentations, you may need to replace old OLE objects with new ones or replace an unsupported OLE object with a supported one. Aspose.Slides for Python via Java allows you to set the file type for an embedded object, enabling you to update the OLE frame data or its extension.
This Python code shows you how to set the file type for an embedded OLE object to zip:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import OleEmbeddedDataInfo, Presentation, SaveFormat
presentation = Presentation("sample.pptx")
try:
slide = presentation.getSlides().get_Item(0)
ole_frame = slide.getShapes().get_Item(0)
file_extension = ole_frame.getEmbeddedData().getEmbeddedFileExtension()
file_data = ole_frame.getEmbeddedData().getEmbeddedFileData()
print("Current embedded file extension is: " + str(file_extension))
# Change the file type to ZIP.
data_info = OleEmbeddedDataInfo(file_data, "zip")
ole_frame.setEmbeddedData(data_info)
presentation.save("output.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Set Icon Images and Titles for Embedded Objects
After an OLE object is embedded, a preview consisting of an icon image is added automatically. This preview is what users see before accessing or opening the OLE object. If you want to use a specific image and text as elements in the preview, you can set the icon image and title using Aspose.Slides for Python via Java.
This Python code shows you how to set the icon image and title for an embedded object:
from pathlib import Path
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Presentation, SaveFormat
presentation = Presentation("sample.pptx")
try:
slide = presentation.getSlides().get_Item(0)
ole_frame = slide.getShapes().get_Item(0)
# Add an image to the presentation resources.
image_data = Path("image.png").read_bytes()
image_data = jpype.JArray(jpype.JByte)(image_data)
ole_image = presentation.getImages().addImage(image_data)
# Set a title and the image for the OLE preview.
ole_frame.setSubstitutePictureTitle("My title")
ole_frame.getSubstitutePictureFormat().getPicture().setImage(ole_image)
ole_frame.setObjectIcon(True)
presentation.save("output.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Prevent an OLE Object Frame from Being Resized and Repositioned
After you add a linked OLE object to a presentation slide, when you open the presentation in PowerPoint, you might see a message asking you to update the links. Clicking the “Update Links” button may change the size and position of the OLE object frame because PowerPoint updates the data from the linked OLE object and refreshes the object preview. To prevent PowerPoint from prompting to update the object’s data, set the setUpdateAutomatic method of the OleObjectFrame class to False:
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import Presentation, SaveFormat
presentation = Presentation("sample.pptx")
try:
slide = presentation.getSlides().get_Item(0)
ole_frame = slide.getShapes().get_Item(0)
ole_frame.setUpdateAutomatic(False)
presentation.save("output.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
Extract Embedded Files
Aspose.Slides for Python via Java allows you to extract the files embedded in slides as OLE objects this way:
- Create an instance of the Presentation class containing the OLE objects you intend to extract.
- Loop through all the shapes in the presentation and access the OleObjectFrame shapes.
- Access the data of embedded files from OLE object frames and write it to disk.
This Python code shows you how to extract files embedded in a slide as OLE objects:
from pathlib import Path
import jpype
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposeslides.api import OleObjectFrame, Presentation
presentation = Presentation("sample.pptx")
try:
slide = presentation.getSlides().get_Item(0)
for index in range(slide.getShapes().size()):
shape = slide.getShapes().get_Item(index)
if isinstance(shape, OleObjectFrame):
ole_frame = shape
file_data = ole_frame.getEmbeddedData().getEmbeddedFileData()
file_extension = ole_frame.getEmbeddedData().getEmbeddedFileExtension()
file_path = Path(f"OLE_object_{index}.{str(file_extension).lstrip('.')}")
file_path.write_bytes(bytes(file_data))
finally:
presentation.dispose()
FAQ
Will the OLE content be rendered when exporting slides to PDF/images?
What is visible on the slide is rendered—the icon/substitute image (preview). The “live” OLE content is not executed during rendering. If needed, set your own preview image to ensure the expected appearance in the exported PDF.
How can I lock an OLE object on a slide so users cannot move/edit it in PowerPoint?
Lock the shape: Aspose.Slides provides shape-level locks. This is not encryption, but it effectively prevents accidental edits and movement.
Why does a linked Excel object “jump” or change size when I open the presentation?
PowerPoint may refresh the preview of the linked OLE. For a stable appearance, follow the Working Solution for Worksheet Resizing practices—either fit the frame to the range, or scale the range to a fixed frame and set an appropriate substitute image.
Will relative paths for linked OLE objects be preserved in the PPTX format?
In PPTX, “relative path” information is not available—only the full path. Relative paths are found in the older PPT format. For portability, prefer reliable absolute paths/accessible URIs or embedding.