Excel Conditional Formatting
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 Conditional Formatting

Excel Conditional Formatting

IronXL for Python supports Conditional Formatting of cells and ranges. Conditional Formatting changes the style of a cell - such as background color or text formatting - based on logical or programmatic rules.

Use CreateConditionalFormattingRule(formula) to create a conditional formatting rule based on a Boolean expression. When the formula evaluates to True, the cell is highlighted. The formula provided must return a Boolean result.

The three-parameter overload of CreateConditionalFormattingRule accepts only ComparisonOperator.Between or ComparisonOperator.NotBetween as the first parameter.

Conditional formatting enables developers to highlight cells and ranges with specific color and format combinations depending on each cell's value. This makes it straightforward to analyze data, detect issues, and identify patterns and trends directly within the Excel file.

Learn to Apply Conditional Formatting in Python

Ready to Get Started?

Version:2026.10just released