Python for Windows Extensions (via pypiwin32 shim)

223 · deprecated · verified Sat Apr 11

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

Install

Imports

Quickstart

This quickstart demonstrates how to use the `win32com.client` module to automate Microsoft Excel, a common use case for pywin32. It shows how to dispatch an Excel application object, make it visible, create a new workbook, and write data to a cell. This requires Microsoft Excel to be installed on the Windows system where the code is run.

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

view raw JSON →