Mastering Conditional Formatting in Excel Using Python
Mastering Conditional Formatting in Excel Using Python
Table of Contents
Conclusion
The Spire.XLS for Python library supports nearly all of the conditional
formatting types mentioned above. The examples below illustrate how
to implement several common types of conditional formatting with this
library. In the end of this article, you will find a complete code example
that includes all the conditional formatting techniques.
Data bars visually represent the relative size of values within cells by
adding horizontal bars. This makes it easy to compare values at a
glance, enhancing the readability of numerical data.
Top and bottom rules allow you to highlight the highest or lowest
values in a dataset. This is useful for identifying key data points, such
as top performers or underperformers.
# Add a condition to format the top 2 ranked values in the specified range
condition_1 = format_1.AddTopBottomCondition(TopBottomType.Top, 1)
# Set the background color for the top values to red
condition_1.BackColor = Color.get_Red()
# Add a condition to format the bottom 2 ranked values in the specified range
condition_2 = format_2.AddTopBottomCondition(TopBottomType.Bottom, 1)
# Set the background color for the bottom values to forest green
condition_2.BackColor = Color.get_ForestGreen()
Example 7: Date-Based
# Add a condition to format cells that contain dates from the last month
condition = format.AddTimePeriodCondition(TimePeriodType.LastMonth)
# Set the background color for these dates to orange
condition.BackColor = Color.get_Orange()
In the complete example below, you will find how to apply numerous
types of conditional formatting to an Excel worksheet using Python
and Spire.XLS for Python:
# This method implements the ColorScale conditional formatting type with some
color scale attributes.
def Add3ColorScale(sheet):
xcfs = sheet.ConditionalFormats.Add()
xcfs.AddRange(sheet.Range["A7:C8"])
sheet.Range["A7:C8"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["A7:C8"].Style.Color = Color.get_Green()
cf = xcfs.AddCondition()
cf.FormatType = ConditionalFormatType.ColorScale
cf.ColorScale.MinValue.Type = ConditionValueType.Number
cf.ColorScale.MinValue.Value = Int32(9)
cf.ColorScale.MinColor = Color.get_Purple()
# Set numeric values for the color scale
sheet.Range["A7"].NumberValue = 6
sheet.Range["B7"].NumberValue = 9
sheet.Range["C7"].NumberValue = 12
sheet.Range["A8"].NumberValue = 8
sheet.Range["B8"].NumberValue = 11
sheet.Range["C8"].NumberValue = 14
# This method implements the ColorScale conditional formatting type with some
color scale attributes.
def Add2ColorScale(sheet):
xcfs = sheet.ConditionalFormats.Add()
xcfs.AddRange(sheet.Range["A9:C10"])
sheet.Range["A9:C10"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["A9:C10"].Style.Color = Color.get_White()
cf = xcfs.AddCondition()
cf.FormatType = ConditionalFormatType.ColorScale
cf.ColorScale.MinColor = Color.get_Gold()
cf.ColorScale.MaxColor = Color.get_SkyBlue()
# Set numeric values for the color scale
sheet.Range["A9"].NumberValue = 8
sheet.Range["B9"].NumberValue = 12
sheet.Range["C9"].NumberValue = 13
sheet.Range["A10"].NumberValue = 10
sheet.Range["B10"].NumberValue = 13
sheet.Range["C10"].NumberValue = 16
Additional Resource