Python Read Excel File Tutorial

This guide provides Python developers with step-by-step instructions on utilizing the IronXL library to reading and editing Microsoft Excel documents.

IronXL is a comprehensive Excel file processing library that supports multiple programming languages, including .NET and Python programming languages. 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


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 management tool, pip. Open the terminal and execute the following command:

 pip install IronXL

This will install the specified version of IronXL in your project, making it accessible for import.

Please note
IronXL for Python relies on IronXL .NET library, specifically .NET 6.0, as its underlying technology. Therefore, it is necessary to have the .NET 6.0 SDK installed on your machine in order to use IronXL for Python.


2. Load an Excel Workbook

The WorkBook class represents an Excel sheet. To open an Excel File, we use WorkBook.Load method, specifying the path of the Excel file.

# Load existing spreadsheet
workbook = WorkBook.Load("Spreadsheets\\GDP.xlsx")
PYTHON

Each WorkBook can have multiple WorkSheet objects. Each one represents a single Excel worksheet in the Excel document. Use the WorkBook.GetWorkSheet method to retrieve a reference to a specific Excel worksheet.

# Assuming workBook is an existing instance of WorkBook
workSheet = workBook.GetWorkSheet("GDPByCountry")
PYTHON

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)
PYTHON

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.

This is how one workbook with two worksheets looks in Excel.

This is how one workbook with two worksheets looks in Excel.

To create a new WorkSheet call WorkBook.CreateWorkSheet with the name of the worksheet.

workSheet = workBook.CreateWorkSheet("GDPByCountry")
PYTHON

3. 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"]
PYTHON

IronXL for Python's Cell class represents an invidual cell in an Excel spreadsheet. It contains properties and methods that enable users to access and modify the cell's value directly.

Each WorkSheet object manages an index of Cell objects corresponding to every cell value in an Excel worksheet. In the source code above, we reference the desired cell by its row and column index (cell B1 in this case) using standard array indexing syntax.

With a reference to Cell object, we can read and write data to and from a spreadsheet cell:

# 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)
PYTHON

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.

The argument text is either the coordinate of a cell (e.g. "A1", as shown previously) or a span of cells from left to right top to bottom (e.g. "B2:E5"). It is also possible to call GetRange on a WorkSheet.

# Access range D2:D101 in the worksheet
range_ = workSheet["D2:D101"]
PYTHON

Add Formula to a Spreadsheet

Set formula of Cells with the Formula property.

The code below iterates through each state and puts 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}"
PYTHON

Summary

IronXL.Excel is a stand alone Python library for reading a wide variety of spreadsheet formats. It does not require Microsoft Excel to be installed, and is not dependant on Interop.