Manage ActiveX Controls in Presentations with Python

Introduction

ActiveX controls give a PowerPoint slide interactive elements such as text boxes, command buttons, and media players. Aspose.Slides for Python via .NET exposes a slide’s controls through Slide.controls. Use this collection to edit a control, move it on the slide, or remove it.

List ActiveX Controls and Their Stored Properties

To inspect an unfamiliar template, open it for reading and iterate through the controls on each slide. The example reports the slide number, control name, class_id, and each key-value pair in properties. It reads the presentation without activating controls, invoking events or macros, changing values, or saving the file.

import aspose.slides as slides

with slides.Presentation("ActiveX.pptm") as presentation:
    for slide_number, slide in enumerate(presentation.slides, start=1):
        for control in slide.controls:
            print(f"Slide {slide_number} | {control.name} | {control.class_id}")
            properties = control.properties
            if properties is None:
                print("  Properties unavailable")
                continue

            if properties.count == 0:
                print("  []")
            for entry in properties:
                print(f"  {entry.key} = {entry.value}")

Selected output from ActiveX.pptm shows a text box and a command button.

Slide 1 | TextBox1 | 8bd21d10-ec42-11ce-9e0d-00aa006002f3
  Value = Some text
  FontName = Arial
Slide 1 | CommandButton1 | d7053240-ce69-11cd-a777-00dd01143c57
  Caption = Show
  FontName = Arial

An empty property collection appears as []. For controls that use binary storage, properties can return None, shown as Properties unavailable. If you need only the names, use names_of_properties after checking for None. The report lists the properties available from the presentation. A control may support additional properties at runtime.

Modify ActiveX Controls

The example expects a macro-enabled presentation with a text box named TextBox1 and a command button named CommandButton1 on the first slide. It uses the control properties collection to change the text box value and the button caption. Each control also has a static preview that PowerPoint can display before activation. Supply a PNG for each control showing its updated text and appearance. The example assigns these images through Control.substitute_picture_format.

The example moves every control on the first slide down by 100 points by changing its frame. It saves the edited presentation in PPTM format, then uses ControlCollection.clear to save a second copy with all ActiveX controls removed from that slide.

from pathlib import Path

import aspose.slides as slides

with slides.Presentation("ActiveX.pptm") as presentation:
    controls = presentation.slides[0].controls
    text_box = next((control for control in controls if control.name == "TextBox1"), None)
    button = next((control for control in controls if control.name == "CommandButton1"), None)
    text_box_preview = Path("text-box-preview.png")
    button_preview = Path("button-preview.png")

    if text_box is None or button is None or text_box.properties is None or button.properties is None:
        print("The first slide must contain both named controls with editable properties.")
    elif not text_box_preview.is_file() or not button_preview.is_file():
        print("Supply both preview images before updating the controls.")
    else:
        text_box.properties.remove("Value")
        text_box.properties.add("Value", "Changed text")
        button.properties.remove("Caption")
        button.properties.add("Caption", "Show message")

        for control, preview in [(text_box, text_box_preview), (button, button_preview)]:
            image_data = preview.read_bytes()
            control.substitute_picture_format.picture.image = presentation.images.add_image(image_data)

        for control in controls:
            frame = control.frame
            control.frame = slides.ShapeFrame(frame.x, frame.y + 100, frame.width, frame.height, frame.flip_h, frame.flip_v, frame.rotation)

        presentation.save("edited-controls.pptm", slides.export.SaveFormat.PPTM)
        controls.clear()
        presentation.save("removed-controls.pptm", slides.export.SaveFormat.PPTM)

Add an ActiveX Media Player Control

To reuse a configured Windows Media Player control, start with a template whose first slide contains a player named WindowsMediaPlayer1. SlideCollection.add_clone copies the entire slide into a new presentation. The example sets the player’s URL in its properties collection and saves the cloned slide as the only slide in the output. The video stays external, so choose a path the player can access during playback.

import aspose.slides as slides

with slides.Presentation("template.pptx") as template, slides.Presentation() as presentation:
    slide = presentation.slides.add_clone(template.slides[0])
    player = next((control for control in slide.controls if control.name == "WindowsMediaPlayer1"), None)

    if player is None or player.properties is None:
        print("The template slide must contain a named media player with editable properties.")
    else:
        player.properties.remove("URL")
        player.properties.add("URL", "Wildlife.mp4")
        presentation.slides.remove_at(0)
        presentation.save("linked-video.pptx", slides.export.SaveFormat.PPTX)

FAQ

Can Aspose.Slides preserve ActiveX controls without running them?

Yes. Aspose.Slides can read and save controls, including their properties and positions, without activating them.

How do ActiveX controls differ from OLE objects?

ActiveX controls let users interact with a slide, for example by entering text or clicking a button. An OLE object lets a slide contain or link to content managed by another application, such as an Excel worksheet.

Will ActiveX events still trigger VBA macros after I save a presentation with Aspose.Slides?

Save the presentation in a macro-enabled format such as PPTM to retain its VBA project. PowerPoint for Windows can run the existing event handlers when its security settings allow both ActiveX controls and macros.