Einführung in IronXL für Python

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronXL für 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 für Python benötigt weder die Installation von Excel auf Ihrem Server noch Interop. IronXL für Python bietet eine schnellere und intuitivere API als Microsoft.Office.Interop.Excel .

IronXL für Python baut auf dem Erfolg und der Popularität von IronXL für .NET auf.

Installieren Sie IronXL für Python

Voraussetzungen

Um IronXL für Python verwenden zu können, stellen Sie bitte sicher, dass auf Ihrem Computer die folgende Softwarevoraussetzung erfüllt ist:

  1. .NET 6.0 SDK : IronXL für 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 für Python verwenden zu können.
  2. 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.
  3. 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.
  4. 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

TippsUm eine bestimmte Version von IronXL zu installieren, verwenden Sie bitte die folgende Syntax: ==2023.xx . Sie können beispielsweise den Befehl pip install ironxl==2023.xx .
` ausführen.

Hinweis:Auf manchen Systemen kann Python 2.x noch die Standardversion sein. In solchen Fällen müssen Sie möglicherweise explizit den Befehl pip3 anstelle von pip verwenden, um sicherzustellen, dass Sie Pip für Python 3 verwenden.

Ein Excel-Dokument lesen

Das Einlesen von Daten aus einer Excel-Datei mit IronXL für 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}'")
PYTHON

Neue Excel-Dokumente erstellen

Um Excel-Dokumente in Python zu erstellen, bietet IronXL für 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")
PYTHON

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")
PYTHON

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
PYTHON

Bereiche sortieren

Mit IronXL für 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()
PYTHON

Formeln bearbeiten

Das Bearbeiten einer Excel-Formel ist so einfach wie das Zuweisen eines Wertes mit einem = 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
PYTHON

Warum IronXL für Python wählen?

IronXL für Python bietet eine benutzerfreundliche API für Entwickler zum Lesen und Schreiben von Excel-Dokumenten.

IronXL für 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 für 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 für Python .

Für weitere Unterstützung und Anfragen fragen Sie unser Team.

Curtis Chau
Technischer Autor

Curtis Chau hat einen Bachelor-Abschluss in Informatik von der Carleton University und ist spezialisiert auf Frontend-Entwicklung mit Expertise in Node.js, TypeScript, JavaScript und React. Leidenschaftlich widmet er sich der Erstellung intuitiver und ästhetisch ansprechender Benutzerschnittstellen und arbeitet gerne mit modernen Frameworks sowie der Erstellung gut strukturierter, optisch ansprechender ...

Weiterlesen
Bereit anzufangen?
Version: 2025.9 gerade veröffentlicht