# Introduction à IronXL for Python
IronXL for Python est une bibliothèque puissante développée par Iron Software, offrant aux ingénieurs logiciels la possibilité de créer, lire et modifier des fichiers Excel (XLS, XLSX et CSV) dans des projets Python 3.
IronXL for Python ne nécessite pas qu'Excel soit installé sur votre serveur ou Interop. IronXL for Python fournit une API plus rapide et plus intuitive que `Microsoft.Office.Interop.Excel`.
IronXL for Python s'appuie sur le succès et la popularité d'IronXL for .NET.
## Installer IronXL for Python
### Prérequis
Pour utiliser IronXL for Python, veuillez vous assurer que l'ordinateur dispose des logiciels prérequis suivants :
1. **SDK .NET 6.0** : IronXL for Python s'appuie sur la bibliothèque IronXL .NET, et plus précisément sur .NET 6.0, comme technologie sous-jacente. Il est donc nécessaire d'avoir le [SDK .NET 6.0](https://dotnet.microsoft.com/en-us/download/dotnet/6.0) installé sur votre machine pour pouvoir utiliser IronXL for Python.
2. **Python** : Téléchargez et installez la dernière version de Python 3.x depuis le site web officiel de Python :[https://www.python.org/downloads/](https://www.python.org/downloads/) . Lors du processus d'installation, assurez-vous de sélectionner l'option pour ajouter Python au PATH système, ce qui le rendra accessible depuis la ligne de commande.
3. **Pip** : Pip est généralement fourni avec l'installation de Python à partir de Python 3.4 et versions ultérieures. Cependant, selon votre installation de Python, vous devrez peut-être vérifier si pip est déjà installé ou l'installer séparément.
4. **Bibliothèque IronXL :** La bibliothèque IronXL peut être ajoutée via pip. Utilisez la commande ci-dessous pour installer IronXL avec pip :
```shell
:ProductInstall
```
[[t:(Pour installer une version spécifique de IronXL, veuillez utiliser la syntaxe suivante : `==2023.x.x`. Par exemple, vous pouvez exécuter la commande `pip install ironxl==2023.x.x`.)]]
[[i:(Sur certains systèmes, Python 2.x peut encore être la version par défaut. Dans de tels cas, vous devrez peut-être utiliser explicitement la commande `pip3` au lieu de `pip` pour vous assurer que vous utilisez Pip for Python 3.)]]
## Lecture d'un document Excel
La lecture des données d'un fichier Excel avec IronXL for Python ne nécessite que quelques lignes de code.
```python
:path=/static-assets/excel-python/content-code-examples/get-started/get-started-1.py
```
## Création de nouveaux documents Excel
Pour créer des documents Excel en Python, IronXL for Python fournit une interface simple et rapide.
```python
:path=/static-assets/excel-python/content-code-examples/get-started/get-started-2.py
```
## Exporter en CSV, XLS, XLSX, JSON ou XML
Nous pouvons également enregistrer ou exporter de nombreux formats de fichiers tableur structurés courants.
```python
:path=/static-assets/excel-python/content-code-examples/get-started/get-started-3.py
```
## Cellules de style et gammes
Les cellules et les plages Excel peuvent être stylisées à l'aide de l'objet Style.
```python
:path=/static-assets/excel-python/content-code-examples/get-started/get-started-4.py
```
## Plages de tri
En utilisant IronXL for Python, nous pouvons trier une plage de cellules Excel à l'aide de la fonction Range.
```python
:path=/static-assets/excel-python/content-code-examples/get-started/get-started-5.py
```
## Modification des formules
Modifier une formule Excel est aussi simple que d'assigner une valeur avec un signe égal `=` au début. La formule sera calculée en direct.
```python
:path=/static-assets/excel-python/content-code-examples/get-started/get-started-6.py
```
## Pourquoi choisir IronXL for Python ?
IronXL for Python propose une API simple permettant aux développeurs de lire et d'écrire des documents Excel.
IronXL for Python ne nécessite pas l'installation de Microsoft Excel sur votre serveur ni Excel Interop pour accéder aux documents Excel. Cela rend la manipulation de fichiers Excel en Python très rapide et simple.
## Licence et support disponibles
**IronXL for Python** est gratuit à utiliser et à tester dans les environnements de développement.
Pour une utilisation dans des projets en production, [veuillez acheter une licence](/python/excel/licensing/) . [Des licences d'essai de 30 jours](trial-license) sont également disponibles.
Pour consulter la liste complète de nos exemples de code, tutoriels, informations sur les licences et documentations, rendez-vous sur : [IronXL for Python](/python/excel/) .
Pour plus d'assistance et pour toute question, veuillez [contacter notre équipe](#live-chat-support) .
IronXL for Python est une bibliothèque puissante développée par Iron Software, offrant aux ingénieurs logiciels la possibilité de créer, lire et modifier des fichiers Excel (XLS, XLSX et CSV) dans des projets Python 3.
IronXL for Python ne nécessite pas qu'Excel soit installé sur votre serveur ou Interop. IronXL for Python fournit une API plus rapide et plus intuitive que Microsoft.Office.Interop.Excel.
IronXL for Python s'appuie sur le succès et la popularité d'IronXL for .NET.
Installer IronXL for Python
Prérequis
Pour utiliser IronXL for Python, veuillez vous assurer que l'ordinateur dispose des logiciels prérequis suivants :
SDK .NET 6.0 : IronXL for Python s'appuie sur la bibliothèque IronXL .NET, et plus précisément sur .NET 6.0, comme technologie sous-jacente. Il est donc nécessaire d'avoir le SDK .NET 6.0 installé sur votre machine pour pouvoir utiliser IronXL for Python.
Python : Téléchargez et installez la dernière version de Python 3.x depuis le site web officiel de Python :https://www.python.org/downloads/ . Lors du processus d'installation, assurez-vous de sélectionner l'option pour ajouter Python au PATH système, ce qui le rendra accessible depuis la ligne de commande.
Pip : Pip est généralement fourni avec l'installation de Python à partir de Python 3.4 et versions ultérieures. Cependant, selon votre installation de Python, vous devrez peut-être vérifier si pip est déjà installé ou l'installer séparément.
Bibliothèque IronXL : La bibliothèque IronXL peut être ajoutée via pip. Utilisez la commande ci-dessous pour installer IronXL avec pip :
> pip install ironxl
pip install ironxl
Conseils: Pour installer une version spécifique de IronXL, veuillez utiliser la syntaxe suivante : ==2023.x.x. Par exemple, vous pouvez exécuter la commande pip install ironxl==2023.x.x.
Veuillez noter: Sur certains systèmes, Python 2.x peut encore être la version par défaut. Dans de tels cas, vous devrez peut-être utiliser explicitement la commande pip3 au lieu de pip pour vous assurer que vous utilisez Pip for Python 3.
Lecture d'un document Excel
La lecture des données d'un fichier Excel avec IronXL for Python ne nécessite que quelques lignes de code.
# Load the necessary module from IronXLfrom ironxl importWorkBook# 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 workbookworksheet = 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 outputprint(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}'")
# 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
Création de nouveaux documents Excel
Pour créer des documents Excel en Python, IronXL for Python fournit une interface simple et rapide.
from ironxl importWorkBook, ExcelFileFormat, BorderType# Import necessary classes from ironxl# Create a new Excel WorkBook document in XLSX formatworkbook = WorkBook.create(ExcelFileFormat.XLSX)# Set metadata for the workbookworkbook.metadata.author = "IronXL"# Add a new blank worksheet named "main_sheet" to the workbookworksheet = 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 colorworksheet["A2"].style.bottom_border.set_color("#ff6600")worksheet["A2"].style.bottom_border.type = BorderType.double# Save the Excel file with the specified filenameworkbook.save_as("NewExcelFile.xlsx")
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
Exporter en CSV, XLS, XLSX, JSON ou XML
Nous pouvons également enregistrer ou exporter de nombreux formats de fichiers tableur structurés courants.
# Assuming workSheet is an existing instance of WorkSheetworkSheet.SaveAs("NewExcelFile.xls")workSheet.SaveAs("NewExcelFile.xlsx")workSheet.SaveAsCsv("NewExcelFile.csv")workSheet.SaveAsJson("NewExcelFile.json")workSheet.SaveAsXml("NewExcelFile.xml")
# 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
Cellules de style et gammes
Les cellules et les plages Excel peuvent être stylisées à l'aide de l'objet Style.
# Set cell's value and stylesworkSheet["A1"].Value = "Hello World"workSheet["A2"].Style.BottomBorder.SetColor("#ff6600")workSheet["A2"].Style.BottomBorder.Type = BorderType.Double
# 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
Plages de tri
En utilisant IronXL for Python, nous pouvons trier une plage de cellules Excel à l'aide de la fonction Range.
# Import IronXL library for handling Excel filesfrom ironxl importWorkBook# Load an existing Excel workbook# 'sample.xls' is the file name of the Excel workbook to be loadedworkbook = 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 0worksheet = 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 A8selected_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 largestselected_range.SortAscending()# Save the changes made to the workbook, including the sorted range# The workbook's state is updated with the changes after executionworkbook.Save()
# 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
Modification des formules
Modifier une formule Excel est aussi simple que d'assigner une valeur avec un signe égal = au début. La formule sera calculée en direct.
# Set a formulaworkSheet["A1"].Formula = "=SUM(A2:A10)"# Get the calculated valuesum_ = workSheet["A1"].DecimalValue
# Set a formula
workSheet["A1"].Formula = "=SUM(A2:A10)"
# Get the calculated value
sum_ = workSheet["A1"].DecimalValue
Python
Pourquoi choisir IronXL for Python ?
IronXL for Python propose une API simple permettant aux développeurs de lire et d'écrire des documents Excel.
IronXL for Python ne nécessite pas l'installation de Microsoft Excel sur votre serveur ni Excel Interop pour accéder aux documents Excel. Cela rend la manipulation de fichiers Excel en Python très rapide et simple.
Licence et support disponibles
IronXL for Python est gratuit à utiliser et à tester dans les environnements de développement.
Pour consulter la liste complète de nos exemples de code, tutoriels, informations sur les licences et documentations, rendez-vous sur : IronXL for Python .
Curtis Chau détient un baccalauréat en informatique (Université de Carleton) et se spécialise dans le développement front-end avec expertise en Node.js, TypeScript, JavaScript et React. Passionné par la création d'interfaces utilisateur intuitives et esthétiquement plaisantes, Curtis aime travailler avec des frameworks modernes et créer des manuels bien structurés et visuellement attrayants.