Automate Excel Like a Pro: Mastering xlwings

NugrohoW
5 min readMar 15, 2024

--

Say goodbye to the tedious task of copying and pasting and hello to Excel automation! xlwings eliminates the need for endless clicks and repetitive chores by utilizing Python’s magic to manage Excel. We’re talking about data manipulation, report production, and analysis on autopilot. This guide will provide you with everything you need to activate xlwings and take your Excel game to the next level. Buckle up, because things are about to get thrilling (and far less time-consuming)!

This tutorial will teach you how to install Python with xlwings, converting your Excel process from manual labor to miraculous automation. Get ready for:

  • Say goodbye to countless clicks by automating repetitive operations, freeing up your time for more strategic work.
  • Embrace Pythonic Power: Use Python to manipulate data, analyze it, and generate reports all within Excel!
  • Improve your Excel skills: Become a spreadsheet ninja with the combined power of Python and Excel.

So, grab a cup of coffee (or your favorite beverage), and let’s dive into the world of xlwings automation!

Part 1: Installing Python

First things first, we need to install Python, the programming language that will become your automation mastermind. Here’s how:

  1. Head over to the official Python website: https://www.python.org/downloads/
  2. Download the latest version of Python that matches your operating system (Windows, Mac, or Linux). Pro Tip: Make sure to check the “Add Python 3.x to PATH” option during installation for easier access later.
  3. Run the downloaded installer and follow the on-screen instructions. It’s usually a straightforward process!

Part 2: Installing xlwings

With Python in place, let’s install xlwings, the bridge that connects Python to your Excel world. There are two main ways to achieve this:

Method 1: Using pip (Python’s package manager)

  1. Open a command prompt or terminal window. You can usually find this by searching for “cmd” or “terminal” in your start menu.
  2. Type the following command and press Enter:
pip install xlwings

This will download and install xlwings, along with any necessary dependencies.

Method 2: Using conda (package manager for scientific Python)

  • If you already have conda installed, open a terminal window.
  • Type the following command and press Enter:
conda install xlwings

This will install xlwings through the conda-forge channel.

Part 3: Insert xlwings in Excel

To activate xlwings in Excel, we may use the following command:

xlwings addin install

To see if the xlwings are already installed in Excel, look on the ribbon:

Menu Ribbon

Congratulations! You’ve successfully installed both Python and xlwings. Now you’re ready to explore the exciting world of Excel automation. Stay tuned for the next part, where we’ll guide you through activating xlwings and unleashing its power!

Part 4: Activate xlwings

Our next step is to activate xlwings in excel, including inserting reference in excel development mode.

First and foremost, we may make an excel file and also write Python files in the same folder. Let’s assume we generate book.xlsx and script.py files in the same folder.

Files Folder

To add xlwings references, go to the Excel developer menu and select “Tools” > “References”.

Developer Window
Popup Menu from References

After adding the reference, you should be told that the reference to xlwings has been added to the “Project” sidebar.

Developer Window

Done! now our Excel and Python are ready to collaborate.

Part 5: Loading data example

I’ll show you how to utilize xlwings in Excel by loading some csv data and then putting it in any sheet within.

Write this Python script to read and load data into sheet1.

import xlwings as xw
import pandas as pd
import os

def load():

folder = f'D:\Data'
df = pd.read_csv(os.path.join(folder, 'netflix_data.csv'), index_col=0)
wb = xw.books.active
sheet = wb.sheets[0]
sheet['A1'].value = df

Also, write a VBA script in Excel Developer mode to call the function.

Visual Basic
Sub load_data()
RunPython "import script; script.load()"
End Sub

Using Alt-F8 on excel files, you can find the function you’ve defined.

Done! Now, when we execute the load_data method, the data will load beautifully onto sheet1.

Final Result

Conclusion

Congratulations! You’ve made your first steps into the wonderful world of Excel automation with xlwings. You’ve now successfully installed Python and xlwings, investigated activation techniques, and gained a basic grasp of how to interface with Excel from Python.

This is only the beginning! The potential for automation using xlwings is immense. Here are a few short suggestions to get you started:

  • Automate repetitive operations and eliminate tedious data entering and formatting. Use Python to automate these tasks, freeing up your time for more strategic research.
  • Data manipulation on steroids. Use Python’s sophisticated data analysis tools, such as NumPy and Pandas, to crunch statistics, clean data sets, and produce informative reports, all from the familiar Excel interface.
  • Chart creation using autopilot: Create professional-looking charts and graphs automatically from your data, eliminating the need for human manipulation.
  • User-defined functions: Extend Excel’s capability by creating custom Python functions that can be invoked straight from spreadsheets.

Remember, the xlwings documentation (https://docs.xlwings.org/) is a treasure resource for additional investigation. There is a variety of knowledge available to help you master xlwings and maximize its potential.

As you go deeper, don’t be afraid to experiment and explore. Python and xlwings are attractive because of its versatility and wide range of capabilities. So, start your Python code, open your Excel file, and prepare to witness the power of automation!

--

--