Introduction to IronXL for Python

IronXL for Python is a powerful library developed by Iron Software, offering software engineers the capability to create, read, and edit Excel (XLS, XLSX and CSV) files in Python 3 projects.

IronXL for Python does not require Excel to be installed on your server or Interop. IronXL for Python provides a faster and more intuitive API than Microsoft.Office.Interop.Excel.

IronXL for Python builds upon the success and popularity of IronXL for .NET.

Install IronXL for Python

Prerequisites

To use IronXL for Python, please ensure that the computer has the following prerequisite software installed:

  1. .NET 6.0 SDK: 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. Python: Download and install the latest version of Python 3.x from the official Python website: https://www.python.org/downloads/ During the installation process, make sure to select the option to add Python to the system PATH, which will make it accessible from the command line.

  3. Pip: Pip is usually bundled with Python installation starting from Python 3.4 and later. However, depending on your Python installation, you may need to check if pip is already installed or install it separately.
  4. IronXL Library: The IronXL library can be added via pip. Use the command below to install IronXL using pip:
 pip install IronXL

Tips
To install a specific version of IronXL, please use the following syntax: "==2023.x.x". For example, you can run the command "pip install IronXL==2023.x.x".
Please note
On some systems, Python 2.x may still be the default version. In such cases, you may need to explicitly use pip3 command instead of pip to ensure that you're using Pip for Python 3.

Reading an Excel Document

Reading data from an Excel file with IronXL for Python takes a few lines of code.

from ironxl import *

# Load existing spreadsheet
workbook = WorkBook.Load("sample.xlsx")
worksheet = workbook.WorkSheets[0]

# Select cells easily in Excel notation and return the calculated value, date, text, or formula
cell_value = worksheet["A2"].IntValue

# Read from ranges of cells elegantly
for cell in worksheet["A2:B10"]:
    print(f"Cell {cell.AddressString} has value '{cell.Text}'")
PYTHON

Creating New Excel Documents

To create Excel documents in Python; IronXL for Python provides a simple, fast interface.

from ironxl import *

# Create a new Excel WorkBook document.
workbook = WorkBook.Create(ExcelFileFormat.XLSX)
workbook.Metadata.Author = "IronXL"

# Add a blank WorkSheet
worksheet = workbook.CreateWorkSheet("main_sheet")

# Add data and styles to the new worksheet
worksheet["A1"].Value = "Hello World"
worksheet["A2"].Style.BottomBorder.SetColor("#ff6600")
worksheet["A2"].Style.BottomBorder.Type = BorderType.Double  # Assuming BorderType is accessible directly

# Save the Excel file
workbook.SaveAs("NewExcelFile.xlsx")
PYTHON

Exporting as CSV, XLS, XLSX, JSON or XML

We can also save or export as many common structured spreadsheet file formats.

# Assuming workSheet is an existing instance of WorkSheet
workSheet.SaveAs("NewExcelFile.xls")
workSheet.SaveAs("NewExcelFile.xlsx")
workSheet.SaveAsCsv("NewExcelFile.csv")
workSheet.SaveAsJson("NewExcelFile.json")
workSheet.SaveAsXml("NewExcelFile.xml")
PYTHON

Styling Cells and Ranges

Excel cells and ranges can be styled using the Style object.

# Set cell's value and styles
workSheet["A1"].Value = "Hello World"
workSheet["A2"].Style.BottomBorder.SetColor("#ff6600")
workSheet["A2"].Style.BottomBorder.Type = BorderType.Double
PYTHON

Sorting Ranges

Using an IronXL for Python we can sort a range of Excel Cells using Range.

from ironxl import *

# Load existing spreadsheet
workbook = WorkBook.Load("sample.xls")
worksheet = workbook.WorkSheets[0]

# This is how we get a range from an Excel worksheet
selected_range = worksheet["A2:A8"]

# Sort the range in the sheet
selected_range.SortAscending()

# Save changes with the sorted range
workbook.Save()
PYTHON

Editing Formulas

Editing an Excel formula is as easy as assigning a value with an = equals sign at the start. The formula will be calculated live.

# Set a formula
workSheet["A1"].Formula = "=SUM(A2:A10)"
# Get the calculated value
sum_ = workSheet["A1"].DecimalValue
PYTHON

Why Choose IronXL for Python?

IronXL for Python features an easy API for developers to read and write Excel documents.

IronXL for Python does not require installation of Microsoft Excel on your server or Excel Interop to access Excel documents. This makes working with Excel files in Python, a very quick and simple task.

Licensing & Support Available

IronXL for Python is free to use and test in development environments.

To use in live projects purchase a license. 30 day Trial licenses are also available here.

For our full list of code examples, tutorials, licensing information, and documentation visit: IronXL for Python.

For more support and inquiries, please ask our team.