xlwings is a BSD-licensed Python library that simplifies interaction with Microsoft Excel, enabling automation and powerful data manipulation. It allows users to call Python from Excel and vice versa, supporting scripting, macros, and User Defined Functions (UDFs) on Windows and macOS. The library is actively maintained with frequent releases, currently at version 0.35.1.
pip install xlwingsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to programmatically control Excel from Python. It shows how to launch Excel in the background, create a new workbook, write and read cell values, and properly close the application. It requires an existing Excel installation.
Replace `@xlwings.sub` with `@xlwings.script` in your Python code.
When using `range.insert()`, provide the `shift` argument, e.g., `sheet.range('A1').insert(shift='down')` or `sheet.range('A1').insert(shift='right')`.Ensure Microsoft Excel is installed on the machine where xlwings scripts are run. Consider xlwings PRO or xlwings Server for Linux or Excel-less environments.
Always place `xw.Book.caller()` directly within the Python function that is invoked by Excel VBA.
If encountering issues with UDFs and Python in Excel, ensure you have the latest xlwings version (0.35.1+) and re-import your UDFs after upgrading.
After upgrading the `xlwings` Python package, run `xlwings addin install` from your command prompt to update the Excel add-in.
Ensure `xlwings` is installed in the Python environment linked by the add-in. Verify the 'PYTHON_WIN' or 'PYTHON_MAC' path in the xlwings add-in settings (Ribbon > xlwings > xlwings Settings) points to the correct `python.exe` or `python` executable.
Access the range's value first before applying string methods: `sheet.range('A1').value.upper()`.Close the target Excel file in any open applications or ensure the script has the necessary read/write permissions for the file and its directory.
Ensure an Excel application is running before attempting to connect to an active instance, or explicitly start a new Excel instance using `xw.App(visible=True)`.
pip install xlwings