Typing Stubs for openpyxl
This is a type stub package for the `openpyxl` library, providing type hints for static analysis by tools like MyPy and Pyright. It aims to offer accurate annotations for specific `openpyxl` versions (e.g., `openpyxl==3.1.5` for `types-openpyxl 3.1.5.20260408`). `types-openpyxl` is part of the larger Typeshed project, which maintains type stubs for numerous Python libraries. It is actively maintained with frequent releases to keep up with `openpyxl` development.
Warnings
- gotcha This package provides *only* type stubs for `openpyxl`, not the `openpyxl` library itself. To use `openpyxl` with type hints, you must install both `openpyxl` and `types-openpyxl`.
- gotcha Type stubs are often tied to specific versions of the underlying library. While typeshed aims for broad compatibility, using `types-openpyxl` with an `openpyxl` version significantly different from the one it was generated for (e.g., `openpyxl==3.1.5` for `types-openpyxl 3.1.5.20260408`) may lead to incorrect type checking results.
- breaking Changes or bug fixes to `openpyxl`'s API (even minor ones) can lead to mismatches with the type stubs, causing type checkers to report errors on valid `openpyxl` code or fail to catch actual type errors. This is particularly relevant if `openpyxl` introduces new features or alters method signatures that are not yet reflected in the stubs.
- gotcha All fixes and updates to the `openpyxl` type stubs should be contributed directly to the `python/typeshed` GitHub repository, not to the `types-openpyxl` PyPI package's maintainers directly. The PyPI package is automatically generated from Typeshed.
Install
-
pip install types-openpyxl openpyxl
Imports
- Workbook
from openpyxl import Workbook
- load_workbook
from openpyxl import load_workbook
- Font
from openpyxl.styles import Font
Quickstart
import datetime
from openpyxl import Workbook
from openpyxl.styles import Font
def create_excel_with_types():
# This code uses openpyxl, and types-openpyxl provides type hints for it.
wb: Workbook = Workbook()
ws = wb.active
ws.title = "Typed Sheet"
# Type checking tools (like MyPy) will use types-openpyxl to validate these operations.
ws['A1'] = "Date"
ws['B1'] = "Value"
ws['A1'].font = Font(bold=True)
ws['B1'].font = Font(bold=True)
ws['A2'] = datetime.date(2026, 4, 9)
ws['B2'] = 123.45
file_path = "typed_sample.xlsx"
wb.save(file_path)
print(f"Excel file '{file_path}' created with type hints verified.")
if __name__ == "__main__":
create_excel_with_types()