Create Excel Charts and Embed Them in Presentations as OLE Objects
Overview
This article shows how to create an Excel column chart with Aspose.Cells for Python via Java and embed its workbook in a PowerPoint presentation with Aspose.Slides for Python via Java. The embedded workbook contains both the source data and the chart. A separate PNG preview displays the chart on the slide before the OLE object is activated.
The result is an embedded Excel object, not a native PowerPoint chart. Editing the workbook through OLE requires a desktop application that supports OLE activation and a compatible Excel installation.
Prerequisites
Install the aspose-cells and aspose-slides-java Python packages and a compatible Java runtime. Import both packages before starting the JVM so that their Java libraries are available to JPype. Microsoft Office is not required to generate the presentation with these libraries.
No input files are required: the example creates the data and chart in memory, then writes OutputChart.pptx to the current working directory. Apply licenses for both products when running outside evaluation mode.
How the Embedding Works
- Create a workbook with a data worksheet and a chart sheet.
- Make the chart sheet active so that the workbook opens on the chart.
- Set the workbook’s OLE display range and render the chart to PNG.
- Save the workbook as XLSX in memory and embed those bytes in a slide.
- Assign the PNG as the OLE object’s preview and save the presentation as PPTX.
The workbook display range and the frame on the slide have different roles. setOleSize takes zero-based start/end row and column indices. ChartCollection.add instead takes the top row, left column, bottom row, and right column. The example uses row index 55 and column index 25 as the lower-right boundaries; these values are layout choices, not the number of data rows or columns.
The slide frame uses points. The example fits it within the slide and centers it while preserving the PNG’s aspect ratio, rather than stretching the preview to fill the slide.
Create and Embed the Chart
The following complete example populates DataSheet with four rows and five columns of values. Each row becomes a chart series. ChartSheet is selected as the active sheet before the workbook is saved.
The workbook is explicitly saved as XLSX, matching the xlsx extension passed to OleEmbeddedDataInfo. Java byte-array streams keep the workbook and image in memory and provide the byte arrays required by the Java APIs. The PNG is loaded directly from its bytes without an intermediate bitmap conversion. Assigning it through getSubstitutePictureFormat replaces the default OLE placeholder.
import jpype
import asposecells
import asposeslides
if not jpype.isJVMStarted():
jpype.startJVM()
from asposecells.api import Workbook, ChartType, SheetType, ImageOrPrintOptions, ImageType
from asposecells.api import SaveFormat as CellsSaveFormat
from asposeslides.api import Images, OleEmbeddedDataInfo, Presentation, SaveFormat
ByteArrayOutputStream = jpype.JClass("java.io.ByteArrayOutputStream")
workbook = Workbook()
try:
data_sheet = workbook.getWorksheets().get(0)
data_sheet.setName("DataSheet")
values = [
[67, 44, 46, 43, 24],
[86, 64, 97, 29, 40],
[68, 89, 78, 69, 38],
[91, 48, 60, 26, 25],
]
for row, row_values in enumerate(values):
for column, value in enumerate(row_values):
data_sheet.getCells().get(row, column).putValue(jpype.JInt(value))
chart_sheet_index = workbook.getWorksheets().add(SheetType.CHART)
chart_sheet = workbook.getWorksheets().get(chart_sheet_index)
chart_sheet.setName("ChartSheet")
last_chart_row = 55
last_chart_column = 25
chart_index = chart_sheet.getCharts().add(ChartType.COLUMN, 0, 0, last_chart_row, last_chart_column)
chart = chart_sheet.getCharts().get(chart_index)
for row in range(1, len(values) + 1):
chart.getNSeries().add(f"DataSheet!A{row}:E{row}", False)
workbook.getWorksheets().setActiveSheetIndex(chart_sheet_index)
workbook.getWorksheets().setOleSize(0, last_chart_row, 0, last_chart_column)
# Render the chart preview and serialize the editable workbook separately.
image_options = ImageOrPrintOptions()
image_options.setImageType(ImageType.PNG)
chart_image_stream = ByteArrayOutputStream()
chart.toImage(chart_image_stream, image_options)
chart_image_bytes = chart_image_stream.toByteArray()
workbook_stream = ByteArrayOutputStream()
workbook.save(workbook_stream, CellsSaveFormat.XLSX)
workbook_bytes = workbook_stream.toByteArray()
chart_image = Images.fromData(chart_image_bytes)
try:
presentation = Presentation()
try:
slide = presentation.getSlides().get_Item(0)
slide_width = presentation.getSlideSize().getSize().getWidth()
slide_height = presentation.getSlideSize().getSize().getHeight()
# Fit the preview within the slide without changing its aspect ratio.
scale = min(slide_width / chart_image.getWidth(), slide_height / chart_image.getHeight())
frame_width = chart_image.getWidth() * scale
frame_height = chart_image.getHeight() * scale
frame_x = (slide_width - frame_width) / 2
frame_y = (slide_height - frame_height) / 2
embedded_data = OleEmbeddedDataInfo(workbook_bytes, "xlsx")
ole_frame = slide.getShapes().addOleObjectFrame(frame_x, frame_y, frame_width, frame_height, embedded_data)
preview_image = presentation.getImages().addImage(chart_image)
ole_frame.getSubstitutePictureFormat().getPicture().setImage(preview_image)
presentation.save("OutputChart.pptx", SaveFormat.Pptx)
finally:
presentation.dispose()
finally:
chart_image.dispose()
finally:
workbook.dispose()
Expected Result
OutputChart.pptx contains one slide with an embedded Excel workbook and a chart preview. The workbook contains the original data and a column chart with four series. The preview is centered on the slide and retains its proportions.

In a compatible desktop PowerPoint environment with Excel available, double-click the object to activate and edit the embedded workbook. Activation and editing depend on the host application’s OLE support and security settings.
FAQ
Why is a preview image needed?
The embedded workbook and its slide preview are separate content. Supply a chart image so the slide shows the chart instead of the default “EMBEDDED OLE OBJECT” placeholder. See Object Preview Issue when Adding OleObjectFrame.
Does the preview update automatically when I change the workbook in code?
Treat the preview as a snapshot. When updating workbook data programmatically, regenerate the chart image and replace the preview as well as the embedded workbook data.
Can I use XLS instead of XLSX?
Yes. Save the workbook in XLS format and use xls as the embedded data extension. The extension must match the actual workbook format; changing the extension alone does not convert the data.
Why might the object change size after activation?
The host application can update the OLE object’s presentation when it activates the workbook. The workbook’s OLE display range and the slide frame dimensions both matter. See Working Solution for Chart Resizing in PPTX.