How to Easily Automate Excel Spreadsheets With Python and OpenPyXL
By: Ryan Flynn
Tags: Data Science, Automation, Excel, Python, Programming

Have you ever found yourself creating the same report in excel over, and over, and over again? If so, you’re in luck because with a little bit of programming knowledge you can easily automate those nasty reports away. You may be thinking “I would love to do that Ryan! But alas, I am not a programmer.” Well the good news is that automating an excel sheet is much easier than you think. Of course it does require a little bit of Python knowledge, but it is well worth it to learn the basics of Python so that you can utilize the easy to learn OpenPyXL. In this tutorial I will be walking you through the basics of OpenPyXL, so that you can automate your problems away with just a few lines of code.

DISCLAIMER: This tutorial will require some very basic Python and or programming knowledge. Therefore, I suggest either looking for a Python tutorial on YouTube, or going through the free Python courses on something like Codecademy. If you don’t feel like doing that then feel free to try the tutorial out anyway.

Step 1: Installing OpenPyXL

In order to install OpenPyXL you will need two things. The first is that you will need to install Python3, and the second is that you will need to have some sort of IDE such as Visual Studio. After that is all said and done, we’re going to need to open up our terminal and type pip3 install openpyxl . If you are on Windows you will need to type pip install openpyxl .

Step 2: Loading in a Workbook

In order to manipulate an excel workbook we will first need to create a new Python file for our script. In that Python file we will need to import some things from OpenPyXL, by doing the following:

This lets us load in our workbooks from excel and manipulate them. After that’s done you will want to load in your excel workbook by calling load_workbook on it, and storing it in a variable. Now we want to grab the active worksheet in that workbook, by calling .active on our workbook variable.

Step 3: Grabbing Columns and Cells

Now that we have our worksheet ready to go we will want to start manipulating it. The first thing we should do is figure out how to grab a column. We can do this by calling our worksheet object and putting in the column letter we want to grab like this:

We can also grab an individual cell by pairing that column letter with it’s row number like so which will grab a cell in column A row 2:

Step 4: Iterating through Rows and Columns

Now that we have a basic idea of how OpenPyXL works we can use loops to iterate through rows and columns and manipulate the data within them. OpenPyXL has its own iterating functions to make things a little easier for us. The first one is iter_rows which, you guessed it (it’s okay if you didn’t) lets us iterate through rows. It has 5 parameters, the first two being the row you want to start at, and the row you want to end at. The next two are the column you want to start at, and the column you want to end at. And the last parameter determines whether or not only cell values are returned. The second way to iterate is through columns with iter_cols which works the exact same way except you’re going by columns and not rows. Here you can see me using both to print certain cells in my worksheet:

Step 5: Save Your Workbook

With iteration you can do so many awesome things with your excel worksheets, but it wouldn’t mean anything if you couldn’t save them. To save your workbook you can do the following:

Conclusion

And that’s it! Congratulations! You now have all that you need to start experimenting with your excel spreadsheets. All you need to do after you write your script is to run your code. Once you hit enter, your spreadsheet will be populated with all your changes in just the blink of an eye. Much better than spending hours looking over rows and rows of data. Hope this helps!

Sources:

https://openpyxl.readthedocs.io/en/stable/index.html