Skip to main content

Make Excel fly: Interact with Excel from Python and vice versa.

Project description

xlwings is a BSD-licensed Python library that makes it easy to call Python from Excel and vice versa:

  • Interact with Excel from Python using a syntax that is close to VBA yet Pythonic.

  • Replace your VBA macros with Python code and still pass around your workbooks as easy as before.

xlwings fully supports NumPy arrays and Pandas DataFrames. It works with Microsoft Excel on Windows and Mac.

Interact with Excel from Python

Writing/reading values to/from Excel and adding a chart is as easy as:

>>> from xlwings import Workbook, Range, Chart
>>> wb = Workbook()  # Creates a connection with a new workbook
>>> Range('A1').value = ['Foo 1', 'Foo 2', 'Foo 3', 'Foo 4']
>>> Range('A2').value = [10, 20, 30, 40]
>>> Range('A1').table.value  # Read the whole table back
[[u'Foo 1', u'Foo 2', u'Foo 3', u'Foo 4'], [10.0, 20.0, 30.0, 40.0]]
>>> chart = Chart().add(source_data=Range('A1').table)

The Range object as used above will refer to the active sheet. Include the Sheet name like this:

Range('Sheet1', 'A1').value

Qualify the Workbook additionally like this:

wb.range('Sheet1', 'A1').value

The good news is that these commands also work seamlessly with NumPy arrays and Pandas DataFrames.

Call Python from Excel (Windows only)

This functionality is currently only available on Windows: If, for example, you want to fill your spreadsheet with standard normally distributed random numbers, your VBA code is just one line:

Sub RandomNumbers()
    RunPython ("import mymodule; mymodule.rand_numbers()")
End Sub

This essentially hands over control to mymodule.py:

import numpy as np
from xlwings import Workbook, Range

wb = Workbook()  # Creates a reference to the calling Excel file

def rand_numbers():
    """ produces standard normally distributed random numbers with shape (n,n)"""
    n = Range('Sheet1', 'B1').value  # Write desired dimensions into Cell B1
    rand_num = np.random.randn(n, n)
    Range('Sheet1', 'C3').value = rand_num

To make this run, just import de VBA module xlwings.bas in the VBA editor (Open the VBA editor with Alt-F11, then go to File > Import File... and import the xlwings.bas file. ). It can be found in the directory of your xlwings installation.

Easy deployment

Deployment is really the part where xlwings shines:

  • Just zip-up your Spreadsheet with your Python code and send it around. The receiver only needs to have an installation of Python with xlwings (and obviously all the other packages you’re using).

  • There is no need to install any Excel add-in.

  • If this still sounds too complicated, just freeze your Python code into an executable and use RunFrozenPython instead of RunPython. This gives you a standalone version of your Spreadsheet tool without any dependencies.

Installation

The easiest way to install xlwings is via pip:

pip install xlwings

Alternatively it can be installed from source. From within the xlwings directory, execute:

python setup.py install

Dependencies

  • Windows: pywin32

  • Mac: psutil, appscript

Note that on Mac, the dependencies are automatically being handled if xlwings is installed with pip. However, the Xcode command line tools need to be available.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xlwings-0.2.0.tar.gz (439.4 kB view details)

Uploaded Source

File details

Details for the file xlwings-0.2.0.tar.gz.

File metadata

  • Download URL: xlwings-0.2.0.tar.gz
  • Upload date:
  • Size: 439.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for xlwings-0.2.0.tar.gz
Algorithm Hash digest
SHA256 8508851dd4968b56612235dbe2829cf11b47068fbc076bed1dcc8ad0be141340
MD5 9bfb31b3a08431705eec1bc262d27ff5
BLAKE2b-256 a975e9478654f0f59574de40e9290e5818df0ae9aac1f19d9e7acd77dc004ddd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page