comtypes is a lightweight pure Python COM package based on the ctypes foreign function interface library. It allows defining, calling, and implementing custom and dispatch-based COM interfaces in pure Python. It currently requires Windows and Python 3.9 or later, with version 1.4.16 being the latest release. The project has an active release cadence with regular updates and maintenance.
pip install comtypesVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to create and interact with a COM object using `comtypes.client.CreateObject`. It attempts to instantiate the `MSScriptControl.ScriptControl` object, which provides scripting capabilities, and then executes a simple JavaScript expression. Error handling is included for when the COM object might not be available on the system.
Upgrade Python to 3.9+ or pin comtypes version to <=1.4.12 for Python 3.8.
Migrate logging setup to use Python's built-in `logging` module functions and configurations.
Run `python -m comtypes.clear_cache` to delete and regenerate the cached modules. This often resolves such import-related issues.
Ensure the application runs on a Microsoft Windows operating system.
Replace `windll.<function>` with `WinDLL('library').<function>` and `oledll.<function>` with `OleDLL('library').<function>`.Review `comtypes`-generated enumeration types when migrating to Python 3.15+ and consult `comtypes` documentation/issues for potential workarounds or updates (GH-894).