Work with VBA Macros in PowerPoint PPT/PPTX in Python

Work with VBA Macros in PowerPoint PPT/PPTX in Python

VBA macros allow you to automate various types of operations in PowerPoint presentations. You can use them for generating charts, unhiding the hidden objects, and performing different repetitive tasks in the presentations. While working with PowerPoint presentations from within your Python applications, you may need to manipulate VBA macros. Therefore, in this article, you will learn how to add, extract and remove VBA Macros in PowerPoint using Python.

Python Library to Work with VBA Macros in PowerPoint

To work with VBA macros in PowerPoint presentations, we will use Aspose.Slides for Python. The library provides a rich set of features to create, edit and convert PowerPoint presentations. You can install the library from PyPI using the following command.

> pip install aspose.slides

Add VBA Macro in PowerPoint Presentations in Python

The following are the steps to add a VBA macro in a PowerPoint presentation using Python.

  • First, use Presentation class to load or create the PowerPoint presentation.
  • Then, create a new VbaProject and assign it to Presentation.vba_project property.
  • After that, add an empty VBA module using Presentation.vba_project.modules.add_empty_module(string) method.
  • Add source code to the VBA module using IVbaModule.source_code property.
  • Associate the references and add them to Presentation.vba_project.references collection.
  • Finally, save the presentation file using Presentation.save(string, SaveFormat.PPTM) method.

The following code sample shows how to add VBA macro in a PowerPoint presentation using Python.

Extract VBA Macros from PowerPoint in Python

You can also access VBA macros added to a PowerPoint presentation and extract their information. The following steps demonstrate how to extract VBA macros and print their name and source code in Python.

  • First, load the macro-enabled presentation using the Presentation class.
  • Then, check if presentation contains the VBA projects using Presentation.vba_project property.
  • Access each VBA module in Presentation.vba_project.modules collection.
  • Finally, extract source code and name of VBA module.

The following code sample shows how to extract PowerPoint VBA macros in Python.

Remove VBA Macros from a PowerPoint Presentation

To remove a VBA macro, you need to access it using its index. Once you have the macro’s reference, you can remove it from the collection. The following are the steps to remove a VBA macro from a PowerPoint presentation.

  • First, load the macro-enabled PowerPoint presentation using the Presentation class.
  • Then, remove VBA macro using Presentation.vba_project.modules.remove(Presentation.vba_project.modules[index]) method.
  • Finally, save the updated presentation using Presentation.save(string, SaveFormat.PPTM) method.

The following code sample shows how to remove a VBA macro in Python.

Get a Free License

You can use Aspose.Slides for Python without evaluation limitations by getting a free temporary license.

Conclusion

In this article, you have learned how to work with VBA macros in PowerPoint presentations using Python. With the help of code samples, we have demonstrated how to add, extract and remove VBA macros in PowerPoint presentations. In addition, you may visit the documentation to explore more about Aspose.Slides for Python. Also, you can ask your question and queries via our forum.

See Also