Manage OLE
Info
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 object opening or editing.
- 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 the PowerPoint.
Aspose.Slides for Python via .NET allows you to insert OLE Objects into slides as OLE object frames (OleObjectFrame).
Adding 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 .NET, you can do it this way:
- Create an instance of the Presentation class.
- Get a slide’s reference through 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 OleObjectFrame using Aspose.Slides for Python via .NET.
Note that the OleEmbeddedDataInfo constructor takes an embeddable object extension as a second parameter. This extension allows PowerPoint to correctly interpret the file type and choose the right application to open this OLE object.
with slides.Presentation() as presentation:
slide_size = presentation.slide_size.size
slide = presentation.slides[0]
# Prepare data for the OLE object.
with open("book.xlsx", "rb") as file_stream:
file_data = file_stream.read()
data_info = slides.dom.ole.OleEmbeddedDataInfo(file_data, "xlsx")
# Add the OLE object frame to the slide.
ole_frame = slide.shapes.add_ole_object_frame(0, 0, slide_size.width, slide_size.height, data_info)
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
Adding Linked OLE Object Frames
Aspose.Slides for Python via .NET allows you to add an OleObjectFrame without embedding data but only with a link to the file.
This Python code shows you how to add an OleObjectFrame with a linked Excel file to a slide:
with slides.Presentation() as presentation:
slide = presentation.slides[0]
# Add an OLE object frame with a linked Excel file.
slide.shapes.add_ole_object_frame(20, 20, 200, 150, "Excel.Sheet.12", "book.xlsx")
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
Accessing 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 the reference of the slide by using its index.
- Access the OleObjectFrame shape. In our example, we used the previously created PPTX that has only one shape on the first slide.
- 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.
with slides.Presentation("sample.pptx") as presentation:
slide = presentation.slides[0]
shape = slide.shapes[0]
if isinstance(shape, slides.OleObjectFrame):
ole_frame = shape
# Get the embedded file data.
file_data = ole_frame.embedded_data.embedded_file_data
# Get the extension of the embedded file.
file_extension = ole_frame.embedded_data.embedded_file_extension
# ...
Accessing 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:
with slides.Presentation("sample.ppt") as presentation:
slide = presentation.slides[0]
shape = slide.shapes[0]
if isinstance(shape, slides.OleObjectFrame):
ole_frame = shape
# Check if the OLE object is linked.
if ole_frame.is_object_link:
# Print the full path to the linked file.
print("OLE object frame is linked to: " + ole_frame.link_path_long)
# Print the relative path to the linked file if present.
# Only the PPT presentations can contain the relative path.
if ole_frame.link_path_relative:
print("OLE object frame relative path: " + ole_frame.link_path_relative)
Changing OLE Object Data
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 the slide’s reference through its index.
- Access the OLEObjectFrame shape. In our example, we used the previously created PPTX that has one shape on the first slide.
- 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 io
import aspose.slides as slides
import aspose.cells as cells
with slides.Presentation("sample.pptx") as presentation:
slide = presentation.slides[0]
shape = slide.shapes[0]
if isinstance(shape, slides.OleObjectFrame):
ole_frame = shape
with io.BytesIO(ole_frame.embedded_data.embedded_file_data) as ole_stream:
# Read the OLE object data as a Workbook object.
workbook = cells.Workbook(ole_stream)
with io.BytesIO() as new_ole_stream:
# Modify the workbook data.
workbook.worksheets.get(0).cells.get(0, 4).put_value("E")
workbook.worksheets.get(0).cells.get(1, 4).put_value(12)
workbook.worksheets.get(0).cells.get(2, 4).put_value(14)
workbook.worksheets.get(0).cells.get(3, 4).put_value(15)
file_options = cells.OoxmlSaveOptions(cells.SaveFormat.XLSX)
workbook.save(new_ole_stream, file_options)
# Change the OLE frame object data.
new_data = slides.dom.ole.OleEmbeddedDataInfo(new_ole_stream.getvalue(), ole_frame.embedded_data.embedded_file_extension)
ole_frame.set_embedded_data(new_data)
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
Embedding Other File Types in Slides
Besides Excel charts, Aspose.Slides for Python via .NET 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:
with slides.Presentation() as presentation:
slide = presentation.slides[0]
with open("sample.html", "rb") as html_stream:
html_data = html_stream.read()
html_data_info = slides.dom.ole.OleEmbeddedDataInfo(html_data, "html")
html_ole_frame = slide.shapes.add_ole_object_frame(150, 120, 50, 50, html_data_info)
html_ole_frame.is_object_icon = True
with open("sample.zip", "rb") as zip_stream:
zip_data = zip_stream.read()
zip_data_info = slides.dom.ole.OleEmbeddedDataInfo(zip_data, "zip")
zip_ole_frame = slide.shapes.add_ole_object_frame(150, 220, 50, 50, zip_data_info)
zip_ole_frame.is_object_icon = True
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
Setting 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 .NET 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
:
with slides.Presentation("sample.pptx") as presentation:
slide = presentation.slides[0]
ole_frame = slide.shapes[0]
file_extension = ole_frame.embedded_data.embedded_file_extension
file_data = ole_frame.embedded_data.embedded_file_data
print(f"Current embedded file extension is: {file_extension}")
# Change the file type to ZIP.
ole_frame.set_embedded_data(slides.dom.ole.OleEmbeddedDataInfo(file_data, "zip"))
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
Setting Icon Images and Titles for Embedded Objects
After embedding an OLE object, 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 .NET.
This Python code shows you how to set the icon image and title for an embedded object:
with slides.Presentation("sample.pptx") as presentation:
slide = presentation.slides[0]
ole_frame = slide.shapes[0]
# Add an image to the presentation resources.
with slides.Images.from_file("image.png") as image:
ole_image = presentation.images.add_image(image)
# Set a title and the image for the OLE preview.
ole_frame.substitute_picture_title = "My title"
ole_frame.substitute_picture_format.picture.image = ole_image
ole_frame.is_object_icon = False
presentation.save("output.pptx", slides.export.SaveFormat.PPTX)
Prevent an OLE Object Frame from Being Resized and Pepositioned
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 update_automatic
property of the OleObjectFrame class to False
:
ole_frame.update_automatic = False
Extracting Embedded Files
Aspose.Slides for Python via .NET 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:
with slides.Presentation("sample.pptx") as presentation:
slide = presentation.slides[0]
for index, shape in enumerate(slide.shapes):
if isinstance(shape, slides.OleObjectFrame):
ole_frame = shape
file_data = ole_frame.embedded_data.embedded_file_data
file_extension = ole_frame.embedded_data.embedded_file_extension
file_path = f"OLE_object_{index}{file_extension}"
with open(file_path, 'wb') as file_stream:
file_stream.write(file_data)