Driving Excel itself
COM & pywin32
win32com on Windows: ranges, refreshes, dialogs and closing Excel cleanly.
Start with the full guides
The long-form treatment of com & pywin32, with the reasoning the shorter pages assume.
Step-by-step how-tos
One task each, with code you can copy, run and adapt.
- How-toTest win32com and xlwings Code Without ExcelUnit-test Python code that drives Excel through win32com or xlwings on machines without Excel — Linux CI runners, Macs, containers: keep the Excel calls in a thin layer, pass the application in, replace it with small fakes that record what was done, check that workbooks are closed and Excel quit even after errors, stub the win32com import for older code, and keep real-Excel tests for Windows only.
- How-toAutomate Excel on macOS with Pythonpywin32 and COM do not exist on a Mac, so automating Excel there takes different tools: openpyxl and pandas when you only need the file, xlwings when Excel itself must do the work, AppleScript through osascript for the odd task xlwings does not cover — plus the macOS permission prompts, the Excel sandbox file-access dialog, and scheduling with launchd.
- How-toClose Excel Cleanly and Avoid Orphan COM ProcessesExcel exits only when every reference is released. Learn the try/finally shape, CoInitialize on worker threads, and how to detect and kill just the instance you started.
- How-toPrint an Excel Workbook from Python with win32comSend workbooks to a printer from a Python script on Windows: prepare the page setup so the printout is right (fit to width, landscape, repeated header rows, print area) with openpyxl or COM, list printers and pick one, call PrintOut for whole workbooks, single sheets or page ranges, print many files in a loop without dialogs, and fall back to LibreOffice or PDF when Excel is not available.
- How-toRead and Write Cell Ranges with win32comMove data between Python and Excel in single calls: block reads, the nested shape writes require, sizing ranges with End(xlUp), and Value versus Value2.
- How-toRefresh Excel Data Connections and Recalculate with win32comRefreshAll returns before the data arrives. Make refreshes synchronous, recalculate with the right level, refresh pivot caches in order, and verify nothing went stale.
- How-toRun Excel COM Automation from Task Scheduler Without a Logged-In UserWhy a pywin32 script that drives Excel works when you run it by hand and fails or hangs from Task Scheduler — session 0, the missing systemprofile Desktop folder, DCOM identity, dialogs nobody can click — and the setups that work, plus how to avoid needing Excel at all.
- How-toSave a Workbook in Another Format with win32comUse Excel itself, through pywin32, to convert workbooks between .xls, .xlsx, .xlsm, .xlsb, CSV, ODS and PDF — the FileFormat codes, SaveAs versus ExportAsFixedFormat, silent overwrites, per-sheet CSV export and folder batch conversion.
- How-toExport an Excel Chart as an Image with PythonTurn a chart inside an Excel workbook into a PNG for an email, a slide or a web page — with Excel itself through COM on Windows, with headless LibreOffice on any server plus a Pillow crop, or by redrawing the chart from the same data with matplotlib — and choose by fidelity, platform and resolution.
- How-toRun an Excel Macro from Python with win32comCall existing VBA from Python with Application.Run: qualified macro names, positional arguments, return values, and staging bulk data so the loop runs inside Excel.
- How-toAutomate LibreOffice Calc from Python with UNOUse LibreOffice as a scriptable spreadsheet engine on any server: start a headless soffice, connect through UNO from Python, open an .xlsx, edit cells, delete rows so that formulas update the way they do in Excel, recalculate, save as .xlsx and export PDF, then shut down cleanly — plus which Python can import uno and how to keep parallel jobs apart.
Compare the options
Which tool or approach to pick, and why.
When it breaks
The errors this topic produces, with the cause explained rather than just the fix.
- FixHandle COM Errors and Excel Dialog Prompts in PythonDecode pywintypes.com_error through excepinfo, recognise the four HRESULTs that matter, suppress every dialog before opening a file, and add a watchdog for the hangs left over.
- FixFix the win32com gen_py AttributeError (CLSIDToClassMap)Excel automation with pywin32 suddenly fails with an AttributeError saying a win32com.gen_py module has no attribute CLSIDToClassMap. The generated type-library cache is broken. Find the gen_py folder, delete just the Excel entry, let EnsureDispatch rebuild it, and stop it happening again on shared and scheduled machines.
Follow it through a track
Prefer an ordered route? These tracks cover com & pywin32 along the way, in the order that builds on itself.
Related topics
Subjects that keep turning up on the same pages as this one, most overlap first.
COM & pywin32 is part of Driving Excel itself.