IRONSOFTWAREHOME
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.BetweenComparisonOperator.NotBetween 作為第一個參數。

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

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

準備好開始了嗎?

版本:2026.9剛剛發布

Key in blue circle

立即免費取得 30 天試用金鑰

Your trial license will be sent to your email address

無任何限制。100% 解鎖。無需信用卡。

bullet_checked無需信用卡或建立帳號無任何限制。100% 解鎖。無需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
預訂您的免費 即時示範
Booking Badge

獲得數百萬工程師的信賴世界各地

Iron Software 的客戶標誌
獲取無需承諾的諮詢
填寫以下表單或發電子郵件至sales@ironsoftware.com
您的資訊將永遠 保持機密。
獲得數百萬工程師的信賴世界各地
Iron Software 的客戶標誌
立即獲取您的30天試用金鑰
無需信用卡或帳戶建立