openpyxl-stubs

0.1.25 · active · verified Sat Apr 11

openpyxl-stubs provides type stubs for the openpyxl library, enabling static type checking with tools like MyPy. The current version is 0.1.25, released in January 2023, and it is currently in an alpha development status. It does not have a defined release cadence, as it is a stub package that augments the `openpyxl` library.

Warnings

Install

Imports

Quickstart

This example demonstrates basic usage of openpyxl with type annotations. When `openpyxl-stubs` is installed, a type checker like MyPy will use its definitions to validate these operations. Note that `wb.active` might require a `# type: ignore` if the type checker has difficulty inferring `Worksheet` due to the dynamic nature of `active` property in some contexts without explicit stubs for that specific assignment, or if the stub is incomplete for that specific case. The actual save operation is commented out for quickstart runnability without file system interaction.

from openpyxl import Workbook
from openpyxl.worksheet.worksheet import Worksheet

# Create a new workbook with type hints
wb: Workbook = Workbook()

# Grab the active worksheet with type hints
ws: Worksheet = wb.active # type: ignore

# Data can be assigned directly to cells
ws['A1'] = 42

# Rows can also be appended
ws.append([1, 2, 3])

# Python types will automatically be converted (example with a datetime object)
import datetime
ws['A2'] = datetime.datetime.now()

# Save the file
# In a real scenario, you'd use a temporary file or a specific path.
# For this example, we'll just show the save operation.
# wb.save("sample.xlsx")
print("Workbook operations with type hints (stubs installed).")

view raw JSON →