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 the 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.

:path=/static-assets/excel-python/content-code-examples/get-started/get-started-1.py
# Load the necessary module from IronXL
from ironxl import WorkBook

# Load an existing Excel spreadsheet
# Replace 'sample.xlsx' with the path to your Excel file as needed.
workbook = WorkBook.load("sample.xlsx")

# Select the first worksheet from the workbook
worksheet = workbook.worksheets[0]

# Access cell A2 and get its integer value
# Ensure the correct method or property is used to fetch the integer value.
# Use 'value' to directly access the cell content.
cell_value = worksheet["A2"].value

# Print out the value of the cell A2
# Utilizing formatted strings for clear output
print(f"Cell A2 has value '{cell_value}'")

# Iterate over a range of cells and print their address and text content
# The range is defined from A2 to B10, which captures all rows in this interval.
for cell in worksheet.range("A2:B10"):
    # Access each cell in the specified range
    # AddressString is used to get the cell's location as a string, and Text to get its content.
    print(f"Cell {cell.address} has value '{cell.text}'")
PYTHON

Creating New Excel Documents

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

:path=/static-assets/excel-python/content-code-examples/get-started/get-started-2.py
from ironxl import WorkBook, ExcelFileFormat, BorderType  # Import necessary classes from ironxl

# Create a new Excel WorkBook document in XLSX format
workbook = WorkBook.create(ExcelFileFormat.XLSX)

# Set metadata for the workbook
workbook.metadata.author = "IronXL"

# Add a new blank worksheet named "main_sheet" to the workbook
worksheet = workbook.create_worksheet("main_sheet")

# Add data to cell "A1"
worksheet["A1"].value = "Hello World"

# Set the style for cell "A2" with a double bottom border and a specific color
worksheet["A2"].style.bottom_border.set_color("#ff6600")
worksheet["A2"].style.bottom_border.type = BorderType.double

# Save the Excel file with the specified filename
workbook.save_as("NewExcelFile.xlsx")
PYTHON

Exporting as CSV, XLS, XLSX, JSON or XML

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

:path=/static-assets/excel-python/content-code-examples/get-started/get-started-3.py
PYTHON

Styling Cells and Ranges

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

:path=/static-assets/excel-python/content-code-examples/get-started/get-started-4.py
` tags:

<code>
// Set value and styles for the workbook cells

// Assign the string "Hello World" to cell A1
workSheet["A1"].Value = "Hello World";

// Configure the style of the bottom border for cell A2

// Set the bottom border color of cell A2 to a specific orange color using a hexadecimal value
workSheet["A2"].Style.BottomBorderColor = "#ff6600";

// Set the bottom border type of cell A2 to a double line
workSheet["A2"].Style.BottomBorderType = BorderType.Double;
PYTHON

Sorting Ranges

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

:path=/static-assets/excel-python/content-code-examples/get-started/get-started-5.py
```python
# Import IronXL library for handling Excel files
from ironxl import WorkBook

# Load an existing Excel workbook
# 'sample.xls' is the file name of the Excel workbook to be loaded
workbook = WorkBook.Load("sample.xls")

# Access the first worksheet in the workbook
# WorkSheets is the collection of all sheets in the workbook, 
# and we select the first one using index 0
worksheet = workbook.WorkSheets[0]

# Select a range of cells from A2 to A8 in the worksheet
# This specifies a contiguous range of cells starting from A2 and ending at A8
selected_range = worksheet["A2:A8"]

# Sort the selected range of cells in ascending order
# This operation reorders the values in the specified range from smallest to largest
selected_range.SortAscending()

# Save the changes made to the workbook, including the sorted range
# The workbook's state is updated with the changes after execution
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.

:path=/static-assets/excel-python/content-code-examples/get-started/get-started-6.py
// Set a formula for the cell A1 to be the sum of cells A2 through A10.
workSheet["A1"].Formula = "=SUM(A2:A10)";

// Retrieve the calculated decimal value from cell A1 after the formula is evaluated.
// The syntax assumes 'workSheet["A1"].DecimalValue' gives the evaluated result of the formula.
// Ensure that the worksheet object is correctly set up with relevant libraries capable of calculating formulae.
decimal 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.

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.