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 天试用密钥
无需信用卡或创建账户