xlwings
xlwings is a Python library that makes it easy to call Python from Excel and vice versa. It works with Excel on Windows and macOS as well as with Google Sheets and Excel on the web.
Top Related Projects
Please use openpyxl where you can...
Library to create spreadsheet files compatible with MS Excel 97/2000/XP/2003 XLS files, on any platform.
Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more
Python for Windows (pywin32) Extensions
Quick Overview
xlwings is a Python library that makes it easy to automate Excel using Python. It allows users to read, write, and manipulate Excel files from Python, as well as call Python functions from Excel. xlwings works with both Windows and macOS, supporting various Excel versions.
Pros
- Seamless integration between Python and Excel
- Cross-platform compatibility (Windows and macOS)
- Supports both .xls and .xlsx file formats
- Allows calling Python functions directly from Excel
Cons
- Requires Excel to be installed for full functionality
- Performance can be slower compared to pure Python libraries like openpyxl
- Limited support for Excel's more advanced features
- Learning curve for users new to Python-Excel integration
Code Examples
- Reading data from Excel:
import xlwings as xw
wb = xw.Book('example.xlsx')
sheet = wb.sheets['Sheet1']
data = sheet.range('A1:B5').value
print(data)
- Writing data to Excel:
import xlwings as xw
wb = xw.Book()
sheet = wb.sheets['Sheet1']
sheet.range('A1').value = [['Name', 'Age'], ['Alice', 30], ['Bob', 25]]
wb.save('output.xlsx')
- Calling a Python function from Excel:
import xlwings as xw
@xw.func
def double_value(x):
return x * 2
# Use this function in Excel as =PYTHON.DOUBLE_VALUE(A1)
Getting Started
To get started with xlwings, follow these steps:
-
Install xlwings using pip:
pip install xlwings
-
Import xlwings in your Python script:
import xlwings as xw
-
Create a new Excel workbook or open an existing one:
wb = xw.Book() # New workbook # OR wb = xw.Book('existing_file.xlsx') # Existing workbook
-
Access sheets and ranges:
sheet = wb.sheets['Sheet1'] data = sheet.range('A1:B5').value
-
Manipulate data and save changes:
sheet.range('C1').value = 'New Data' wb.save('updated_file.xlsx')
Competitor Comparisons
Please use openpyxl where you can...
Pros of xlrd
- Lightweight and focused solely on reading Excel files
- Supports older Excel file formats (including .xls)
- Generally faster for simple read operations
Cons of xlrd
- Limited to read-only operations
- No support for writing or modifying Excel files
- Less actively maintained compared to xlwings
Code Comparison
xlrd:
import xlrd
workbook = xlrd.open_workbook('example.xls')
sheet = workbook.sheet_by_index(0)
value = sheet.cell_value(0, 0)
xlwings:
import xlwings as xw
wb = xw.Book('example.xlsx')
sheet = wb.sheets[0]
value = sheet.range('A1').value
Both xlrd and xlwings provide ways to read Excel files, but xlwings offers a more comprehensive set of features for interacting with Excel, including writing and modifying files. xlrd is simpler and more focused on reading, while xlwings provides a broader range of functionality and better integration with Excel applications.
Library to create spreadsheet files compatible with MS Excel 97/2000/XP/2003 XLS files, on any platform.
Pros of xlwt
- Lightweight and focused solely on writing Excel files
- Simple and straightforward API for basic Excel operations
- No external dependencies, making it easy to install and use
Cons of xlwt
- Limited to creating older .xls file format (Excel 97-2003)
- Lacks advanced features and interactivity with Excel applications
- No longer actively maintained (last update in 2016)
Code Comparison
xlwt:
import xlwt
workbook = xlwt.Workbook()
sheet = workbook.add_sheet('Sheet1')
sheet.write(0, 0, 'Hello, World!')
workbook.save('example.xls')
xlwings:
import xlwings as xw
wb = xw.Book()
sheet = wb.sheets['Sheet1']
sheet.range('A1').value = 'Hello, World!'
wb.save('example.xlsx')
Both libraries allow for basic Excel file creation and data writing. However, xlwings offers more advanced features, including the ability to interact with running Excel applications and support for newer Excel file formats. xlwt is simpler but limited to older file formats and lacks active development.
Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more
Pros of pandas
- Powerful data manipulation and analysis capabilities
- Extensive documentation and large community support
- Seamless integration with other data science libraries
Cons of pandas
- Steeper learning curve for beginners
- Higher memory usage for large datasets
- Limited Excel interaction capabilities
Code Comparison
xlwings:
import xlwings as xw
wb = xw.Book('example.xlsx')
sheet = wb.sheets['Sheet1']
data = sheet.range('A1:C10').value
sheet.range('D1').value = 'Result'
pandas:
import pandas as pd
df = pd.read_excel('example.xlsx', sheet_name='Sheet1')
result = df.groupby('Column1')['Column2'].sum()
df['Result'] = result
df.to_excel('example.xlsx', sheet_name='Sheet1', index=False)
xlwings focuses on direct Excel interaction, allowing for real-time manipulation of workbooks. pandas excels in data analysis and transformation, with Excel files serving as input/output sources rather than live documents. xlwings is more suitable for Excel automation tasks, while pandas is better for complex data operations and analysis workflows.
Python for Windows (pywin32) Extensions
Pros of pywin32
- Broader Windows API coverage, allowing access to more Windows-specific functionalities
- More mature and established project with a longer history
- Supports a wider range of Windows versions and Python implementations
Cons of pywin32
- Steeper learning curve due to its lower-level nature and extensive API
- Requires more code to accomplish Excel-specific tasks compared to xlwings
- Less user-friendly documentation for Excel-related operations
Code Comparison
xlwings:
import xlwings as xw
wb = xw.Book('example.xlsx')
sheet = wb.sheets['Sheet1']
sheet.range('A1').value = 'Hello, xlwings!'
pywin32:
import win32com.client
excel = win32com.client.Dispatch('Excel.Application')
wb = excel.Workbooks.Open('example.xlsx')
sheet = wb.Worksheets('Sheet1')
sheet.Range('A1').Value = 'Hello, pywin32!'
Both libraries allow interaction with Excel, but xlwings provides a more intuitive and Pythonic interface for Excel-specific tasks. pywin32 offers broader Windows API access but requires more verbose code for Excel operations. xlwings is better suited for Excel-centric projects, while pywin32 is more versatile for general Windows programming.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
xlwings - Make Excel fly with Python!
xlwings (Open Source)
xlwings is a BSD-licensed <http://opensource.org/licenses/BSD-3-Clause>
_ Python library that makes it easy to call Python from Excel and vice versa:
- Scripting: Automate/interact with Excel from Python using a syntax that is close to VBA.
- Macros: Replace your messy VBA macros with clean and powerful Python code.
- UDFs: Write User Defined Functions (UDFs) in Python (Windows only).
Numpy arrays and Pandas Series/DataFrames are fully supported. xlwings-powered workbooks are easy to distribute and work on Windows and macOS.
xlwings includes all files in the xlwings package except the pro
folder, i.e., the xlwings.pro
subpackage.
xlwings PRO
xlwings PRO offers additional functionality on top of xlwings (Open Source), including:
- xlwings Server: No local Python installation required, supports Excel on the web and Google Sheets in addition to Excel on Windows and macOS. Integrates with VBA, Office Scripts and Office.js and supports custom functions on all platforms. See
GitHub repo <https://github.com/xlwings/xlwings-server>
_ andxlwings Server docs <https://server.xlwings.org/>
_. - xlwings Reports: the flexible, template-based reporting system
- xlwings Reader: A faster and more feature-rich alternative for
pandas.read_excel()
(no Excel installation required) - Easy deployment via 1-click installer and embedded code
- See the
full list of PRO features <https://www.xlwings.org/pricing>
_
xlwings PRO is source available <https://en.wikipedia.org/wiki/Source-available_software>
_ and dual-licensed under one of the following licenses:
PolyForm Noncommercial License 1.0.0 <https://polyformproject.org/licenses/noncommercial/1.0.0>
_ (noncommercial use is free)xlwings PRO License <https://github.com/xlwings/xlwings/blob/main/LICENSE_PRO.txt>
_ (commercial use requires apaid plan <https://www.xlwings.org/pricing>
_)
License Key
To use xlwings PRO, you need to install a license key on a Terminal/Command Prompt like so (alternatively, set the env var XLWINGS_LICENSE_KEY
::
xlwings license update -k YOUR_LICENSE_KEY
See the docs <https://docs.xlwings.org/en/latest/pro/license_key.html>
_ for more details.
License key for noncommercial purpose:
- To use xlwings PRO for free in a noncommercial context, use the following license key:
noncommercial
.
License key for commercial purpose:
- To try xlwings PRO for free in a commercial context, request a trial license key: https://www.xlwings.org/trial
- To use xlwings PRO in a commercial context beyond the trial, you need to enroll in a paid plan (they include additional services like support and the ability to create one-click installers): https://www.xlwings.org/pricing
xlwings PRO licenses are developer licenses, are verified offline (i.e., no telemetry/license server involved) and allow royalty-free deployments to unlimited internal and external end-users and servers for a hassle-free management. Deployments use deploy keys that don't expire but instead are bound to a specific version of xlwings.
Links
- Homepage: https://www.xlwings.org
- Quickstart: https://docs.xlwings.org/en/stable/quickstart.html
- Documentation: https://docs.xlwings.org
- Book (O'Reilly, 2021): https://www.xlwings.org/book
- Video Course: https://training.xlwings.org/p/xlwings
- Source Code: https://github.com/xlwings/xlwings
- xltrail (Version control for Excel files): https://www.xltrail.com
Top Related Projects
Please use openpyxl where you can...
Library to create spreadsheet files compatible with MS Excel 97/2000/XP/2003 XLS files, on any platform.
Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more
Python for Windows (pywin32) Extensions
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot