IronXL Python 入門

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

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,請確保電腦已安裝以下必備軟體:

  1. .NET 6.0 SDK :IronXL for Python 依賴 IronXL .NET 函式庫,特別是 .NET 6.0 作為其底層技術。 因此,要使用 IronXL for Python,必須在您的電腦上安裝.NET 6.0 SDK
  2. Python :從 Python 官方網站下載並安裝最新版本的 Python 3.x:https://www.python.org/downloads/ 。 在安裝過程中,請務必選擇將 Python 新增至系統 PATH 的選項,這樣就可以從命令列存取它。
  3. Pip :從 Python 3.4 及更高版本開始,Pip 通常與 Python 安裝程式捆綁在一起。 但是,根據你的 Python 安裝情況,你可能需要檢查 pip 是否已經安裝,或單獨安裝它。
  4. IronXL 庫:可以透過 pip 新增 IronXL 庫。使用以下命令透過 pip 安裝 IronXL:
pip install IronXL

若要安裝特定版本的 IronXL,請使用下列語法: ==2023.xx 。 例如,您可以執行指令pip install ironxl==2023.xx .)}]

在某些系統中,Python 2.x 可能仍然是預設版本。 在這種情況下,您可能需要明確使用pip3命令而不是pip ,以確保您使用的是 Python 3 的 Pip。

讀取 Excel 文檔

使用 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}'")
PYTHON

建立新的Excel文檔

要使用 Python 建立 Excel 文檔,IronXL for Python 提供了一個簡單、快速的介面。

: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

匯出格式為 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")
PYTHON

設定單元格和區域的樣式

可以使用 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
PYTHON

排序範圍

使用 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()
PYTHON

編輯公式

編輯 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
PYTHON

為什麼選擇 IronXL 進行 Python 開發?

IronXL for Python 為開發人員提供了簡單易用的 API,方便他們讀取和寫入 Excel 文件。

IronXL for Python 不需要在伺服器上安裝 Microsoft Excel 或 Excel Interop 即可存取 Excel 文件。 這使得在 Python 中處理 Excel 檔案成為一項非常快速且簡單的任務。

提供許可和支持

IronXL for Python可以在開發環境中免費使用和測試。

要在實際項目中使用,請購買許可證。 我們也提供30 天試用許可證

如需查看完整的程式碼範例、教學課程、許可資訊和文檔,請造訪: IronXL for Python

如需更多協助或有任何疑問,請聯絡我們的團隊

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Version: 2025.9 剛發表