Python for Windows Extensions (via pypiwin32 shim)
pypiwin32 is a legacy shim package that facilitated installation of the `pywin32` project on older Python versions. The core `pywin32` extensions provide comprehensive access to many Windows APIs from Python, including COM automation, Windows services, and GUI programming. While `pypiwin32` itself is no longer actively developed (last released 2018), it still serves as a dependency for projects that might not have updated their requirements. The underlying `pywin32` project is actively maintained, with frequent releases. The current latest version of the actual `pywin32` project is 311, released on July 14, 2025.
Warnings
- breaking `pypiwin32` (version 223) is a legacy package and has effectively been superseded by `pywin32`. Installing `pypiwin32` will pull in `pywin32`, but it's recommended to install `pywin32` directly for current versions and active maintenance.
- gotcha `pywin32` is a Windows-specific library and cannot be installed or run on non-Windows operating systems (like Linux or macOS). Attempts to install it elsewhere will fail.
- gotcha After installing `pywin32` (or indirectly `pypiwin32`), a post-installation script (`pywin32_postinstall.py`) might need to be run from an administrator command prompt to correctly register COM objects and DLLs, especially for global Python installations. This script should generally NOT be run inside virtual environments.
- gotcha Older versions of `pypiwin32` (like 223) may contain Python 2 syntax (e.g., `print "string"`) in their `setup.py` or other scripts. If pip attempts to build one of these old cached versions in a Python 3 environment, it will result in a `SyntaxError`.
- gotcha The `pywin32` project uses a simple incremental version numbering scheme, and any increase in the version number may correspond to a breaking interface change.
Install
-
pip install pypiwin32 -
pip install pywin32
Imports
- win32api
import win32api
- win32con
import win32con
- win32gui
import win32gui
- win32com.client
import win32com.client
- pywintypes
import pywintypes
Quickstart
import win32com.client
import os
# This example demonstrates interacting with Microsoft Excel via COM.
# Ensure Excel is installed on the system.
try:
# Attempt to get an existing Excel application
excel = win32com.client.GetActiveObject("Excel.Application")
except:
# If no active Excel instance, create a new one
excel = win32com.client.Dispatch("Excel.Application")
excel.Visible = True
# Create a new workbook or open an existing one
workbook = excel.Workbooks.Add() # Creates a new workbook
# Access the first sheet
sheet = workbook.Sheets(1)
sheet.Name = "PyWin32_Example"
# Write some data to a cell
sheet.Cells(1, 1).Value = "Hello from pywin32!"
sheet.Cells(2, 1).Value = f"Current user: {os.environ.get('USERNAME', 'Unknown')}"
print("Excel automation complete. Check your Excel window.")
# To close Excel cleanly (optional, uncomment if desired)
# workbook.Close(SaveChanges=False)
# excel.Quit()
# del excel
# del workbook
# del sheet