Skip to main content

aioopenpyxl

CI PyPI Downloads Python versions License: MIT

Asynchronous wrapper around openpyxl with the complete openpyxl API surface in its default thread mode, built on anyio so it works on asyncio and trio alike. openpyxl is synchronous, CPU-bound and not thread-safe: calling it on the event loop stalls every other request, and merely moving it to a thread lets a second coroutine corrupt the file silently. By default, aioopenpyxl runs blocking operations in worker threads, serialises all work on one workbook, and makes synchronous access to a workbook a thread currently owns raise instead of racing. It is an independent project, not affiliated with or endorsed by openpyxl; please report problems with this wrapper here.

For CPU-heavy work that must not share the server's GIL, opt into process=True or use the explicit ProcessPool API below.

Install

uv add aioopenpyxl   # or: pip install aioopenpyxl

Process execution is available starting in 0.2.0. Upgrade an existing installation with uv add 'aioopenpyxl>=0.2.0' or pip install --upgrade aioopenpyxl.

Python 3.10 or newer (including the free-threaded 3.14t build); depends on anyio, openpyxl, types-openpyxl, cloudpickle and typing_extensions.

Quick start

import aioopenpyxl

async with aioopenpyxl.load_workbook("data.xlsx", read_only=True) as wb:  # read, off the loop
    ws = wb.active
    async for row in ws.iter_rows(values_only=True):
        ...

wb = aioopenpyxl.Workbook()  # accepts openpyxl's write_only=... and iso_dates=... arguments
ws = wb.active
ws["A1"] = "header"  # in-memory: forwarded directly, no thread hop
await ws.append_rows(rows)  # whole batch in one thread hop
await wb.save("output.xlsx")  # off the event loop


def fill(raw_ws):  # bulk in-memory work: plain openpyxl code, one hop, workbook locked meanwhile
    for i, item in enumerate(items, start=2):
        raw_ws.cell(i, 1, item.name)


await ws.run(fill)

Wrap an openpyxl workbook you already have with aioopenpyxl.Workbook.wrap(existing_workbook). Workbook is a real class: isinstance, annotations and subclassing work as with openpyxl.Workbook, and a subclass can keep attributes of its own.

Process execution

Mode Choose it when How to edit
Default Workbook() / load_workbook(...) You want the existing synchronous cell API and async I/O Direct cell access or run; blocking operations use threads
process=True You want to keep one live workbook in a separate interpreter/GIL All sheet/cell operations inside await wb.run(fn)
ProcessPool(max_workers=N) You want complete Excel jobs to share a bounded pool An importable synchronous job creates/loads, edits and saves the workbook

Select a persistent child process with Workbook(process=True) or load_workbook(..., process=True); keep cell/sheet operations inside the existing run API:

def edit(raw_wb):  # plain synchronous openpyxl code; local functions/lambdas also work
    ws = raw_wb["Data"]
    ws["A1"] = "updated"


async with aioopenpyxl.load_workbook("input.xlsx", process=True) as wb:
    await wb.run(edit)
    titles = await wb.run(lambda raw: raw.sheetnames)
    await wb.save("output.xlsx")

async with aioopenpyxl.Workbook(process=True) as wb:
    await wb.run(lambda raw: raw.create_sheet("Report").append(["item", 7]))
    xlsx = await wb.to_bytes()

The workbook stays in one worker for its lifetime. Synchronous access such as wb.active, wb["Data"] or wb.wrapped raises BlockingCallError: move it inside run so the event loop never waits synchronously for a process response. Captured values and results must be serializable; results are copies. await wb.close() / context exit joins the worker and ends the workbook's lifetime. See process workbooks.

Shared pool for complete jobs

Use async with ProcessPool(max_workers=2) as pool: and await pool.run(export, rows) to share a fixed number of workers across complete jobs. export must be an importable, module-level synchronous function that creates/loads, edits and saves the workbook, with picklable arguments/results. At most max_workers jobs are admitted at once (default 1). Running jobs finish before cancellation or shutdown completes. See process pool jobs for a complete FastAPI example and the spawn requirements (including a guarded if __name__ == "__main__": entry point for scripts using either process API). The default workbook mode continues to use threads.

What runs where

Rows without an explicit process option describe the default thread mode.

Operation Where Why
Workbook(process=True) / load_workbook(..., process=True): run, save, to_bytes, close same worker process for the workbook's lifetime independent interpreter/GIL; cell operations belong inside run
await pool.run(fn, *args, **kwargs) inside async with ProcessPool(...) worker process complete jobs with an independent interpreter/GIL
await load_workbook(...), await load_workbook_bytes(data), await wb.save(...), await wb.to_bytes(), await wb.close() worker thread real I/O, XML parsing / writing
async for row in ws.iter_rows(..., chunk_size=1024, prefetch=0), ws.rows, ws.values, ws.columns, async for row in ws worker thread, one hop per chunk_size rows; with prefetch=N the thread parses N chunks ahead read-only sheets parse XML lazily
await ws.read_rows(...), await ws.fetch(key), await ws.calculate_dimension() worker thread, one hop coarse-grained reads
await ws.append_rows(rows) worker thread, one hop bulk insert
await ws.append(row) inline for regular sheets, thread for write-only sheets write-only sheets spool to a temp file
await wb.run(fn), await ws.run(fn) worker thread, one hop your bulk in-memory work
ws["A1"], ws.cell(), ws.title, ws.merge_cells(), ws.insert_rows(), wb.create_sheet(), styles, dimensions ... inline on the event loop in-memory operations
ws["A1"], ws.cell() on a read-only sheet raises BlockingCallError openpyxl would parse XML on the event loop; use fetch / read_rows / run

Keep chunk_size at 256 or more (each chunk is one thread round trip); for large exports use Workbook(write_only=True) with append_rows. The internals are explained in docs/design.md.

Rules of the road (default thread mode)

  • A thread owns the workbook while an await is pending. Synchronous access meanwhile (ws.cell(...), wb.sheetnames) raises WorkbookBusyError; await first or move the work into run(...).
  • Inside a prefetch>0 loop, never await the same workbook. The loop holds the semaphore, so await ws.run(...) or a second async for from the body hangs rather than erroring. Use contextlib.aclosing(...) around the iterator when you may leave such a loop early.
  • Read-only sheets have no synchronous cell access. ws["A1"] / ws.cell() raise BlockingCallError; use await ws.fetch(key), await ws.read_rows(...) or run.
  • Cancellation waits for the thread, including native Task.cancel() / asyncio.timeout; it takes effect at the next checkpoint, and async with always closes the workbook.
  • import aioopenpyxl as openpyxl keeps the helpers synchronous. aioopenpyxl.styles, aioopenpyxl.workbook.Workbook ... are openpyxl's own modules; only the top-level Workbook, load_workbook and open are asynchronous.
  • The guard covers the wrappers, not the objects they hand out. A Cell from ws["A1"], a Font, a ColumnDimension or cell.parent is a plain openpyxl object; mutating it while an await on the same workbook is pending races with the thread. Finish or await the operation first, or do such edits inside run(...).
  • wb.wrapped / ws.wrapped are unguarded: the raw openpyxl objects, no busy check. Keep blocking calls on them inside run(...).

Differences from openpyxl

openpyxl aioopenpyxl
load_workbook(...) await load_workbook(...) or async with load_workbook(...) as wb
wb.save(f), wb.close() await wb.save(f), await wb.to_bytes(), await wb.close()
for row in ws.iter_rows() / ws.rows / ws.values / for row in ws async for versions (chunked); iter_rows / iter_cols gain chunk_size= and prefetch=
ws.append(row) await ws.append(row); plus await ws.append_rows(rows)
ws["A1"] on a read-only sheet BlockingCallError; use await ws.fetch("A1")
– await ws.read_rows(...), await x.run(fn), x.wrapped, limiter=

In the default thread mode, every other public member is forwarded with its typed openpyxl signature. Workbook and AsyncWorksheet are generic in the kind of worksheet (Workbook() is a Workbook[Worksheet], load_workbook(..., read_only=True) yields a Workbook[ReadOnlyWorksheet]), so ws.run(fn) accepts a callback written for the concrete openpyxl class. The full table and the typing, sub-module and subclass rules are in docs/compatibility.md.

Development

uv sync && uv run pytest -q                                 # both backends, random order
uv run ruff check . && uv run ruff format --check .
uv run ty check src tests && uv run mypy
uv run python tools/gen_proxies.py && uv run ruff format src/aioopenpyxl/   # after an openpyxl / stubs bump

See CONTRIBUTING.md for the workflow and CHANGELOG.md for history.

Acknowledgements

All the real work is done by openpyxl (MIT), which this package depends on rather than vendors. The typed forwarders and .pyi shims are generated from the types-openpyxl stubs maintained in typeshed (Apache-2.0); concurrency primitives come from anyio (MIT).

License

MIT. openpyxl, typeshed and anyio remain under their own licenses.

Metadata

Release files for aioopenpyxl 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aioopenpyxl 0.2.0
File Size Uploaded Provenance Actions
aioopenpyxl-0.2.0.tar.gz 50.1 kB With provenance Details for aioopenpyxl-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for aioopenpyxl 0.2.0
File Interpreter ABI Platform Actions
aioopenpyxl-0.2.0-py3-none-any.whl Python 3 none any Details for aioopenpyxl-0.2.0-py3-none-any.whl

Total release size: 158.3 kB

Release files / aioopenpyxl-0.2.0.tar.gz

Download URL aioopenpyxl-0.2.0.tar.gz
Size 50.1 kB
Tags Source
SHA-256 checksum
How to use checksums
150f4d95e086803ce412941ed6b3236fef1578e9f18169060ae7d40ae15a5e6a
BLAKE2b-256 checksum
How to use checksums
275112c6d1bdd0fa120f39829a712d53ff313a5629c22422202f21f5125126b0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 18, 2026.

Transparency log

Release files / aioopenpyxl-0.2.0-py3-none-any.whl

Download URL aioopenpyxl-0.2.0-py3-none-any.whl
Size 108.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9a64a517118cb80f93ac7d5a98db00a91683276390b33a94cb51b3d71ec26d0e
BLAKE2b-256 checksum
How to use checksums
310388bf933c22dd0deabced20c0c791dfd560916d02a65a6090ef1d1f810fd4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 18, 2026.

Transparency log

Security for aioopenpyxl 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for aioopenpyxl-0.2.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 18, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page