Featured Article

Python Code in Excel: A Comprehensive Guide

Evelyn Jul 13, 2026

Did you know you can use Python to interact with Excel, automate tasks, and perform complex analyses? Python, a popular programming language, offers libraries like openpyxl, pandas, and XlsxWriter that make it easy to read, manipulate, and write data in Excel files. Let's delve into how you can use Python in Excel, step by step.

How to Automate an Excel Sheet in Python?
How to Automate an Excel Sheet in Python?

First, let's understand why you might want to use Python with Excel. Python's capabilities in data manipulation, cleaning, and analysis are unmatched. By combining these with Excel's user-friendly interface and widespread use, you can leverage the strengths of both tools. But how can you get started? Let's explore.

Data Science Tutorials | DataCamp Blogs
Data Science Tutorials | DataCamp Blogs

Reading Excel Files with Python

To read data from an Excel file, you can use the openpyxl or pandas library. Both are powerful and widely used, each with its advantages. Let's look at both.

a woman standing in front of a poster with the words python in excel on it
a woman standing in front of a poster with the words python in excel on it

OpenPyXL, for instance, allows you to read and edit existing Excel files. It's wrote by people who worked on Excel at Microsoft. Here's a simple example:

Using OpenPyXL

Can Excel Use Python?
Can Excel Use Python?

First, install openpyxl using pip: `pip install openpyxl`. Then, you can read an Excel file like this:

from openpyxl import load_workbook

wb = load_workbook(filename='example.xlsx')
ws = wb['Sheet1']

def read_excel_data():
    for row in ws.iter_rows():
        for cell in row:
            print(cell.value, end=' ')
        print()

read_excel_data()

Using Pandas

Pandas is another powerful library for data manipulation and analysis. It offers a data frame structure similar to SQL tables, making it easy to work with Excel data. Here's how to read an Excel file with pandas:

an image of a table with text on it that says excel vs sql vs python
an image of a table with text on it that says excel vs sql vs python

import pandas as pd

df = pd.read_excel('example.xlsx')
print(df.head())

Writing to Excel Files with Python

Writing to Excel files is as straightforward as reading. Let's see how to create a new Excel file and write data to it using both openpyxl and pandas.

First, openpyxl:

All Important Python Functions for Beginners (Complete Cheat Sheet)
All Important Python Functions for Beginners (Complete Cheat Sheet)

Using OpenPyXL

from openpyxl import Workbook

wb = Workbook()
ws = wb.active

def write_excel_data():
    ws['A1'] = 'Hello'
    ws['B1'] = 'World!'
    wb.save('example.xlsx')

write_excel_data()

Using Pandas

Python in Excel - Beginner Tutorial
Python in Excel - Beginner Tutorial
Python in Excel
Python in Excel
How to use Excel, SQL, and Python together for data analysis | Sonia Sangwan posted on the topic | LinkedIn
How to use Excel, SQL, and Python together for data analysis | Sonia Sangwan posted on the topic | LinkedIn
Ezekiel (@ezekiel_aleke) on X
Ezekiel (@ezekiel_aleke) on X
How To Use Python To Manage an Excel Spreadsheet?
How To Use Python To Manage an Excel Spreadsheet?
Automate Excel With Python - Python Excel Tutorial (OpenPyXL)
Automate Excel With Python - Python Excel Tutorial (OpenPyXL)
Python Cheat Sheet for Beginners 2026 | Python Basics, Syntax, Loops, Functions & Variables
Python Cheat Sheet for Beginners 2026 | Python Basics, Syntax, Loops, Functions & Variables
#dataanlytics #sql #python #excel | Azubuike Alex Mmaghiri
#dataanlytics #sql #python #excel | Azubuike Alex Mmaghiri
PyXLL - The Python Excel Add-in
PyXLL - The Python Excel Add-in

Pandas offers a convenient method, to_excel, to write data frames to Excel. Here's how:

data = {
    'Name': ['John', 'Anna', 'Peter'],
    'Age': [28, 24, 35]
}
df = pd.DataFrame(data)

df.to_excel('example.xlsx', index=False)

Automating Excel Tasks with Python

Python's automation capabilities are a game-changer when it comes to Excel. With a bit of code, you can automate repetitive tasks, reducing errors and saving time. Let's look at an example using themodule schedule to automate an Excel task.

First, install the schedule module using pip: `pip install schedule`. Then, you can automate a task like this:

Automating Excel Tasks with Schedule and OpenPyXL

import schedule
import time
from openpyxl import Workbook

def automate_excel_task():
   wb = Workbook()
    ws = wb.active
    ws['A1'] = 'Hello'
    ws['B1'] = 'World!'
    wb.save('example.xlsx')

schedule.every(10).minutes.do(automate_excel_task)

while True:
    schedule.run_pending()
    time.sleep(1)

This script will create a new Excel file every 10 minutes with the words 'Hello' and 'World' in cells A1 and B1, respectively.

In the world of data management and analysis, Python and Excel are a powerful duo. They complement each other perfectly, with Python's might in data manipulation and Excel's ease of use. With the right tools and a bit of learning, you can unlock their full potential and streamline your work.