Read the complete article on how to add, extract, and remove VBA macros in PowerPoint using Python: https://blog.aspose.com/2022/02/21/work-with-vba-macros-in-powerpoint-in-python/
Forked from aspose-com-gists/add-vba-macro-in-powerpoint.py
Created
April 3, 2023 09:58
-
-
Save imvickykumar999/c93321e367c5f433f1bdb42d047b7114 to your computer and use it in GitHub Desktop.
Work with VBA Macros in PowerPoint PPT/PPTX in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create or load a presentation | |
with slides.Presentation() as presentation: | |
# Create new VBA project | |
presentation.vba_project = slides.vba.VbaProject() | |
# Add empty module to the VBA project | |
module = presentation.vba_project.modules.add_empty_module("Module") | |
# Set module source code | |
module.source_code = "Sub Test(oShape As Shape) MsgBox ""Test"" End Sub" | |
# Create reference to <stdole> | |
stdoleReference = slides.vba.VbaReferenceOleTypeLib("stdole", "*\\G{00020430-0000-0000-C000-000000000046}#2.0#0#C:\\Windows\\system32\\stdole2.tlb#OLE Automation") | |
# Create reference to Office | |
officeReference =slides.vba.VbaReferenceOleTypeLib("Office", "*\\G{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}#2.0#0#C:\\Program Files\\Common Files\\Microsoft Shared\\OFFICE14\\MSO.DLL#Microsoft Office 14.0 Object Library") | |
# add references to the VBA project | |
presentation.vba_project.references.add(stdoleReference) | |
presentation.vba_project.references.add(officeReference) | |
# Save presentation | |
presentation.save("add-vba-macro.pptm", slides.export.SaveFormat.PPTM) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Load a presentation | |
with slides.Presentation("presentation.pptm") as presentation: | |
# Check if presentation contains VBA Project | |
if presentation.vba_project is not None: | |
# Print each module | |
for module in presentation.vba_project.modules: | |
print(module.name) | |
print(module.source_code) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Load a presentation | |
with slides.Presentation("presentation.pptm") as presentation: | |
# Remove VBA macro using index | |
presentation.vba_project.modules.remove(presentation.vba_project.modules[0]) | |
# Save presentation | |
presentation.save("remove-vba-macro.pptm", slides.export.SaveFormat.PPTM) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment