Xlwings

Posted onby admin
  1. Xlwings64
  2. Xlwings Github
  3. Xlwings Documentation
  4. Xlwings
  5. Xlwings Python
  6. Xlwings32

Xlwings comes pre-installed with. Anaconda (Windows and macOS); WinPython (Windows only) Make sure not to take the dot version as this only contains Python.; If you are new to Python or have trouble installing xlwings, one of these distributions is highly recommended. As I wrote in the edits of the op, to edit existing excel documents you must use the xlutils module (Thanks Oliver). Here is the proper way to do it: #xlrd, xlutils and xlwt modules need to be installed. Xlwings is a BSD-licensed 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).

Introduction

I have written severaltimes about the usefulness of pandas as a data manipulation/wranglingtool and how it can be used to efficiently move data to and from Excel.There are cases, however, where you need an interactive environment for data analysisand trying to pull that together in pure python, in a user-friendly manner would be difficult.This article will discuss how to use xlwings to tie Excel, Python and pandas togetherto build a data analysis tool that pulls information from an external database,manipulates it and presents it to the user in a familiar spreadsheet format.

A Quick Excel Automation Intro

Excel supports several automation options using VBA. User Defined Functions (UDF)are relatively simple in that they take inputs and returns a single value.The more powerful option is a macro (or procedure) that can automate just about anythingExcel can do.

Despite the fact that UDF’s and macros are powerful, they are still written in VBA and thereare times when it would be useful to bring the power of python to our Excel-basedsolution. That’s where xlwings comes into play. At the simplest level, xlwings allowsus to glue python and Excel together in two main ways:

  • Control Excel from python
  • Call custom python code from within Excel

This article will focus on building an Excel worksheet that calls your custom python code.

The Problem

For this example, we are going to develop a simple modeling application thatwill allow someone to enter an account number and date range then return somesummarized sales information that has been transformed via pandas. The solution issimple but shows the power of this combination and how easily you could performmore complex data analysis.

Here’s a diagram of what we are trying to do:

The example shown below could easily be expanded to query multiple databases or interactwith any kind of file that python can read (CSV, Excel, json, etc.)

Setting Up The Environment

For the purposes of this article, I will assume you are running the applicationon a Windows-based system. I highly recommend you use anaconda (or miniconda)as your distro of choice.

Xlwings

Xlwings64

The first thing we need to do is install xlwings (assuming python+pandas are already installed):

xlwings is being constantly updated. This code is based on version 0.7.1.
Python

Xlwings Github

There is a nice xlwings helper function called quickstart which will create a sample Excel fileand stub python file for you.

If you look in the newly created pbp_proj directory, you’ll see two files:

The python file is empty and the Excel file looks empty but there has been somebehind the scenes work done to make the excel to python interface easier for you.

To see what is put into the Excel file, open your newly created file in Excel andgo into Developer -> Visual Basic and you should see something like this:

You will notice that there are two modules - xlwings and Module1. The xlwingsmodule includes all the VBA code to make your custom code work. For the mostpart you should leave that alone. However, if you have issues with your configuration(like you can’t find python) then you can update the config information in this section.

The Module1 will have some default code that looks like this:

We will modify that in a moment to call our custom code. First, I want to create theExcel input fields.

Xlwings Documentation

For this application, we are going to allow the user to enter an accountnumber, start date and end date and will manipulate the sales date based on these inputs.

Here is the simple spreadsheet:

I have only made some minor formatting changes, there are no formulas in the cells.Be sure to save the changes to the Excel file.

For the next step, I’m going to create a short python function that illustrateshow to read data from Excel and write it back. I will be saving this in theempty file called pbp_proj.py

The program is simple and not very useful at this point. I think it is easierto develop a skeleton program in order to make sure all the “plumbing” is in place.The key thing to remember is that the file is called pbp_proj.py and thefunction is called summarize_sales.

Xlwings

To wire this all together, we need to define an Excel procedure to run our code:

The code is really concise just import the module and execute the function:

The final piece is to add a button to our sheet and assign it to theprocedure/macro RetrieveSales.

Once you have that in place, you should be able to press the button and see something like this:

The basic process is in place. We can read from Excel into a python program and usethat to output data back into Excel. Now, let’s make this a little more useful.

Reading From a Database

For this example, I’m going to use sqlalchemy to query a small sqlite db and read thatquery directly into a pandas dataframe. The nice thing about this approach is that ifyou decide that you want to query another database, you can just change the slqlalchemyengine and keep the rest of your code the same. For reference, the xlwings siteshows another example that should be helpful as a further reference.

Before proceeding with the code, make sure sqlalchemy is installed:

Here is how to connect to the sqlite engine by using the full path to the database:

Now that we have the engine, we can construct and execute the query and read theresults into a dataframe:

Once we have the data in the sales_data dataframe, we can do anything we want with it.For the sake of simplicity, I will do a simple groupby then a sum of the total spend:

Fortunately xlwings “understands” a pandas dataframe so placing the value back inthe Excel sheet is straightforward:

That completes the round trip of data from Excel -> Python -> Excel.

Full Program

Here is the fully functioning code included in pbp_proj.py

Here is a sample result:

Xlwings Python

All of the data, including the sqlite db is in my github repo.

Summary

Xlwings32

Xlwings

xlwings provides a useful capability to interact seamlessly with Excel from python.By using this code, you can easily build interactive tools for yourself orfor less technical users that pull data from multiple sources and analyze it inthe very familiar Excel environment. Once the structure is set up, it is really usefulto put all your complex logic and data analysis in the python file and harness allthe tools available in the python ecosystem. I hope that once you start to playwith this, you will find lots of opportunities to use this approach to bring pythonsolutions to some of your less technical users who are stuck using Excel as theironly tool for data analysis.

Comments