Read and write VBA macros inside Excel workbooks (.xlsm, .xlsb, .xlam, .xls) in pure Python, no dependencies.
Project description
pyOpenVBA
Read and write the VBA macros inside Excel workbooks, in pure Python.
No external dependencies. No Excel install required. Works on Windows, macOS, and Linux. Python 3.10 or newer.
Supports .xlsm, .xlsb, .xlam, and legacy .xls.
Why use this?
Several excellent Python tools already exist for reading VBA out of Excel files (oletools, olefile, and friends), and they remain the right choice for forensics, malware analysis, and audit use-cases. pyOpenVBA focuses on the next step: safely writing changes back so the workbook still opens cleanly in Excel.
The write path is the whole point of the library:
- Modify a module's source in place.
- Add a new standard module, class module, or document/UserForm code-behind.
- Rename any module (the CFB stream,
dirrecord,PROJECTdeclaration,PROJECTwmname map, andAttribute VB_Nameare all updated in lockstep). - Delete a module cleanly.
- Save the workbook and have it reopen in Excel with no "we found
a problem with some content" repair dialog. Every supported format
(
.xlsm,.xlsb,.xlam,.xls) is verified against live Excel. - Create new
.xlsmfiles on the fly, and inject VBA code into them.
That makes it a good fit for:
- Version-controlling your VBA in git like normal source code, then pushing edits back without ever opening Excel.
- Diffing two workbooks to see what changed in
Module1. - Generating or updating macros from a script without scripting Excel through COM automation.
- Reading and writing macros on a server (Linux / CI) where Excel is not installed.
- Agentic AI Integration allow your AI agent easy access to both push and pull VBA code in your workbook.
pyOpenVBA is a complete read-and-write library, so it covers the full lifecycle of a VBA project in one place: extract, edit, version, write back, and verify. If you only ever need to read, the existing tools remain a solid choice; if you might ever need to write, start here and you will not have to switch later.
Installation
From PyPI:
pip install pyOpenVBA
Requires Python 3.10 or newer. There are no other dependencies.
After install, the CLI is available either as a module or as a script:
python -m pyopenvba --help
pyopenvba --help
From source (for development):
git clone https://github.com/WilliamSmithEdward/pyOpenVBA
cd pyOpenVBA
pip install -e ".[dev]"
30-second tour
from pyopenvba import ExcelFile
with ExcelFile("workbook.xlsm") as wb:
# 1. List all VBA modules in the workbook.
print(wb.module_names())
# ['ThisWorkbook', 'Sheet1', 'Module1']
# 2. Read a module's source as a string.
source = wb.get_module("Module1")
print(source)
# 3. Edit a module and save the workbook.
wb.set_module("Module1", 'Sub Hello()\r\n MsgBox "hi"\r\nEnd Sub\r\n')
wb.save() # overwrites the original file
# wb.save("edited.xlsm") # ...or save to a new file
That is the entire core API. Three methods.
Create a brand-new .xlsm from scratch
Need a fresh macro-enabled workbook without launching Excel? Use
ExcelFile.create_new():
from pyopenvba import ExcelFile
with ExcelFile.create_new("new_book.xlsm") as wb:
wb.set_module(
"Module1",
'Sub Hello()\r\n MsgBox "hello from python"\r\nEnd Sub\r\n',
)
wb.save()
The new workbook ships with ThisWorkbook, Sheet1, and an empty
Module1, and opens cleanly in Excel with no repair prompt.
Add, rename, or delete a module
from pyopenvba import ExcelFile, VBAModuleKind
with ExcelFile("workbook.xlsm") as wb:
project = wb.vba_project()
project.add_module(
"NewModule",
"Sub Hi()\r\n MsgBox \"hi\"\r\nEnd Sub\r\n",
kind=VBAModuleKind.standard,
)
project.rename_module("OldName", "NewName")
project.delete_module("Obsolete")
wb.save("out.xlsm")
Edit your macros as files on disk (recommended workflow)
This is the easiest way to manage VBA in a git repo. Export every module to a folder, edit the files in any text editor, then push the changes back into the workbook.
From the command line:
# 1. Pull every module out of the workbook into ./vba/
python -m pyopenvba pull workbook.xlsm ./vba
# 2. ...edit ./vba/Module1.bas in your editor of choice...
# 3. Push your edits back into the workbook
python -m pyopenvba push ./vba workbook.xlsm
# Bonus: list modules without extracting anything
python -m pyopenvba ls workbook.xlsm
From Python:
from pyopenvba import pull, push
pull("workbook.xlsm", "./vba")
# ...edit files...
push("./vba", "workbook.xlsm") # in place
push("./vba", "workbook.xlsm", out="edited.xlsm") # to a new file
Module files use the extensions VBA already uses: .bas for standard
modules, .cls for class modules, .frm for UserForm code-behind.
Supported formats
| Extension | What it is | Read | Write |
|---|---|---|---|
.xlsm |
Macro-enabled workbook | yes | yes |
.xlsb |
Binary workbook | yes | yes |
.xlam |
Macro-enabled add-in | yes | yes |
.xls |
Legacy (Excel 97-2003) | yes | yes |
Every save is verified to reopen in Excel without the "we found a problem with some content" repair dialog.
Safety guards
save() refuses to silently produce a broken workbook.
Password-protected projects
If the VBA project is password-protected, any mutation will raise
VBAProjectError unless you explicitly opt in:
wb.save(allow_protected=True)
The library never tries to decrypt or change the password - it just preserves the existing protection bytes verbatim. The resulting workbook still requires the original password to open the VBE.
Digitally-signed projects
A digital signature is invalidated by any change to the macros. On
mutation, the library drops the stale signature streams and emits a
UserWarning so you know trust has been removed:
import warnings
warnings.filterwarnings("error", category=UserWarning) # treat as fatal
# ...or silence the warning if you accept the consequence:
wb.save(allow_invalidate_signature=True)
What's out of scope
This library is intentionally focused on module source code. The following are preserved byte-for-byte but not interpreted:
- UserForm layout (controls, properties, positions). Editing the code-behind of a UserForm works fine; editing the design surface does not.
- VBA project password decryption / re-encryption.
- Re-signing digitally signed projects.
- ActiveX license editing.
See docs/roadmap.md for the full feature matrix.
Architecture
src/pyopenvba/
__init__.py public API (ExcelFile, pull, push, exceptions)
excel.py ExcelFile facade (ZIP / CFB dispatch, pull/push helpers)
vba.py VBA project parser + MS-OVBA codec
cfb.py MS-CFB (Compound File Binary) parser/writer
exceptions.py custom exception hierarchy
_templates/ baked-in empty .xlsm bytes for ExcelFile.create_new()
__main__.py `python -m pyopenvba {pull,push,ls}` CLI
For deeper documentation:
- docs/architecture.md - internal module layout.
- docs/ms-ovba-implementation-guide_v2.md - language-agnostic guide for re-implementing MS-OVBA in another language.
- docs/roadmap.md - per-feature implementation status.
Contributing
Bug reports, weird workbooks that break the library, and PRs are all welcome. Please include the workbook (or a minimal redacted version) when filing a parsing bug.
Run the full local check (same as CI):
pip install -e ".[dev]"
pyright src tests
pytest -p no:randomly
CI runs the test matrix on Python 3.10 / 3.11 / 3.12 / 3.13 across
Linux, plus 3.12 on Windows and macOS, on every push and pull request.
Releases are published to PyPI automatically when a v*.*.* tag is
pushed.
License
MIT.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyopenvba-1.1.1.tar.gz.
File metadata
- Download URL: pyopenvba-1.1.1.tar.gz
- Upload date:
- Size: 69.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
27b8d4b09fc3e1e89e9904d17a767c631e18e30f09f7576548f6f91694c088bc
|
|
| MD5 |
0b0c591381bc61fc3e50415a4bc32b23
|
|
| BLAKE2b-256 |
a9b3d3c2cf62f8c093ee022d827b9af741e5469060932e00cbcf1a313aaea822
|
Provenance
The following attestation bundles were made for pyopenvba-1.1.1.tar.gz:
Publisher:
publish.yml on WilliamSmithEdward/pyOpenVBA
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyopenvba-1.1.1.tar.gz -
Subject digest:
27b8d4b09fc3e1e89e9904d17a767c631e18e30f09f7576548f6f91694c088bc - Sigstore transparency entry: 1606903911
- Sigstore integration time:
-
Permalink:
WilliamSmithEdward/pyOpenVBA@44edb2622c6c5c1f9e0a6810787c292b35f93857 -
Branch / Tag:
refs/tags/v1.1.1 - Owner: https://github.com/WilliamSmithEdward
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@44edb2622c6c5c1f9e0a6810787c292b35f93857 -
Trigger Event:
push
-
Statement type:
File details
Details for the file pyopenvba-1.1.1-py3-none-any.whl.
File metadata
- Download URL: pyopenvba-1.1.1-py3-none-any.whl
- Upload date:
- Size: 50.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fa91770758cb9b342a54b586da795c7386bb945bf27a92beafdf373462e6f3f4
|
|
| MD5 |
93b3f32cdc764ade1e17c13b78e3be32
|
|
| BLAKE2b-256 |
1289ca0b937163e2520a28827dfe47988c24e1de6f54a8659f78658fa7d7e92f
|
Provenance
The following attestation bundles were made for pyopenvba-1.1.1-py3-none-any.whl:
Publisher:
publish.yml on WilliamSmithEdward/pyOpenVBA
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyopenvba-1.1.1-py3-none-any.whl -
Subject digest:
fa91770758cb9b342a54b586da795c7386bb945bf27a92beafdf373462e6f3f4 - Sigstore transparency entry: 1606904013
- Sigstore integration time:
-
Permalink:
WilliamSmithEdward/pyOpenVBA@44edb2622c6c5c1f9e0a6810787c292b35f93857 -
Branch / Tag:
refs/tags/v1.1.1 - Owner: https://github.com/WilliamSmithEdward
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@44edb2622c6c5c1f9e0a6810787c292b35f93857 -
Trigger Event:
push
-
Statement type: