
- Learn basics of OpenPyXl package
- Learn a little about Pandas
Automate Excel with Python with openpyxl
By now you know how to work with Text, CSV and JSON.
Now, let's learn how to Automate Excel with Python.
There are many packages for working with Excel in Python, but we'll focus on OpenPyXL.

OpenPyXl is a very simple and straight-forward package that allows you to do anything you need to automate your work in Excel. So let's dive in.

📃Step 1 - Documentation
Whenever you start working with a new package it's worth checking the documentation. You can often find a lot of information and code examples on how to use it.
So head over to:
1️⃣ https://openpyxl.readthedocs.io/en/stable/
2️⃣ https://ssopenpyxl.readthedocs.io/en/stable/ (Modern Look)
Inside you'll find a lot of information and code samples. I'd recommend you to briefly go through Tutorial section just to see what's available

⬇️ Install OpenPyXL
First of all, we need to import the package:
However, this is not a built-in package so we need to install it.
If you use pyCharm:
Settings → Project → Python Interpreter → + (Add Package) → search openpyxl

For other IDE:
You can use the terminal and install package with pip .
⚠️ If you get any errors, double-check your virtual environment or Python version.
And if everything works, then let's start with the basics of openpyxl.
1️⃣ Open/Create Excel Workbook
To begin we need to get the Workbook, which refers to the Excel file itself.
We can either open existing file or create a new one. Just remember that nothing will happen on disk until you use .save() on your Workbook.
💡 Tip: If you see "Permission denied" when saving, it usually means the Excel file is currently open. Close it and try again.
2️⃣ Excel Worksheets: Get/Create/Copy/Remove
Next, we need to learn about Worksheets, which are tabs inside your Excel file.

It's very common to use wb.active to get the last used worksheet, but sometimes you want to be more specific. So here are different ways to get existing Worksheet.
We can also Create/Copy/Remove Worksheets. Just make sure you don't delete important data when you use Remove.
💡Don't forget to include the code where we created/opened a Workbook.
🔎 Reading data
Alright we have a Workbook and a Worksheet, now we can look at the data.
There are multiple ways to read information so let's cover the basics.
Starting with Reading Specific Cells (very straight-forward):
💡Notice that Rows/Columns count starts with 1 and not 0 like in programming.
Next, let's create a loop to read lots of values at once. At first we'll look at creating ranges to read over certain array of numbers.
Read with Range Loop :
This works well for writing data, but not so great for reading because we'll get a bunch of empty cells...

So let's look at better way to read.
Loop over Rows/Columns with Data:
So, instead of using random numbers to iterate over, we can actually get the number of rows/columns already used by Worksheet. This way you'll iterate over actual data.
Worksheet.iter_rows() - Iterate over list of rows containing dataWorksheet.iter_columns() - Iterate over list of columns containing data

So let's iterate over rows and read their data:
Formulas vs Raw-Data
If you pay attention, you might notice that when we get values from cells we might also read actual formula instead of the data. Sometimes it might be useful, but in Python you'll often want to work with actual data instead.

So there is a trick to convert your workbook cells into raw-data with load_workbook(filename, data_only=True).
💡IMPORTANT: Be careful using
data_only=True, because if you save your Excel, you'll remove all your formulas from all sheets! Might be a catastrophie
💡Pro Tip: Always create copies of your Excel files before you start automations. You don't want to destroy important data!
So, let me show you a workaround so we can read the same Excel file as Raw-Data and Values with Formulas at the same time:
💡IMPORTANT: Again! Be VERY careful using
data_only=True, because if you save your Excel, you'll remove all your formulas from all sheets! Might be a catastrophie
✍️ Writing data (cells, appending rows, inserting, moving)
So we can read data, how about writing data to Excel?
Let's start with writing data to specific cells, and there are many options to choose from:
This works fine for single cells, but you'll more often want to add rows of data at the same time. Therefore it's best to use append like this:
💡Notice that we can use ws.max_row or ws.max_column to get the number of the last Column/Row Used. That can be really useful for formulas or getting the right cells!
Now, imagine that you have a simple table with data and the last row or column are sums. Then you might want to insert a row/column to write more data. For that you can use:
ws.insert_row()ws.insert_cols()
Or similarly, you can also delete rows/columns
Lastly, it's less common to use but you can move your data around by defining a range of data and then specifying how many rows/columns to move it. Be careful overriding existing data!
🔠 Ranges
Now, let's look how we can work with ranges of data.
We can get all data in a Column, Row or a 2D-Matrix of data.
Here's an example:
Now if you'd want to iterate over the data in a range, keep in mind of its structure.
If it's just a Column or a Row it will be treated as a list, so you'd use only one loop. But if it's a range of multiple rows and columns, then you'd want to iterate over rows and then items in each raw.
Like this:
🎨 Formatting
We've covered the main basics, now let's look at formatting.
For that we'll need to import styles from openpyxl.styles import * and then we'd want to configure various attributes of cells:
This is a big example that includes a lot of examples, but you can use only the parts that are relevant to you. Maybe you only want to add some styling or hyperlink then only use those sections.

💡If you use stlyes like
Hyperlinkit may override other attributes.
🧪 Final Example: EF-Rooms (mini “report”)
Alright, we've covered a lot of bits and pieces now let's put it all together into an actual example you might want to use in your Python scripts.
Let's say we calculated some data where we made a list of nested lists to represent multiple rows:
Now here's a code snippet on how we could create a new Workbook and write this data with some simple formatting for headers:
Overall, it's not complicated and you can expand it to your own needs. Just take it one step at the time.

🐼 Bonus: pandas
Remember I've mentioned that there are plenty of packages to work with Excel.
Openpyxl is a great all-in-one package for working with Excel using Python. However, if you're going to go deep into data science and often work with analyzing data, you might also want to learn a more scientific package called - Pandas.
It's also a custom pacakge so don't forget to install it - pip install pandas
It's a more powerful package which is often used in AI, machine learning and other data related disciplines. And it also has many option export your data in various formats including Excel. For that we'd need to use DataFrame data structure in pandas.
Here's an example on how we could convert a JSON data into an excel in just a few lines:
What I like a lot about pandas is that if you print your DataFrame it has a really nice styling in the console like this:

It also has advanced features for working with data, but it can be a lesson on its own...

Happy Coding!
🙋♂️ See you in the next lesson.
- EF
And that's the basics of how you could start automating Excel.
For the homework follow along the lesson and try out various code snippets that you can see yourself using on a real project.
Also give pandas a try. It might be a more complex package, but it can also provide you far more benefits if you work with large sets of data or need more analysis tools.
⌨️ Happy Coding!
