Einführung in IronXL for Python
IronXL for Python ist eine leistungsstarke Bibliothek, die von Iron Software entwickelt wurde und Softwareentwicklern die Möglichkeit bietet, Excel-Dateien (XLS, XLSX und CSV) in Python 3-Projekten zu erstellen, zu lesen und zu bearbeiten.
IronXL for Python benötigt weder Excel auf Ihrem Server noch Interop. IronXL for Python bietet eine schnellere und intuitivere API als Microsoft.Office.Interop.Excel.
IronXL for Python baut auf dem Erfolg und der Popularität von IronXL for .NET auf.
Installieren Sie IronXL for Python
Voraussetzungen
Um IronXL for Python verwenden zu können, stellen Sie bitte sicher, dass auf Ihrem Computer die folgende Softwarevoraussetzung erfüllt ist:
- .NET 6.0 SDK : IronXL for Python basiert auf der IronXL .NET-Bibliothek, insbesondere auf .NET 6.0. Daher ist es notwendig, das .NET 6.0 SDK auf Ihrem Rechner installiert zu haben, um IronXL for Python verwenden zu können.
- Python: Laden Sie die neueste Version von Python 3.x von der offiziellen Python-Website herunter: https://www.python.org/downloads/. Wählen Sie während des Installationsvorgangs die Option aus, Python zum System-PATH hinzuzufügen, damit es über die Befehlszeile zugänglich ist.
- Pip : Pip wird üblicherweise ab Python 3.4 mit der Python-Installation mitgeliefert. Je nach Ihrer Python-Installation müssen Sie jedoch möglicherweise prüfen, ob pip bereits installiert ist oder es separat installieren.
- IronXL-Bibliothek: Die IronXL-Bibliothek kann über pip hinzugefügt werden. Verwenden Sie den folgenden Befehl, um IronXL mit pip zu installieren:
pip install IronXL
==2023.x.x. Beispielsweise können Sie den Befehl pip install ironxl==2023.x.x.pip3 anstelle von pip verwenden, um sicherzustellen, dass Sie Pip for Python 3 verwenden.Ein Excel-Dokument lesen
Das Einlesen von Daten aus einer Excel-Datei mit IronXL for Python erfordert nur wenige Codezeilen.
: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}'")
Neue Excel-Dokumente erstellen
Um Excel-Dokumente in Python zu erstellen, bietet IronXL for Python eine einfache und schnelle Schnittstelle.
: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")
Exportieren als CSV, XLS, XLSX, JSON oder XML
Wir können außerdem viele gängige strukturierte Tabellenkalkulationsdateiformate speichern oder exportieren.
:path=/static-assets/excel-python/content-code-examples/get-started/get-started-3.py
# 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")
Zellen und Bereiche formatieren
Excel-Zellen und -Bereiche können mithilfe des Style-Objekts formatiert werden.
:path=/static-assets/excel-python/content-code-examples/get-started/get-started-4.py
# Set cell's value and styles
workSheet["A1"].Value = "Hello World"
workSheet["A2"].Style.BottomBorder.SetColor("#ff6600")
workSheet["A2"].Style.BottomBorder.Type = BorderType.Double
Bereiche sortieren
Mit IronXL for Python können wir einen Bereich von Excel-Zellen mithilfe von Range sortieren.
:path=/static-assets/excel-python/content-code-examples/get-started/get-started-5.py
# 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()
Formeln bearbeiten
Das Bearbeiten einer Excel-Formel ist so einfach wie das Zuweisen eines Wertes mit einem Gleichheitszeichen = am Anfang. Die Formel wird live berechnet.
:path=/static-assets/excel-python/content-code-examples/get-started/get-started-6.py
# Set a formula
workSheet["A1"].Formula = "=SUM(A2:A10)"
# Get the calculated value
sum_ = workSheet["A1"].DecimalValue
Warum IronXL for Python wählen?
IronXL for Python bietet eine benutzerfreundliche API für Entwickler zum Lesen und Schreiben von Excel-Dokumenten.
IronXL for Python benötigt weder die Installation von Microsoft Excel auf Ihrem Server noch Excel Interop, um auf Excel-Dokumente zuzugreifen. Dadurch wird die Arbeit mit Excel-Dateien in Python zu einer sehr schnellen und einfachen Aufgabe.
Lizenzierung & Support verfügbar
IronXL for Python kann kostenlos in Entwicklungsumgebungen verwendet und getestet werden.
Für die Nutzung in Live-Projekten ist der Erwerb einer Lizenz erforderlich . Es sind auch 30-Tage-Testlizenzen erhältlich.
Unsere vollständige Liste mit Codebeispielen, Tutorials, Lizenzinformationen und Dokumentation finden Sie unter: IronXL for Python .
Für weitere Unterstützung und Anfragen fragen Sie unser Team.

