Python openpyxl – Read data from spreadsheet

In this post we will be creating a Python script that will read cell data within an existing Excel spreadsheet using the openpyxl library. Openpyxl is a library that allows us to interact with Excel files in Python. It provides a range of tools for reading, writing, manipulating, and formatting Excel data. This makes it a useful tool for data analysis and reporting tasks.

See the sample of Python code below where we use the openpyxl library to read data from cells within an already existing Excel spreadsheet. We start by loading our existing Excel file called example.xlsx and assign it to a variable named workbook. Next we retrieve the active worksheet from the workbook. We then set the name and role variables to the values in cells A2 and B2 of the active worksheet, respectively. Finally these variables are print to output.

import openpyxl

workbook = openpyxl.load_workbook('example.xlsx')

worksheet =

name = worksheet['A2'].value
role = worksheet['B2'].value

print(f"Name: {name}, Role: {role}")

See an image of our spreadsheet for this example below.

Python openpyxl - Read data from spreadsheet

The output of our script would be seen as.

Name: John, Age: Software Developer

Take a look at some of our other content around the Python programming language by clicking here.

Leave a Reply