Wprowadzenie do IronXL for Python
IronXL for Python to potężna biblioteka opracowana przez Iron Software, oferująca inżynierom oprogramowania możliwość tworzenia, odczytywania i edytowania plików Excel (XLS, XLSX i CSV) w projektach Python 3.
IronXL for Python nie wymaga instalacji programu Excel na serwerze ani Interop. IronXL for Python zapewnia szybszy i bardziej intuicyjny interfejs API niż Microsoft.Office.Interop.Excel.
IronXL for Python bazuje na sukcesie i popularności IronXL for .NET.
Instalacja IronXL for Python
Wymagania wstępne
Aby używać IronXL for Python, upewnij się, że komputer ma zainstalowane następujące oprogramowanie wstępne:
- .NET 6.0 SDK: IronXL for Python opiera się na bibliotece IronXL .NET, konkretnie .NET 6.0, jako podstawowej technologii. W związku z tym konieczne jest zainstalowanie .NET 6.0 SDK na Twoim komputerze, aby używać IronXL for Python.
- Python: Pobierz i zainstaluj najnowszą wersję Python 3.x z oficjalnej strony Pythona: https://www.python.org/downloads/. Podczas procesu instalacji, upewnij się, że wybierzesz opcję dodania Python do systemowej PATH, co umożliwi jego dostępność z linii komend.
- Pip: Pip jest zazwyczaj dołączany do instalacji Pythona od wersji 3.4 wzwyż. Jednakże, w zależności od Twojej instalacji Pythona, możesz potrzebować upewnić się, czy pip jest już zainstalowany lub zainstalować go oddzielnie.
- Biblioteka IronXL: Bibliotekę IronXL można dodać za pomocą pip. Użyj poniższego polecenia, aby zainstalować IronXL za pomocą pip:
pip install IronXL
==2023.x.x. Na przykład można uruchomić polecenie pip install ironxl==2023.x.x.pip3 zamiast pip, aby upewnić się, że używasz Pip dla Pythona 3.Odczytywanie dokumentu Excel
Czytanie danych z pliku Excel za pomocą IronXL for Python zajmuje kilka linii kodu.
: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}'")Tworzenie nowych dokumentów Excel
Aby tworzyć dokumenty Excel w Pythonie, IronXL for Python zapewnia prosty, szybki interfejs.
:path=/static-assets/excel-python/content-code-examples/get-started/get-started-2.pyfrom 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")Eksportowanie jako CSV, XLS, XLSX, JSON lub XML
Możemy również zapisywać lub eksportować w wielu powszechnych formatach arkuszy kalkulacyjnych.
: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")Stylizacja komórek i zakresów
Komórki i zakresy w Excelu można formatować, używając obiektu Style.
: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.DoubleSortowanie zakresów
Korzystając z IronXL for Python, możemy sortować zakres komórek Excela za pomocą Range.
: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()
Edytowanie formuł
Edytowanie formuły w Excelu jest tak proste, jak przypisanie wartości z znakiem równości = na początku. Formuła zostanie obliczona na żywo.
: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
Dlaczego warto wybrać IronXL for Python?
IronXL for Python oferuje proste API dla programistów do odczytu i zapisu dokumentów Excel.
IronXL for Python nie wymaga instalacji Microsoft Excel na serwerze ani Interopa Excela, aby uzyskać dostęp do dokumentów Excel. To sprawia, że praca z plikami Excel w Pythonie jest bardzo szybka i prosta.
Licencjonowanie i wsparcie dostępne
IronXL for Python jest darmowy do użytku i testowania w środowiskach deweloperskich.
Aby używać w projektach na żywo, kup licencję. 30-dniowe licencje próbne są również dostępne.
Aby zobaczyć pełną listę przykładów kodu, samouczków, informacji o licencjach i dokumentacji, odwiedź: IronXL for Python.
W przypadku dodatkowego wsparcia i zapytań, proszę skontaktować się z naszym zespołem.







