How to Read Excel Files in Python with IronXL
This guide provides Python developers with step-by-step instructions on utilizing the IronXL library to read and edit Microsoft Excel documents.
IronXL is a comprehensive Excel file processing library that supports multiple programming languages, including .NET and Python. This tutorial focuses specifically on using IronXL in Python scripts to read and edit Microsoft Excel documents.
For a separate tutorial on how to read and edit Microsoft Excel documents in .NET applications, please refer to the following here.
Reading and creating Excel files in Python is easy using the IronXL for Python software library.
Overview
How to Read Excel File in Python
- Download the Python Library to read Excel files
- Load and read an Excel file (workbook)
- Create an Excel workbook in CSV or XLSX
- Edit cell values in a range of cells
- Validate spreadsheet data
- Export data using Entity Framework
Tutorial
Step 1: Add IronXL as a Dependency in Your Python Project
To integrate the IronXL library into your Python project, you must install it as a dependency using the widely used Python package manager, pip. Open the terminal and execute the following command:
This will install the specified version of IronXL in your project, making it accessible for import.
Step 2: Load an Excel Workbook
The WorkBook class represents an Excel workbook. To open an Excel file, we use the WorkBook.Load method, specifying the path of the Excel file.
# Load existing spreadsheet
workbook = WorkBook.Load("Spreadsheets\\GDP.xlsx")
Each WorkBook can have multiple WorkSheet objects. Each one represents a single Excel worksheet in the Excel document. Use the WorkBook.get_worksheet method to retrieve a reference to a specific Excel worksheet.
# Assuming workBook is an existing instance of WorkBook
workSheet = workBook.GetWorkSheet("GDPByCountry")
Creating new Excel Documents
To create a new Excel document, construct a new WorkBook object with a valid file type.
# Create a new WorkBook with the specified Excel file format
workBook = WorkBook(ExcelFileFormat.XLSX)
Note: Use ExcelFileFormat.XLS to support legacy versions of Microsoft Excel (95 and earlier).
Add a Worksheet to an Excel Document
As explained previously, an IronXL for Python WorkBook contains a collection of one or more WorkSheets.
To create a new worksheet, call workbook.create_worksheet with the name of the worksheet.
workSheet = workBook.CreateWorkSheet("GDPByCountry")
Access Cell Values
Read and Edit a Single Cell
Access to the values of individual spreadsheet cells is carried out by retrieving the desired cell from its WorkSheet as shown below:
# Load existing spreadsheet
workbook = WorkBook.Load("test.xlsx")
worksheet = workbook.DefaultWorkSheet
# Access cell B1 in the worksheet
cell = worksheet["B1"]
IronXL for Python's Cell class represents an individual cell in an Excel spreadsheet. It contains properties and methods that enable users to access and modify the cell's value directly.
With a reference to a Cell object, we can read and write data to and from a spreadsheet cell.
Read and Write a Range of Cell Values
The Range class represents a two-dimensional collection of Cell objects. This collection refers to a literal range of Excel cells. Obtain ranges by using the string indexer on a WorkSheet object.
# Access cell B1 in the worksheet
cell = workSheet["B1"]
# Read the value of the cell as a string
value = cell.StringValue
print(value)
# Write a new value to the cell
cell.Value = "10.3289"
print(cell.StringValue)
Add Formula to a Spreadsheet
Set the formula of Cells with the formula property.
# Access range D2:D101 in the worksheet
range_ = workSheet["D2:D101"]
The code below iterates through each cell and sets a percentage total in column C.
# Iterate through all rows with a value
for y in range(2, i):
# Get the C cell
cell = workSheet[f"C{y}"]
# Set the formula for the Percentage of Total column
cell.Formula = f"=B{y}/B{i}"
Summary
IronXL.Excel is a standalone Python library for reading a wide variety of spreadsheet formats. It does not require Microsoft Excel to be installed and is not dependent on Interop.
Frequently Asked Questions
What is IronXL for Python used for?
IronXL for Python is used for reading, editing, and writing Microsoft Excel documents in Python scripts without requiring Microsoft Excel to be installed.
How do I install the IronXL library in a Python project?
You can install IronXL in a Python project using pip, the Python package manager, by executing the appropriate command in the terminal.
Can I use IronXL to create new Excel documents?
Yes, IronXL allows you to create new Excel documents by constructing a new WorkBook object with the desired file format.
How can I load an existing Excel workbook in IronXL?
You can load an existing Excel workbook in IronXL using the WorkBook.Load method and specifying the path to your Excel file.
Is it possible to access and edit specific cell values using IronXL?
Yes, IronXL allows you to access and edit individual cell values by retrieving the desired cell from a WorkSheet object and using the properties and methods available.
How do I add a new worksheet to an Excel document using IronXL?
To add a new worksheet to an Excel document, use the CreateWorkSheet method of a WorkBook object with the desired worksheet name.
Does IronXL support multiple programming languages?
Yes, IronXL supports multiple programming languages, including .NET and Python, for processing Excel files.
Can IronXL manipulate a range of cells in a spreadsheet?
IronXL allows you to manipulate a range of cells using the Range class, which represents a two-dimensional collection of Cell objects.
What is required to use IronXL for Python on my machine?
To use IronXL for Python, you need to have the .NET 6.0 SDK installed on your machine, since IronXL for Python relies on the IronXL for .NET library.
Does IronXL require Microsoft Excel to be installed on my system?
No, IronXL is a standalone library and does not require Microsoft Excel to be installed on your system to read and write Excel files.

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.
