Passer au contenu du pied de page
UTILISATION D'IRONXL

Comment analyser un fichier Excel en Python

Spreadsheets made with Microsoft Excel are widely used in workflows for data processing and analysis in many different sectors. Python is a versatile programming language with several libraries for working with Excel files. One such library, IronXL, was created especially for .NET programs like IronPython and offers a smooth connection with Excel files. This in-depth tutorial will examine how to parse an Excel file in Python using IronXL.

  1. Create a new Python project or create a new file with the .py extension.
  2. Install the IronXL library.
  3. Import the required library.
  4. Import the file which needs to be parsed.
  5. Access the specific sheet and parse the values.
  6. Process the values and close the created objects.

IronXL

With the IronXL Python library, developers can effortlessly read and write Excel files in Python. You can work with several Excel sheets at once in addition to writing Excel files. You can manipulate Excel files using this library without having to install Microsoft Excel on your computer.

IronXL is useful when you need to import data directly into an Excel spreadsheet. Using IronXL simplifies the handling of Excel spreadsheets. It facilitates the simple management of data in an XLSX file across several sheets.

Key characteristics of IronXL

1. Reading and Writing Data to and from Excel Files

IronXL makes it simple for developers to read and write data to and from Excel files. IronXL offers simple ways to read from and manipulate Excel files, whether you're pulling data for analysis or creating reports.

2. Support for a Broad Range of Excel Formats

IronXL is compatible with a number of Excel formats, such as .csv, .xls, .xlsx, .xlsm, .xlsb, .xltx, and .xltm. This adaptability guarantees compatibility with many Excel file formats and versions, enabling smooth working with Excel files of any format.

3. Worksheet and Cell Access

Developers may quickly access specific worksheets and cells in Excel workbooks by using IronXL. This makes it possible to precisely manipulate data at the worksheet and cell levels, making activities like data entry, editing, and extraction easier.

4. Formatting and Style

IronXL offers extensive assistance with the formatting and style of Excel files. Excel files can be made more aesthetically pleasing and readable by developers by applying a variety of formatting choices, including font styles, colors, borders, alignment, and more, to individual cells, rows, and columns.

5. Formula Calculation

IronXL has functionality for calculating Excel formulas, enabling programmers to assess formulas inside Excel documents. This capability is perfect for situations where automated data processing and analysis are needed since it allows complicated calculations and formulas to be executed.

6. Chart Generation

With IronXL, developers may use programming to generate and modify charts inside of Excel files. Developers can use this functionality to show correlations, trends, and patterns in data using a variety of chart formats, including pie charts, bar charts, and line charts.

7. Data Validation

IronXL has data validation capabilities that let developers set limitations and guidelines for entering data into Excel files. By restricting users from entering erroneous or incorrect data, this function helps maintain data integrity and guarantees the quality and dependability of Excel documents.

8. Performance Optimization

IronXL has been designed with performance in mind, making it capable of handling large Excel files and datasets with ease. IronXL's specialized algorithms and data structures offer quick and dependable performance while reading, writing, and modifying data—even when working with large Excel spreadsheets.

9. Thorough Documentation and Support

To assist developers in getting started quickly and making the most of IronXL's features, the tool provides a wealth of documentation that includes tutorials, guides, and API references. In addition, Iron Software offers committed assistance to help developers with any queries or problems they could run across when utilizing the library.

To know more about the IronXL documentation, refer here.

Prerequisites

Make sure the following prerequisites are installed on your machine before beginning the tutorial:

  • .NET framework: Your machine must have the .NET 6.0 SDK installed.
  • Python 3.0+: You must have Python 3.0 or higher installed in order to follow this tutorial.
  • pip: Since IronXL will be installed via pip, make sure pip, the Python package installer, is installed.

Setting Up Your Environment

1. Creating a File in Visual Studio Code

Launch Visual Studio Code, then create the ParseExcel.py Python file. This file will include our IronXL script for reading Excel files.

How to parse an Excel file in Python: Figure 1 - Open Visual Studio Code editor and create a new file

2. Installing IronXL

In Visual Studio Code, choose Terminal > New Terminal from the menu to launch the command line.

How to parse an Excel file in Python: Figure 2

To install IronXL, execute the subsequent command:

pip install ironxl
pip install ironxl
SHELL

How to parse an Excel file in Python: Figure 3 - To install IronXL, use the following command: pip install ironxl

Parse Excel files using IronXL

The process to read Excel files is made easy with the help of the IronXL library. Parsing Excel files in Python can be easily done with a few lines of code.

from ironxl import *

# Load the workbook
workbook = WorkBook.Load("Demo.xlsx")

# Accessing the first worksheet
worksheet = workbook.WorkSheets[0]

# Iterate over each row and column
for row in range(worksheet.RowCount):
    for col in range(worksheet.ColumnCount):
        # Get the value of each cell
        cell_value = worksheet.Columns[col].Rows[row].Value
        print(cell_value)

# Close the workbook to free up system resources
workbook.Close()
from ironxl import *

# Load the workbook
workbook = WorkBook.Load("Demo.xlsx")

# Accessing the first worksheet
worksheet = workbook.WorkSheets[0]

# Iterate over each row and column
for row in range(worksheet.RowCount):
    for col in range(worksheet.ColumnCount):
        # Get the value of each cell
        cell_value = worksheet.Columns[col].Rows[row].Value
        print(cell_value)

# Close the workbook to free up system resources
workbook.Close()
PYTHON

The first step is to import the required IronXL modules into your script. Next, use the WorkBook class in IronXL to import Excel files. Replace "Demo.xlsx" with the path to your Excel file, or use a different file format like CSV, XLS, or XLSX. You can access individual Excel sheets inside the workbook once the Excel file has loaded using the WorkBook.Load() method. The IronXL library also allows access to multiple Excel sheets using the index or sheet names.

Next, we extract data from the Excel file by iterating over rows and columns while accessing the worksheet. This code prints the value of each spreadsheet cell as it iterates over them. This logic can be adjusted to meet your unique needs, such as gathering data for additional processing or examination. It is imperative to close the Excel file using the workbook.Close() method once you have completed processing it in order to free up system resources.

Output generated from the above code

How to parse an Excel file in Python: Figure 4 - Output generated using IronXL to read and extract data from an excel file.

To learn more about the IronXL code, refer here.

Conclusion

An efficient way to work with Excel spreadsheets in your Python programs is to parse Excel files using IronXL. Through the combination of IronXL with IronPython, developers may effectively manage data by utilizing the combined capabilities of Excel and .NET. By following the instructions in this article and using the example, you can easily parse Excel files in your Python applications. This opens up a world of possibilities for data analysis and manipulation. IronXL is a useful tool for interacting with Excel files in Python programs because of its user-friendly API and comprehensive documentation.

Recall that there are a plethora of options available for processing and presenting data. Having IronXL in your toolbox gives you the ability to manage a variety of activities linked to Excel in your Python programs.

When ready to commit, users can start using IronXL for free with its trial version. After that, license choices start at $799. To know more about the IronXl license, please refer to the license page.

To know more about other products offered by Iron Software, please check their website.

Questions Fréquemment Posées

Comment puis-je analyser un fichier Excel en Python ?

Pour analyser un fichier Excel en Python en utilisant IronXL, commencez par importer les bibliothèques nécessaires, puis chargez le classeur avec WorkBook.Load(). Accédez aux feuilles de calcul souhaitées, parcourez les lignes et les colonnes, et extrayez les valeurs des cellules.

Est-il possible de manipuler des fichiers Excel en Python sans Microsoft Excel ?

Oui, avec IronXL, vous pouvez manipuler des fichiers Excel en Python sans avoir besoin d'installer Microsoft Excel. IronXL permet la lecture, l'écriture et le traitement des fichiers Excel directement dans les applications Python.

Quels formats Excel sont pris en charge par IronXL ?

IronXL prend en charge une variété de formats Excel, garantissant la compatibilité pour les opérations de lecture et d'écriture sur différents types de fichiers.

IronXL peut-il gérer efficacement de grands ensembles de données ?

Oui, IronXL est optimisé pour la performance et peut traiter efficacement de grands fichiers et ensembles de données Excel, le rendant adapté aux applications intensives en données.

Comment installer IronXL pour le développement en Python ?

Vous pouvez installer IronXL dans votre environnement Python via le gestionnaire de packages pip en utilisant la commande : pip install ironxl.

IronXL offre-t-il un support pour les calculs de formules Excel en Python ?

Oui, IronXL prend en charge les calculs de formules Excel, vous permettant d'exécuter et d'évaluer les formules dans vos applications Python de manière transparente.

Quelle documentation est disponible pour utiliser IronXL avec Python ?

IronXL fournit une documentation complète, comprenant des tutoriels, des guides et des références API pour aider les développeurs à utiliser efficacement ses fonctionnalités pour la manipulation de fichiers Excel en Python.

Quelles sont les options de licence pour IronXL ?

IronXL propose une version d'essai gratuite et après la période d'essai, plusieurs options de licence sont disponibles, à partir d'une licence lite. Plus de détails peuvent être trouvés sur la page de licence d'IronXL.

IronXL peut-il être utilisé pour la validation des données dans les fichiers Excel ?

Oui, IronXL inclut des fonctionnalités pour la validation des données, permettant aux développeurs d'implémenter des contrôles et des règles au sein des fichiers Excel pour garantir l'intégrité des données.

Jordi Bardia
Ingénieur logiciel
Jordi est le plus compétent en Python, C# et C++, et lorsqu'il ne met pas à profit ses compétences chez Iron Software, il programme des jeux. Partageant les responsabilités des tests de produit, du développement de produit et de la recherche, Jordi apporte une immense valeur à l'amé...
Lire la suite