Excel 條件格式設定
from ironxl import *

# Load existing spreadsheet
workbook = WorkBook.Load("sample.xlsx")
worksheet = workbook.DefaultWorkSheet

# Create conditional formatting rule
rule = worksheet.ConditionalFormatting.CreateConditionalFormattingRule(ComparisonOperator.LessThan, "8")
# Set style options
rule.FontFormatting.IsBold = True
rule.FontFormatting.FontColor = "#123456"
rule.BorderFormatting.RightBorderColor = "#ffffff"
rule.BorderFormatting.RightBorderType = BorderType.Thick
rule.PatternFormatting.BackgroundColor = "#54bdd9"
rule.PatternFormatting.FillPattern = FillPattern.Diamonds
# Apply formatting on the specified region
worksheet.ConditionalFormatting.AddConditionalFormatting("A3:A8", rule)

# Create another conditional formatting rule
rule1 = worksheet.ConditionalFormatting.CreateConditionalFormattingRule(ComparisonOperator.Between, "7", "10")
# Set style options
rule1.FontFormatting.IsItalic = True
rule1.FontFormatting.UnderlineType = FontUnderlineType.Single
# Apply formatting on the specified region
worksheet.ConditionalFormatting.AddConditionalFormatting("A3:A9", rule1)

# Save changes with applied conditional formatting
workbook.SaveAs("applyConditionalFormatting.xlsx")
pip install ironxl
Excel 條件格式設定

Excel 條件格式設定

IronXL for Python 支援條件格式化的儲存格和範圍。 條件格式化會根據邏輯或程式規則改變儲存格的樣式——例如背景顏色或文字格式。

使用 CreateConditionalFormattingRule(formula) 建立基於布林表達式的條件格式化規則。 當公式評估為 True 時,該儲存格會高亮顯示。 提供的公式必須返回布林結果。

CreateConditionalFormattingRule 的三參數重載只接受 ComparisonOperator.Between 或 ComparisonOperator.NotBetween 作為第一個參數。

條件格式化使開發人員能夠根據每個儲存格的值,用特定的顏色和格式組合高亮顯示儲存格和範圍。 這使得可以直接在 Excel 文件中簡單地分析資料,檢測問題,並識別模式和趨勢。

學習在 Python 中應用條件格式化

準備好開始了嗎?

版本:2026.10剛剛發布