IronXL for Python 簡介
IronXL for Python 是由 Iron Software 開發的一套強大程式庫,讓軟體工程師能夠在 Python 3 專案中建立、讀取及編輯 Excel (XLS、XLSX 和 CSV) 檔案。
IronXL for Python 無需在伺服器上安裝 Excel 或 Interop。IronXL for Python 提供的 API 比 Microsoft.Office.Interop.Excel 更快且更直觀。
IronXL for Python 延續了 IronXL for .NET 的成功與廣受歡迎。
安裝 IronXL for Python
先決條件
若要使用 IronXL for Python,請確保電腦已安裝以下必要軟體:
- .NET 6.0 SDK:IronXL for Python 仰賴 IronXL程式庫(具體而言是 .NET 6.0)作為其底層技術。 因此,若要使用 IronXL for Python,您的電腦上必須已安裝 .NET 6.0 SDK。 https://www.python.org/downloads/2. Python:請從 Python 官方網站下載並安裝最新版本的 Python 3.x:. 在安裝過程中,請務必選取將 Python 加入系統 PATH 的選項,如此一來即可透過命令列存取 Python。
- Pip:自 Python 3.4 及後續版本起,Pip 通常會隨 Python 安裝程式一併提供。 不過,視您的 Python 安裝環境而定,您可能需要確認 pip 是否已安裝,或需另行安裝。
- IronXL程式庫:可透過 pip 安裝 IronXL程式庫。請使用以下指令透過 pip 安裝 IronXL程式庫:
pip install IronXL
==2023.x.x。 例如,您可以執行以下指令 pip install ironxl==2023.x.x.pip3 指令,而非 pip,以確保您使用的是 Python 3 版的 Pip。)}]讀取 Excel 文件
using IronXL for Python 從 Excel 檔案讀取資料,只需幾行程式碼即可完成。
: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}'")建立新的 Excel 文件
若要在 Python 中建立 Excel 文件,IronXL for Python 提供了一個簡單且快速的介面。
: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")匯出為 CSV、XLS、XLSX、JSON 或 XML
我們亦可儲存或匯出多種常見的結構化試算表檔案格式。
: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")設定儲存格與範圍的樣式
可透過 Style 物件為 Excel 儲存格和範圍設定樣式。
: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排序範圍
透過 IronXL for Python,我們可以利用 Range 方法對 Excel 儲存格範圍進行排序。
: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()
公式編輯
編輯 Excel 公式非常簡單,只需在數值前加上 = 等號即可。 公式將即時進行計算。
: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
為何選擇 IronXL for Python?
IronXL for Python 提供簡易的 API,讓開發人員能夠讀取和寫入 Excel 文件。
IronXL for Python 無需在伺服器上安裝 Microsoft Excel 或 Excel Interop 即可存取 Excel 文件。 這使得在 Python 中處理 Excel 檔案成為一項非常快速且簡單的任務。
提供授權與技術支援
IronXL for Python 可在開發環境中免費使用與測試。
如需在實際專案中使用,請購買授權。 亦提供 30 天試用授權。
如需查看完整的程式碼範例、教學指南、授權資訊及文件,請造訪:IronXL for Python。
如需更多支援或有任何疑問,請聯繫我們的團隊。







