Driving Excel itself
xlwings
Reading and writing a live workbook, calling Python from Excel, refreshing pivots.
Start with the full guides
The long-form treatment of xlwings, with the reasoning the shorter pages assume.
Step-by-step how-tos
One task each, with code you can copy, run and adapt.
- How-toControl How xlwings Converts DataFrames and RangesGet the Python values you expect from xlwings: why a range comes back as a scalar, a list or a list of lists, and how ndim, numbers, dates, empty, transpose and expand change that; how the DataFrame converter treats headers and the index on read and write; dictionaries from two-column ranges; and writing a custom converter for your own conventions, with its logic tested without Excel.
- How-toFormat Cells in a Live Workbook with xlwingsStyle a workbook that is open in Excel from Python: fill colours, fonts, number formats and column widths through xlwings' own properties, borders and conditional formats through the .api escape hatch, an Excel table with a built-in style, and the screen-updating and range-at-once habits that keep formatting thousands of cells fast — plus how this differs from formatting a file with openpyxl.
- How-toInsert a matplotlib Plot into a Live Workbook with xlwingsPut a matplotlib chart into a workbook that is open in Excel and keep it up to date: read the data with xlwings, build the figure, add it with sheet.pictures.add, place it next to a cell with anchor, replace it in place on every run with name and update=True, choose PNG resolution or SVG, and size it so it looks right on screen and in print.
- How-toRead and Write a Live Excel Workbook with xlwingsDrive the open application rather than the file: connect to a running workbook, read and write ranges, force a recalculation, read what the formulas produced, and clean up so no invisible Excel is left behind.
- How-toRun Python from an Excel Button with xlwings RunPythonLet Excel users run your Python code with a button — xlwings quickstart, the RunPython VBA call, Book.caller() to reach the calling workbook, passing arguments, configuring the interpreter, testing with a mock caller, and distributing the workbook.
- How-toSpeed Up xlwings with Bulk Reads and WritesMake xlwings scripts fast by moving whole ranges in single calls instead of looping over cells — expand and options for DataFrames, 2D lists, turning off screen updating, events and automatic calculation, and chunksize for very large ranges.
- How-toWork with Named Ranges and Tables in xlwingsAddress data in a live Excel workbook by name instead of by cell address with xlwings: read and write named ranges for inputs and results, list, add and remove names at workbook and sheet scope, read an Excel table into pandas, write a DataFrame back into the table with update() so it resizes, add totals, and create new tables from Python.
- 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-toCall Python from Excel with an xlwings UDFExpose a Python function as a worksheet formula with xlwings — the add-in setup, @xw.func and @xw.arg decorators, array and DataFrame arguments, and the Windows-only limits.
- How-toxlwings: Run a VBA Macro From Python (Example)A complete xlwings example for running a VBA macro from Python: open the workbook, bind the Sub with app.macro() or book.macro(), pass arguments, save, and quit Excel.
Compare the options
Which tool or approach to pick, and why.
- Comparisonxlwings vs pywin32 for Excel AutomationBoth drive the same Excel through the same interface. Compare the code for one task, see what the wrapper adds, and learn when the raw object model is still the better choice.
- ComparisonWhen to Use xlwings Instead of openpyxlxlwings drives a real copy of Excel; openpyxl edits the file. Four jobs justify the cost — recalculation, objects openpyxl drops, live workbooks and native output.
- ComparisonPython in Excel vs openpyxl and xlwingsPython in Excel runs code inside a cell; openpyxl and pandas write files from a script; xlwings drives the Excel application. Compare where each runs, what it can reach and which jobs it suits.
Follow it through a track
Prefer an ordered route? These tracks cover xlwings 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.
xlwings is part of Driving Excel itself.