Why run a Python script through Excel? Why bother with the middleman when environments such as Spyder and Jupyter Notebook exists?
Something that I have been learning of late is the importance of diversifying methods of presenting one’s work in the spirit of open science, communicability and inclusion. In that line of thinking, running a Python script via an Excel ‘user interface’ addresses two issues:
- Excel VBA’s slower reading and writing of data
- The steep learning curve associated with learning how to code in Python
In short, executing Python via Excel provides those with sufficient experience in the language with an avenue to efficiently communicate and visualize their data in a way that most people can see and understand. This blog post will demonstrate this point by extracting the mean and standard deviation of the sepal length and width, as well as the petal length and width, of three different iris subspecies available from the famed Iris dataset, that can be found here.
Download the dataset, called ‘iris.data’ into a folder or location of your choice. Next, change the extension of the file to ‘.csv’. Let’s start processing this in Python!
1. Writing the Python script
Here, we will be writing a Python script to generate two files: the means (iris_means.csv
) and standard deviations (iris_std.csv
) of each iris subspecies’ attributes. We will first write the Python script to do so:
import pandas as pd
# the types of data within the iris dataset
col_names = ["sepal_length", "sepal_width", "petal_length", "petal_width", "subspecies"]
iris_data = pd.read_csv('C:/your-preferred-location/iris.csv',
sep=',', header=None, names=col_names, index_col=None)
#%%
iris_setosa = iris_data[iris_data['subspecies']=='Iris-setosa']
iris_setosa=iris_setosa.drop(['subspecies'], axis=1)
iris_versicolor = iris_data[iris_data['subspecies']=='Iris-versicolor']
iris_versicolor=iris_versicolor.drop(['subspecies'], axis=1)
iris_virginica = iris_data[iris_data['subspecies']=='Iris-virginica']
iris_virginica=iris_virginica.drop(['subspecies'], axis=1)
#%%
mean_setosa = iris_setosa.mean(axis=0)
std_setosa = iris_setosa.std(axis=0)
mean_versicolor = iris_versicolor.mean(axis=0)
std_versicolor = iris_versicolor.std(axis=0)
mean_virginica = iris_virginica.mean(axis=0)
std_virginica = iris_virginica.std(axis=0)
subspecies = ['Setosa', 'Versicolor', 'Virginica']
mean_vals = pd.concat([mean_setosa, mean_versicolor, mean_virginica], axis=1)
mean_vals.columns=subspecies
std_vals = pd.concat([std_setosa, std_versicolor, std_virginica], axis=1)
std_vals.columns=subspecies
mean_vals.to_csv('C:/Users/your-preferred-location/iris_means.csv',
sep=',', header=True, index=True)
std_vals.to_csv('C:/Users/your-preferred-location/iris_std.csv',
sep=',', header=True, index=True)
2. Write the Excel VBA macro
We will first set up the Excel document that will execute the Excel macro. Create an Excel worksheet file. Mine is named ‘iris_GUI.xlsx’. Next, navigate to the ‘File’ tab and select ‘Options’. Go to ‘Customize Ribbon’ and make sure that ‘Developer’ is checked:
Click ‘OK’. The developer tab should now be visible in your Toolbar Ribbon:
Let’s get to the macro! Under the Developer tab, identify the ‘Macros’ tool on the far-left side of the toolbar. Select it and give your macro a suitable name. I called mine ‘link_python_excel’.
Once this is done, click ‘Create’. Next, you should see a window like this pop up:
Within the provided space, first initialize the macro using Sub link_python_excel()
. This tells Excel VBA (Excel’s programming language) that you are about to write a macro called ‘link_python_excel’.
Next, declare your macro as an object, and your Python executable and Python script as strings. This is to enable VBA to locate the Python executable and use it to run the script as intended.
Dim objShell As Object
Dim PythonExe, PythonScript As String
You will then want to assign a macro object to its declaration:
Set objShell = VBA.CreateObject("Wscript.shell")
Please do not tamper with the “Wscript.shell” term. This assignment is the portion of the code that enables the macro to interact with Windows PowerShell, thus enabling VBA to execute the Python script. More information on this matter can be found at this website.
Following this, provide the filepath to the Python executable and the Python script:
PythonExe = """C:\Users\lbl59\AppData\Local\Programs\Python\Python39\python.exe"""
PythonScript = "C:\Users\lbl59\Desktop\run_python_in_excel\process_iris_data.py"
Note the use of the triple quotation marks. This method of assigning a string in VBA is used when the string potentially contains spaces. It is generally considered good practice to use “””…””” for file paths.
Finally, run your Python script and activate your workbook. The activation is necessary if you would like to run the script via a button in Excel, which we shall be going through in a bit.
objShell.Run PythonExe & PythonScript
Application.Goto Reference:="link_python_excel"
Finally, don’t forget to end the macro using End Sub
.
Overall, your script should look as such:
Sub link_python_excel()
' link_python_excel Macro
' Declare all variables
Dim objShell As Object
Dim PythonExe, PythonScript As String
'Create a new Shell Object
Set objShell = VBA.CreateObject("Wscript.shell")
'Provide the file path to the Python Exe
PythonExe = """C:\Users\lbl59\AppData\Local\Programs\Python\Python39\python.exe"""
'Provide the file path to the Python script
PythonScript = "C:\Users\lbl59\Desktop\run_python_in_excel\process_iris_data.py"
'Run the Python script
objShell.Run PythonExe & PythonScript
Application.Goto Reference:="link_python_excel"
End Sub
3. Run the Python script for Excel
Save the macro. Note that you will have to save the Excel workbook as a ‘.xlsm’ file to enable macro functionality. Once this is done, navigate to the ‘Developer’ tab and select ‘Insert’ and click on the button icon.
Draw the button the same way you would a rectangular shape. Rename the button, if you so prefer. In this exercise, the button is labeled ‘Run Code’. Next, right click on the button and select ‘Assign Macro’.
Once this is selected, you should be able to see the option to add the ‘link_python_excel’ macro to the button. Select the macro, and you are done! The two output files should have been output into the same location where you stored your iris.csv
dataset.
Summary
In this post, we walked through the steps of writing a Python script to be run using an Excel macro. First, a Python script was written to process the iris dataset and output two files. Next, the Excel macro was written to execute this script. Finally, the macro was assigned to a button in the Excel sheet, where the Python script can be executed when the button is clicked.
Hope you found this useful!
Thank you, it worked.
How to get the output of the macro to be inserted in an Excel cell?
Pingback: 12 Years of WaterProgramming: A Retrospective on >500 Blog Posts – Water Programming: A Collaborative Research Blog