xlwings makes it easy to deploy your Python powered Excel tools on Windows. Just zip up your Excel and Python files and send them to your users with the following instructions:
*Note that xlwings works with every Python installation but then you need to manually install pywin32 which can sometimes be a bit troublesome.
On the developer side, xlwings is equally easy:
-
Open the VBA editor (
Alt-F11
), then go toFile > Import File...
and import thexlwings.bas
file. Alternatively, just work off theexample.xlsm
and continue to the next step: -
Call Python from VBA like so:
Sub Example() RunPython ("import example; example.rand_numbers()") End Sub
-
This essentially hands over control to
example.py
:import numpy as np from xlwings import Workbook wb = Workbook() # Creates a reference to the calling Excel file def rand_numbers(): """ produces standard normally distributed random numbers with dim (n,n)""" n = wb.range('Sheet1', 'B1').value rand_num = np.random.randn(n, n) wb.range('Sheet1', 'C3').value = rand_num
xlwings let's you comfortably interact with Excel by calling Workbook()
either with no arguments to work off a new file or with the full path to your Excel file (from your favorite Python environment). Range
can be used as shortcut for wb.range()
. It always refers to the workbook created last. Also, when omitting the sheet name, it refers to the currently active sheet.
>>> from xlwings import Workbook, Range
>>> wb = Workbook(r'C:\full\path\to\file.xlsx') # Use Workbook() for a new file
>>> Range('A1').value = 'Hello xlwings!'
>>> Range('A1').value
u'Hello xlwings!'
BSD (3-clause) license