xlwings

0.35.1 · active · verified Sun Apr 12

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.

Warnings

Install

Imports

Quickstart

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.

import xlwings as xw

# Start an Excel app (visible=False runs it in the background)
# xlwings (Open Source) requires Excel to be installed on the machine.
try:
    app = xw.App(visible=False)
    
    # Open an existing workbook or create a new one
    # wb = app.books.open("path/to/my_workbook.xlsx")
    wb = app.books.add() # Create a new, empty workbook
    sheet = wb.sheets

    # Write a value to cell A1
    sheet.range('A1').value = 'Hello from xlwings!'

    # Read a value from cell A1
    cell_value = sheet.range('A1').value
    print(f"Value in A1: {cell_value}")

    # Write a 2D list to a range (will expand automatically)
    sheet.range('A3').value = [['Header1', 'Header2'],,]

    # Save the workbook (optional)
    # wb.save("my_output.xlsx")
    # print("Workbook saved as my_output.xlsx")

finally:
    # Ensure Excel app is quit even if errors occur
    if 'app' in locals() and app.alive:
        wb.close() # Close the workbook (without saving if not explicitly saved)
        app.quit()
        print("Excel application quit.")

view raw JSON →