Mastering Conditional
Formatting in Excel Using
Python: A Comprehensive
Guide
Conditional formatting is a valuable feature in spreadsheet
applications like Microsoft Excel and Google Sheets. It allows you to
automatically change the appearance of cells — using colors, icons, or
data bars — based on the values they contain. This makes it easier to
analyze and understand your data by highlighting key trends, patterns,
and outliers. With conditional formatting, you can quickly see
important information at a glance, helping you make informed
decisions.
In this blog, we’ll delve into how to master conditional formatting in
Excel using Python.
Table of Contents
Set Up Your Environment
Understanding the Types of Conditional Formatting in Excel
Add Conditional Formatting to Excel in Python
Complete Code Example
Conclusion
Set Up Your Environment
Before you start working with conditional formatting in Excel, you
need to ensure that Python is installed on your computer. If you
haven’t done this yet, you can download and install Python from
the official Python website.
Once Python is installed, you’ll need to install the Spire.XLS for
Python library, which will enable you to manipulate Excel files and
apply various conditional formatting. To install this library, follow
these steps:
Open your terminal (Command Prompt on Windows, Terminal on
macOS or Linux).
Type the following command and press Enter:
pip install Spire.Xls
Understanding the Types of Conditional Formatting in
Excel
Conditional formatting comes in various types, each designed to
address specific data visualization needs. Below are the main types of
conditional formatting commonly available in Microsoft Excel:
Cell Value-Based: Formats cells based on their values (e.g.,
greater than, less than).
Color Scales: Applies gradient colors to create heat maps.
Data Bars: Adds horizontal bars proportional to cell values.
Icon Sets: Inserts icons (e.g., arrows, traffic lights) to represent
data trends.
Top/Bottom Rules: Highlights top or bottom values (e.g., top
10%, above average).
Duplicate/Unique Values: Highlights duplicate or unique
entries in a dataset.
Date-Based: Formats cells based on date criteria (e.g., past due,
upcoming).
Blank/Non-Blank: Highlights empty or non-empty cells.
Custom Formula-Based: Uses formulas to define complex
formatting rules.
Text-Based: Formats cells based on text content (e.g., contains,
starts with).
Row/Column-Based: Formats entire rows or columns based on a
condition in one cell.
Error or Warning Formatting: Highlights cells with errors or
invalid data.
Add Conditional Formatting to Excel in Python
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.
Example 1: Cell Value-Based Conditional Formatting
Cell value-based conditional formatting allows you to apply formatting
rules based on the numeric values in a cell. This is useful for
highlighting cells that meet specific criteria, such as values greater than
or less than a certain threshold.
from spire.xls import *
# Initialize a new workbook and load an existing Excel file
workbook = Workbook()
workbook.LoadFromFile("conditional_formatting_example.xlsx")
# Access the first worksheet in the workbook
worksheet = workbook.Worksheets[0]
# Create a conditional formatting rule for the range "B2:B6"
format = worksheet.ConditionalFormats.Add()
format.AddRange(worksheet.Range["B2:B6"])
# Define the condition for the formatting rule
condition = format.AddCondition()
# Set the format type to cell value
condition.FormatType = ConditionalFormatType.CellValue
# Set the comparison operator to 'greater than'
condition.Operator = ComparisonOperatorType.Greater
# Specify the threshold value
condition.FirstFormula = "75"
# Set the background color to yellow
condition.BackColor = Color.FromRgb(204, 204, 0)
# Save the modified workbook to a new file
workbook.SaveToFile("CellValueBased.xlsx", ExcelVersion.Version2016)
# Release resources associated with the workbook
workbook.Dispose()
Set Cell Value Based Conditional Formatting in Excel in Python
Example 2: Color Scales
Color scales provide a visual representation of data by applying a
gradient of colors based on the values in the cells. This is particularly
effective for identifying trends and patterns in large datasets.
from spire.xls import *
# Initialize a new workbook and load an existing Excel file
workbook = Workbook()
workbook.LoadFromFile("conditional_formatting_example.xlsx")
# Access the first worksheet in the workbook
worksheet = workbook.Worksheets[0]
# Create a conditional formatting rule for the range "B2:B6"
format = worksheet.ConditionalFormats.Add()
format.AddRange(worksheet.Range["B2:B6"])
# Define the condition for the formatting rule
condition = format.AddCondition()
# Set the format type to color scale
condition.FormatType = ConditionalFormatType.ColorScale
# Save the modified workbook to a new file
workbook.SaveToFile("ColorScales.xlsx", ExcelVersion.Version2016)
# Release resources associated with the workbook
workbook.Dispose()
Set Color Scales Conditional Formatting in Excel in Python
Example 3: Data Bars
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.
from spire.xls import *
# Initialize a new workbook and load an existing Excel file
workbook = Workbook()
workbook.LoadFromFile("conditional_formatting_example.xlsx")
# Access the first worksheet in the workbook
worksheet = workbook.Worksheets[0]
# Create a conditional formatting rule for the range "B2:B6"
format = worksheet.ConditionalFormats.Add()
format.AddRange(worksheet.Range["B2:B6"])
# Define the condition for the formatting rule
condition = format.AddCondition()
# Set the format type to data bar
condition.FormatType = ConditionalFormatType.DataBar
# Use a gradient fill for the data bar
condition.DataBar.BarFillType = DataBarFillType.DataBarFillGradient
# Set the color of the data bar
condition.DataBar.BarColor = Color.FromRgb(99, 142, 198)
# Save the modified workbook to a new file
workbook.SaveToFile("DataBars.xlsx", ExcelVersion.Version2016)
# Release resources associated with the workbook
workbook.Dispose()
Set Data Bars Conditional Formatting in Excel in Python
Example 4: Icon Sets
Icon sets use symbols to represent data trends and categories. By
applying icons such as arrows or traffic lights, you can quickly convey
the status or comparison of values in your dataset.
from spire.xls import *
# Initialize a new workbook and load an existing Excel file
workbook = Workbook()
workbook.LoadFromFile("conditional_formatting_example.xlsx")
# Access the first worksheet in the workbook
worksheet = workbook.Worksheets[0]
# Create a conditional formatting rule for the range "B2:B6"
format = worksheet.ConditionalFormats.Add()
format.AddRange(worksheet.Range["B2:B6"])
# Define the condition for the formatting rule
condition = format.AddCondition()
# Set the format type to icon set
condition.FormatType = ConditionalFormatType.IconSet
# Define the type of icon set to four arrows
condition.IconSet.IconSetType = IconSetType.FourArrows
# Save the modified workbook to a new file
workbook.SaveToFile("IconSets.xlsx", ExcelVersion.Version2016)
# Release resources associated with the workbook
workbook.Dispose()
Set Icon Sets Conditional Formatting in Excel in Python
Example 5: Top/Bottom Rules
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.
from spire.xls import *
# Initialize a new workbook and load an existing Excel file
workbook = Workbook()
workbook.LoadFromFile("conditional_formatting_example.xlsx")
# Access the first worksheet in the workbook
sheet = workbook.Worksheets[0]
# Create a conditional formatting rule for the range "B2:B6"
format_1 = sheet.ConditionalFormats.Add()
format_1.AddRange(sheet.Range["B2:B6"])
# 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()
# Create another conditional formatting rule for the range "B2:B6"
format_2 = sheet.ConditionalFormats.Add()
format_2.AddRange(sheet.Range["B2:B6"])
# 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()
# Save the modified workbook to a new file
workbook.SaveToFile("TopOrBottomRules.xlsx", ExcelVersion.Version2016)
# Release resources associated with the workbook
workbook.Dispose()
Set Top or Bottom Rules Conditional Formatting in Excel in Python
Example 6: Duplicate/Unique Values
This type of formatting helps identify duplicate or unique entries in
your data. It is particularly useful for data validation and ensuring data
integrity.
from spire.xls import *
# Initialize a new workbook and load an existing Excel file
workbook = Workbook()
workbook.LoadFromFile("conditional_formatting_example.xlsx")
# Access the first worksheet in the workbook
sheet = workbook.Worksheets[0]
# Create a conditional formatting rule for the range "B2:B6"
format_1 = sheet.ConditionalFormats.Add()
format_1.AddRange(sheet.Range["B2:B6"])
# Add a condition to format cells that contain duplicate values
condition_1 = format_1.AddCondition()
condition_1.FormatType = ConditionalFormatType.DuplicateValues
# Set the background color for the duplicate values to light yellow
condition_1.BackColor = Color.get_LightYellow()
# Create another conditional formatting rule for the range "B2:B6"
format_2 = sheet.ConditionalFormats.Add()
format_2.AddRange(sheet.Range["B2:B6"])
# Add a condition to format cells that contain unique values
condition_2 = format_2.AddCondition()
condition_2.FormatType = ConditionalFormatType.UniqueValues
# Set the background color for the unique values to sky blue
condition_2.BackColor = Color.get_SkyBlue()
# Save the modified workbook to a new file
workbook.SaveToFile("DuplicateOrUniqueValues.xlsx", ExcelVersion.Version2016)
# Release resources associated with the workbook
workbook.Dispose()
Set Duplicate or Unique Values Conditional Formatting in Excel in Python
Example 7: Date-Based
Date-based conditional formatting allows you to format cells based on
specific date criteria, such as highlighting past due dates. This is
valuable for managing deadlines and timelines.
from spire.xls import *
# Initialize a new workbook and load an existing Excel file
workbook = Workbook()
workbook.LoadFromFile("conditional_formatting_example.xlsx")
# Access the first worksheet in the workbook
sheet = workbook.Worksheets[0]
# Create a conditional formatting rule for the range "B2:B6"
format = sheet.ConditionalFormats.Add()
format.AddRange(sheet.Range["C2:C6"])
# 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()
# Save the modified workbook to a new file
workbook.SaveToFile("DateBasedFormatting.xlsx", ExcelVersion.Version2016)
# Release resources associated with the workbook
workbook.Dispose()
Set Date Based Conditional Formatting in Excel in Python
Complete Code Example
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:
from spire.xls import *
# Adds various types of conditional formatting to a new Excel sheet.
def AddConditionalFormattingForNewSheet(sheet):
# Add default icon sets to the sheet
AddDefaultIconSet(sheet)
AddIconSet2(sheet)
AddIconSet3(sheet)
AddIconSet4(sheet)
AddIconSet5(sheet)
AddIconSet6(sheet)
AddIconSet7(sheet)
AddIconSet8(sheet)
AddIconSet9(sheet)
AddIconSet10(sheet)
AddIconSet11(sheet)
AddIconSet12(sheet)
AddIconSet13(sheet)
AddIconSet14(sheet)
AddIconSet15(sheet)
AddIconSet16(sheet)
AddIconSet17(sheet)
AddIconSet18(sheet)
# Add default color scales
AddDefaultColorScale(sheet)
Add3ColorScale(sheet)
Add2ColorScale(sheet)
# Add various average-based conditional formatting
AddAboveAverage(sheet)
AddAboveAverage2(sheet)
AddAboveAverage3(sheet)
# Add top/bottom 10 conditional formatting
AddTop10_1(sheet)
AddTop10_2(sheet)
AddTop10_3(sheet)
AddTop10_4(sheet)
# Add data bar conditional formatting
AddDataBar1(sheet)
AddDataBar2(sheet)
# Add text-based conditional formatting
AddContainsText(sheet)
AddNotContainsText(sheet)
AddContainsBlank(sheet)
AddNotContainsBlank(sheet)
AddBeginWith(sheet)
AddEndWith(sheet)
AddContainsError(sheet)
AddNotContainsError(sheet)
AddDuplicate(sheet)
AddUnique(sheet)
# Add time period based conditional formatting
AddTimePeriod_1(sheet)
AddTimePeriod_2(sheet)
AddTimePeriod_3(sheet)
AddTimePeriod_4(sheet)
AddTimePeriod_5(sheet)
AddTimePeriod_6(sheet)
AddTimePeriod_7(sheet)
AddTimePeriod_8(sheet)
AddTimePeriod_9(sheet)
AddTimePeriod_10(sheet)
# Configure the layout of the sheet
sheet.AllocatedRange.ColumnWidth = 15
sheet.AllocatedRange.AutoFitRows()
# This method implements the IconSet conditional formatting type with a
ThreeArrows icon set.
def AddIconSet2(sheet):
xcfs = sheet.ConditionalFormats.Add()
xcfs.AddRange(sheet.Range["M1:O2"])
sheet.Range["M1:O2"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["M1:O2"].Style.Color = Color.get_AliceBlue()
cf = xcfs.AddCondition()
cf.FormatType = ConditionalFormatType.IconSet
cf.IconSet.IconSetType = IconSetType.ThreeArrows
# Set values and labels for the icon set
sheet.Range["M1"].Text = "ThreeArrows"
sheet.Range["N1"].NumberValue = 15
sheet.Range["O1"].NumberValue = 18
sheet.Range["M2"].NumberValue = 14
sheet.Range["N2"].NumberValue = 17
sheet.Range["O2"].NumberValue = 20
# This method implements the IconSet conditional formatting type with a
FourArrows icon set.
def AddIconSet3(sheet):
xcfs = sheet.ConditionalFormats.Add()
xcfs.AddRange(sheet.Range["M3:O4"])
sheet.Range["M3:O4"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["M3:O4"].Style.Color = Color.get_AntiqueWhite()
cf = xcfs.AddCondition()
cf.FormatType = ConditionalFormatType.IconSet
cf.IconSet.IconSetType = IconSetType.FourArrows
# Set values and labels for the FourArrows icon set
sheet.Range["M3"].Text = "FourArrows"
sheet.Range["N3"].NumberValue = 17
sheet.Range["O3"].NumberValue = 20
sheet.Range["M4"].NumberValue = 16
sheet.Range["N4"].NumberValue = 19
sheet.Range["O4"].NumberValue = 22
# This method implements the IconSet conditional formatting type with a
FiveArrows icon set.
def AddIconSet4(sheet):
xcfs = sheet.ConditionalFormats.Add()
xcfs.AddRange(sheet.Range["M5:O6"])
sheet.Range["M5:O6"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["M5:O6"].Style.Color = Color.get_Aqua()
cf = xcfs.AddCondition()
cf.FormatType = ConditionalFormatType.IconSet
cf.IconSet.IconSetType = IconSetType.FiveArrows
# Set values and labels for the FiveArrows icon set
sheet.Range["M5"].Text = "FiveArrows"
sheet.Range["N5"].NumberValue = 17
sheet.Range["O5"].NumberValue = 20
sheet.Range["M6"].NumberValue = 16
sheet.Range["N6"].NumberValue = 19
sheet.Range["O6"].NumberValue = 22
# This method implements the IconSet conditional formatting type with a
ThreeArrowsGray icon set.
def AddIconSet5(sheet):
xcfs = sheet.ConditionalFormats.Add()
xcfs.AddRange(sheet.Range["M7:O8"])
sheet.Range["M7:O8"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["M7:O8"].Style.Color = Color.get_Aquamarine()
cf = xcfs.AddCondition()
cf.FormatType = ConditionalFormatType.IconSet
cf.IconSet.IconSetType = IconSetType.ThreeArrowsGray
# Set values and labels for the ThreeArrowsGray icon set
sheet.Range["M7"].Text = "ThreeArrowsGray"
sheet.Range["N7"].NumberValue = 21
sheet.Range["O7"].NumberValue = 24
sheet.Range["M8"].NumberValue = 20
sheet.Range["N8"].NumberValue = 23
sheet.Range["O8"].NumberValue = 26
# This method implements the IconSet conditional formatting type with a
FourArrowsGray icon set.
def AddIconSet6(sheet):
xcfs = sheet.ConditionalFormats.Add()
xcfs.AddRange(sheet.Range["M9:O10"])
sheet.Range["M9:O10"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["M9:O10"].Style.Color = Color.get_Azure()
cf = xcfs.AddCondition()
cf.FormatType = ConditionalFormatType.IconSet
cf.IconSet.IconSetType = IconSetType.FourArrowsGray
# Set values and labels for the FourArrowsGray icon set
sheet.Range["M9"].Text = "FourArrowsGray"
sheet.Range["N9"].NumberValue = 23
sheet.Range["O9"].NumberValue = 26
sheet.Range["M10"].NumberValue = 22
sheet.Range["N10"].NumberValue = 25
sheet.Range["O10"].NumberValue = 28
# This method implements the IconSet conditional formatting type with a
FiveArrowsGray icon set.
def AddIconSet7(sheet):
xcfs = sheet.ConditionalFormats.Add()
xcfs.AddRange(sheet.Range["M11:O12"])
sheet.Range["M11:O12"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["M11:O12"].Style.Color = Color.get_Beige()
cf = xcfs.AddCondition()
cf.FormatType = ConditionalFormatType.IconSet
cf.IconSet.IconSetType = IconSetType.FiveArrowsGray
# Set values and labels for the FiveArrowsGray icon set
sheet.Range["M11"].Text = "FiveArrowsGray"
sheet.Range["N11"].NumberValue = 25
sheet.Range["O11"].NumberValue = 28
sheet.Range["M12"].NumberValue = 24
sheet.Range["N12"].NumberValue = 27
sheet.Range["O12"].NumberValue = 30
# This method implements the IconSet conditional formatting type with a
ThreeFlags icon set.
def AddIconSet8(sheet):
xcfs = sheet.ConditionalFormats.Add()
xcfs.AddRange(sheet.Range["M13:O14"])
sheet.Range["M13:O14"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["M13:O14"].Style.Color = Color.get_Bisque()
cf = xcfs.AddCondition()
cf.FormatType = ConditionalFormatType.IconSet
cf.IconSet.IconSetType = IconSetType.ThreeFlags
# Set values and labels for the ThreeFlags icon set
sheet.Range["M13"].Text = "ThreeFlags"
sheet.Range["N13"].NumberValue = 27
sheet.Range["O13"].NumberValue = 30
sheet.Range["M14"].NumberValue = 26
sheet.Range["N14"].NumberValue = 29
sheet.Range["O14"].NumberValue = 32
# This method implements the IconSet conditional formatting type with a
FiveQuarters icon set.
def AddIconSet9(sheet):
xcfs = sheet.ConditionalFormats.Add()
xcfs.AddRange(sheet.Range["M15:O16"])
sheet.Range["M15:O16"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["M15:O16"].Style.Color = Color.get_BlanchedAlmond()
cf = xcfs.AddCondition()
cf.FormatType = ConditionalFormatType.IconSet
cf.IconSet.IconSetType = IconSetType.FiveQuarters
# Set values and labels for the FiveQuarters icon set
sheet.Range["M15"].Text = "FiveQuarters"
sheet.Range["N15"].NumberValue = 29
sheet.Range["O15"].NumberValue = 32
sheet.Range["M16"].NumberValue = 28
sheet.Range["N16"].NumberValue = 31
sheet.Range["O16"].NumberValue = 34
# This method implements the IconSet conditional formatting type with a
FourRating icon set.
def AddIconSet10(sheet):
xcfs = sheet.ConditionalFormats.Add()
xcfs.AddRange(sheet.Range["M17:O18"])
sheet.Range["M17:O18"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["M17:O18"].Style.Color = Color.get_LightBlue()
cf = xcfs.AddCondition()
cf.FormatType = ConditionalFormatType.IconSet
cf.IconSet.IconSetType = IconSetType.FourRating
# Set values and labels for the FourRating icon set
sheet.Range["M17"].Text = "FourRating"
sheet.Range["N17"].NumberValue = 31
sheet.Range["O17"].NumberValue = 34
sheet.Range["M18"].NumberValue = 30
sheet.Range["N18"].NumberValue = 33
sheet.Range["O18"].NumberValue = 36
# This method implements the IconSet conditional formatting type with a
FiveRating icon set.
def AddIconSet11(sheet):
xcfs = sheet.ConditionalFormats.Add()
xcfs.AddRange(sheet.Range["M19:O20"])
sheet.Range["M19:O20"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["M19:O20"].Style.Color = Color.get_BlueViolet()
cf = xcfs.AddCondition()
cf.FormatType = ConditionalFormatType.IconSet
cf.IconSet.IconSetType = IconSetType.FiveRating
# Set values and labels for the FiveRating icon set
sheet.Range["M19"].Text = "FiveRating"
sheet.Range["N19"].NumberValue = 33
sheet.Range["O19"].NumberValue = 36
sheet.Range["M20"].NumberValue = 32
sheet.Range["N20"].NumberValue = 35
sheet.Range["O20"].NumberValue = 38
# This method implements the IconSet conditional formatting type with a
FourRedToBlack icon set.
def AddIconSet12(sheet):
xcfs = sheet.ConditionalFormats.Add()
xcfs.AddRange(sheet.Range["M21:O22"])
sheet.Range["M21:O22"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["M21:O22"].Style.Color = Color.get_Brown()
cf = xcfs.AddCondition()
cf.FormatType = ConditionalFormatType.IconSet
cf.IconSet.IconSetType = IconSetType.FourRedToBlack
# Set values and labels for the FourRedToBlack icon set
sheet.Range["M21"].Text = "FourRedToBlack"
sheet.Range["N21"].NumberValue = 35
sheet.Range["O21"].NumberValue = 38
sheet.Range["M22"].NumberValue = 34
sheet.Range["N22"].NumberValue = 37
sheet.Range["O22"].NumberValue = 40
# This method implements the IconSet conditional formatting type with a
ThreeSigns icon set.
def AddIconSet13(sheet):
xcfs = sheet.ConditionalFormats.Add()
xcfs.AddRange(sheet.Range["M23:O24"])
sheet.Range["M23:O24"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["M23:O24"].Style.Color = Color.get_BurlyWood()
cf = xcfs.AddCondition()
cf.FormatType = ConditionalFormatType.IconSet
cf.IconSet.IconSetType = IconSetType.ThreeSigns
# Set values and labels for the ThreeSigns icon set
sheet.Range["M23"].Text = "ThreeSigns"
sheet.Range["N23"].NumberValue = 37
sheet.Range["O23"].NumberValue = 40
sheet.Range["M24"].NumberValue = 36
sheet.Range["N24"].NumberValue = 39
sheet.Range["O24"].NumberValue = 42
# This method implements the IconSet conditional formatting type with a
ThreeSymbols icon set.
def AddIconSet14(sheet):
xcfs = sheet.ConditionalFormats.Add()
xcfs.AddRange(sheet.Range["M25:O26"])
sheet.Range["M25:O26"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["M25:O26"].Style.Color = Color.get_CadetBlue()
cf = xcfs.AddCondition()
cf.FormatType = ConditionalFormatType.IconSet
cf.IconSet.IconSetType = IconSetType.ThreeSymbols
# Set values and labels for the ThreeSymbols icon set
sheet.Range["M25"].Text = "ThreeSymbols"
sheet.Range["N25"].NumberValue = 39
sheet.Range["O25"].NumberValue = 42
sheet.Range["M26"].NumberValue = 38
sheet.Range["N26"].NumberValue = 41
sheet.Range["O26"].NumberValue = 44
# This method implements the IconSet conditional formatting type with a
ThreeSymbols2 icon set.
def AddIconSet15(sheet):
xcfs = sheet.ConditionalFormats.Add()
xcfs.AddRange(sheet.Range["M27:O28"])
sheet.Range["M27:O28"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["M27:O28"].Style.Color = Color.get_Chartreuse()
cf = xcfs.AddCondition()
cf.FormatType = ConditionalFormatType.IconSet
cf.IconSet.IconSetType = IconSetType.ThreeSymbols2
# Set values and labels for the ThreeSymbols2 icon set
sheet.Range["M27"].Text = "ThreeSymbols2"
sheet.Range["N27"].NumberValue = 41
sheet.Range["O27"].NumberValue = 44
sheet.Range["M28"].NumberValue = 40
sheet.Range["N28"].NumberValue = 43
sheet.Range["O28"].NumberValue = 46
# This method implements the IconSet conditional formatting type with a
ThreeTrafficLights1 icon set.
def AddIconSet16(sheet):
xcfs = sheet.ConditionalFormats.Add()
xcfs.AddRange(sheet.Range["M29:O30"])
sheet.Range["M29:O30"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["M29:O30"].Style.Color = Color.get_Chocolate()
cf = xcfs.AddCondition()
cf.FormatType = ConditionalFormatType.IconSet
cf.IconSet.IconSetType = IconSetType.ThreeTrafficLights1
# Set values and labels for the ThreeTrafficLights1 icon set
sheet.Range["M29"].Text = "ThreeTrafficLights1"
sheet.Range["N29"].NumberValue = 43
sheet.Range["O29"].NumberValue = 46
sheet.Range["M30"].NumberValue = 42
sheet.Range["N30"].NumberValue = 45
sheet.Range["O30"].NumberValue = 48
# This method implements the IconSet conditional formatting type with a
ThreeTrafficLights2 icon set.
def AddIconSet17(sheet):
xcfs = sheet.ConditionalFormats.Add()
xcfs.AddRange(sheet.Range["M31:O32"])
sheet.Range["M31:O32"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["M31:O32"].Style.Color = Color.get_Coral()
cf = xcfs.AddCondition()
cf.FormatType = ConditionalFormatType.IconSet
cf.IconSet.IconSetType = IconSetType.ThreeTrafficLights2
# Set values and labels for the ThreeTrafficLights2 icon set
sheet.Range["M31"].Text = "ThreeTrafficLights2"
sheet.Range["N31"].NumberValue = 45
sheet.Range["O31"].NumberValue = 48
sheet.Range["M32"].NumberValue = 44
sheet.Range["N32"].NumberValue = 47
sheet.Range["O32"].NumberValue = 50
# This method implements the IconSet conditional formatting type with a
FourTrafficLights icon set.
def AddIconSet18(sheet):
xcfs = sheet.ConditionalFormats.Add()
xcfs.AddRange(sheet.Range["M33:O35"])
sheet.Range["M33:O35"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["M33:O35"].Style.Color = Color.get_CornflowerBlue()
cf = xcfs.AddCondition()
cf.FormatType = ConditionalFormatType.IconSet
cf.IconSet.IconSetType = IconSetType.FourTrafficLights
# Set values and labels for the FourTrafficLights icon set
sheet.Range["M33"].Text = "FourTrafficLights"
sheet.Range["N33"].NumberValue = 48
sheet.Range["O33"].NumberValue = 52
sheet.Range["M34"].NumberValue = 46
sheet.Range["N34"].NumberValue = 50
sheet.Range["O34"].NumberValue = 54
sheet.Range["M35"].NumberValue = 48
sheet.Range["N35"].NumberValue = 52
sheet.Range["O35"].NumberValue = 56
# This method implements the TimePeriod conditional formatting type with
Yesterday attribute.
def AddTimePeriod_10(sheet):
conds = sheet.ConditionalFormats.Add()
conds.AddRange(sheet.Range["I19:K20"])
sheet.Range["I19:K20"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["I19:K20"].Style.Color = Color.get_MediumSeaGreen()
cf = conds.AddTimePeriodCondition(TimePeriodType.Yesterday)
cf.FillPattern = ExcelPatternType.Solid
cf.BackColor = Color.get_Pink()
# Set date values for the Yesterday condition
c = sheet.Range["I19"]
c.Value2 = DateTime.get_Now().AddDays(-2).Date
c = sheet.Range["J19"]
c.Value2 = DateTime.get_Now().AddDays(-1).Date
c = sheet.Range["K19"]
c.Value2 = DateTime.get_Now().Date
c = sheet.Range["I20"]
c.Text = "Yesterday"
c = sheet.Range["J20"]
c.Value2 = DateTime.get_Now().AddDays(1).Date
c = sheet.Range["K20"]
c.Value2 = DateTime.get_Now().AddDays(2).Date
# This method implements the TimePeriod conditional formatting type with
Tomorrow attribute.
def AddTimePeriod_9(sheet):
conds = sheet.ConditionalFormats.Add()
conds.AddRange(sheet.Range["I17:K18"])
sheet.Range["I17:K18"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["I17:K18"].Style.Color = Color.get_MediumPurple()
cf = conds.AddTimePeriodCondition(TimePeriodType.Tomorrow)
cf.FillPattern = ExcelPatternType.Solid
cf.BackColor = Color.get_Pink()
# Set date values for the Tomorrow condition
c = sheet.Range["I17"]
c.Value2 = DateTime.get_Now().AddDays(-2).Date
c = sheet.Range["J17"]
c.Value2 = DateTime.get_Now().AddDays(-1).Date
c = sheet.Range["K17"]
c.Value2 = DateTime.get_Now().Date
c = sheet.Range["I18"]
c.Text = "Tomorrow"
c = sheet.Range["J18"]
c.Value2 = DateTime.get_Now().AddDays(1).Date
c = sheet.Range["K18"]
c.Value2 = DateTime.get_Now().AddDays(2).Date
# This method implements the TimePeriod conditional formatting type with
ThisWeek attribute.
def AddTimePeriod_8(sheet):
conds = sheet.ConditionalFormats.Add()
conds.AddRange(sheet.Range["I15:K16"])
sheet.Range["I15:K16"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["I15:K16"].Style.Color = Color.get_MediumOrchid()
cf = conds.AddTimePeriodCondition(TimePeriodType.ThisWeek)
cf.FillPattern = ExcelPatternType.Solid
cf.BackColor = Color.get_Pink()
# Set date values for the ThisWeek condition
c = sheet.Range["I15"]
c.Value2 = DateTime.get_Now().AddDays(-2).Date
c = sheet.Range["J15"]
c.Value2 = DateTime.get_Now().AddDays(-1).Date
c = sheet.Range["K15"]
c.Value2 = DateTime.get_Now().Date
c = sheet.Range["I16"]
c.Text = "ThisWeek"
c = sheet.Range["J16"]
c.Value2 = DateTime.get_Now().AddDays(2).Date
c = sheet.Range["K16"]
c.Value2 = DateTime.get_Now().AddDays(3).Date
# This method implements the TimePeriod conditional formatting type with
ThisMonth attribute.
def AddTimePeriod_7(sheet):
conds = sheet.ConditionalFormats.Add()
conds.AddRange(sheet.Range["I13:K14"])
sheet.Range["I13:K14"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["I13:K14"].Style.Color = Color.get_MediumBlue()
cf = conds.AddTimePeriodCondition(TimePeriodType.ThisMonth)
cf.FillPattern = ExcelPatternType.Solid
cf.BackColor = Color.get_Pink()
# Set date values for the ThisMonth condition
c = sheet.Range["I13"]
c.Value2 = DateTime.get_Now().AddMonths(-1).Date
c = sheet.Range["J13"]
c.Value2 = DateTime.get_Now().AddDays(-1).Date
c = sheet.Range["K13"]
c.Value2 = DateTime.get_Now().Date
c = sheet.Range["I14"]
c.Text = "ThisMonth"
c = sheet.Range["J14"]
c.Value2 = DateTime.get_Now().AddMonths(1).Date
c = sheet.Range["K14"]
c.Value2 = DateTime.get_Now().AddMonths(2).Date
# This method implements the TimePeriod conditional formatting type with
NextWeek attribute.
def AddTimePeriod_6(sheet):
conds = sheet.ConditionalFormats.Add()
conds.AddRange(sheet.Range["I11:K12"])
sheet.Range["I11:K12"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["I11:K12"].Style.Color = Color.get_MediumAquamarine()
cf = conds.AddTimePeriodCondition(TimePeriodType.NextWeek)
cf.FillPattern = ExcelPatternType.Solid
cf.BackColor = Color.get_Pink()
# Set date values for the NextWeek condition
c = sheet.Range["I11"]
c.Value2 = DateTime.get_Now().AddDays(-3).Date
c = sheet.Range["J11"]
c.Value2 = DateTime.get_Now().AddDays(-2).Date
c = sheet.Range["K11"]
c.Value2 = DateTime.get_Now().Date
c = sheet.Range["I12"]
c.Text = "NextWeek"
c = sheet.Range["J12"]
c.Value2 = DateTime.get_Now().AddDays(3).Date
c = sheet.Range["K12"]
c.Value2 = DateTime.get_Now().AddMonths(4).Date
# This method implements the TimePeriod conditional formatting type with
NextMonth attribute.
def AddTimePeriod_5(sheet):
conds = sheet.ConditionalFormats.Add()
conds.AddRange(sheet.Range["I9:K10"])
sheet.Range["I9:K10"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["I9:K10"].Style.Color = Color.get_Maroon()
cf = conds.AddTimePeriodCondition(TimePeriodType.NextMonth)
cf.FillPattern = ExcelPatternType.Solid
cf.BackColor = Color.get_Pink()
# Set date values for the NextMonth condition
c = sheet.Range["I9"]
c.Value2 = DateTime.get_Now().AddDays(-3).Date
c = sheet.Range["J9"]
c.Value2 = DateTime.get_Now().AddMonths(-1).Date
c = sheet.Range["K9"]
c.Value2 = DateTime.get_Now().Date
c = sheet.Range["I10"]
c.Text = "NextMonth"
c = sheet.Range["J10"]
c.Value2 = DateTime.get_Now().AddMonths(1).Date
c = sheet.Range["K10"]
c.Value2 = DateTime.get_Now().AddMonths(2).Date
# This method implements the TimePeriod conditional formatting type with
LastWeek attribute.
def AddTimePeriod_4(sheet):
conds = sheet.ConditionalFormats.Add()
conds.AddRange(sheet.Range["I7:K8"])
sheet.Range["I7:K8"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["I7:K8"].Style.Color = Color.get_Linen()
cf = conds.AddTimePeriodCondition(TimePeriodType.LastWeek)
cf.FillPattern = ExcelPatternType.Solid
cf.BackColor = Color.get_Pink()
# Set date values for the LastWeek condition
c = sheet.Range["I7"]
c.Value2 = DateTime.get_Now().AddDays(-6).Date
c = sheet.Range["J7"]
c.Value2 = DateTime.get_Now().AddDays(-5).Date
c = sheet.Range["K7"]
c.Value2 = DateTime.get_Now().Date
c = sheet.Range["I8"]
c.Text = "LastWeek"
c = sheet.Range["J8"]
c.Value2 = DateTime.get_Now().AddDays(3).Date
c = sheet.Range["K8"]
c.Value2 = DateTime.get_Now().AddMonths(4).Date
# This method implements the TimePeriod conditional formatting type with
LastMonth attribute.
def AddTimePeriod_3(sheet):
conds = sheet.ConditionalFormats.Add()
conds.AddRange(sheet.Range["I5:K6"])
sheet.Range["I5:K6"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["I5:K6"].Style.Color = Color.get_Linen()
cf = conds.AddTimePeriodCondition(TimePeriodType.LastMonth)
cf.FillPattern = ExcelPatternType.Solid
cf.BackColor = Color.get_Pink()
# Set date values for the LastMonth condition
c = sheet.Range["I5"]
c.Value2 = DateTime.get_Now().AddDays(-6).Date
c = sheet.Range["J5"]
c.Value2 = DateTime.get_Now().AddMonths(-1).Date
c = sheet.Range["K5"]
c.Value2 = DateTime.get_Now().Date
c = sheet.Range["I6"]
c.Text = "LastMonth"
c = sheet.Range["J6"]
c.Value2 = DateTime.get_Now().AddDays(3).Date
c = sheet.Range["K6"]
c.Value2 = DateTime.get_Now().AddMonths(1).Date
# This method implements the TimePeriod conditional formatting type with
Last7Days attribute.
def AddTimePeriod_2(sheet):
conds = sheet.ConditionalFormats.Add()
conds.AddRange(sheet.Range["I3:K4"])
sheet.Range["I3:K4"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["I3:K4"].Style.Color = Color.get_LightSkyBlue()
cf = conds.AddTimePeriodCondition(TimePeriodType.Last7Days)
cf.FillPattern = ExcelPatternType.Solid
cf.BackColor = Color.get_Pink()
# Set date values for the Last7Days condition
c = sheet.Range["I3"]
c.Value2 = DateTime.get_Now().AddDays(-8).Date
c = sheet.Range["J3"]
c.Value2 = DateTime.get_Now().AddDays(-7).Date
c = sheet.Range["K3"]
c.Value2 = DateTime.get_Now().Date
c = sheet.Range["I4"]
c.Text = "Last7Days"
c = sheet.Range["J4"]
c.Value2 = DateTime.get_Now().AddDays(3).Date
c = sheet.Range["K4"]
c.Value2 = DateTime.get_Now().AddMonths(2).Date
# This method implements the TimePeriod conditional formatting type with
Today attribute.
def AddTimePeriod_1(sheet):
conds = sheet.ConditionalFormats.Add()
conds.AddRange(sheet.Range["I1:K2"])
sheet.Range["I1:K2"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["I1:K2"].Style.Color = Color.get_LightSlateGray()
cf = conds.AddTimePeriodCondition(TimePeriodType.Today)
cf.FillPattern = ExcelPatternType.Solid
cf.BackColor = Color.get_Pink()
# Set date values for the Today condition
c = sheet.Range["I1"]
c.Value2 = DateTime.get_Now().AddDays(-8).Date
c = sheet.Range["J1"]
c.Value2 = DateTime.get_Now().AddDays(-7).Date
c = sheet.Range["K1"]
c.Value2 = DateTime.get_Now().Date
c = sheet.Range["I2"]
c.Text = "Today"
c = sheet.Range["J2"]
c.Value2 = DateTime.get_Now().AddDays(3).Date
c = sheet.Range["K2"]
c.Value2 = DateTime.get_Now().AddMonths(2).Date
# This method implements the DuplicateValues conditional formatting type.
def AddDuplicate(sheet):
conds = sheet.ConditionalFormats.Add()
conds.AddRange(sheet.Range["E23:G24"])
sheet.Range["E23:G24"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["E23:G24"].Style.Color = Color.get_LightSlateGray()
cf = conds.AddDuplicateValuesCondition()
cf.FillPattern = ExcelPatternType.Solid
cf.BackColor = Color.get_Pink()
# Set values for the Duplicate condition
c = sheet.Range["E23"]
c.Text = "aa"
c = sheet.Range["F23"]
c.Text = "bb"
c = sheet.Range["G23"]
c.Text = "aa"
c = sheet.Range["E24"]
c.Text = "bbb"
c = sheet.Range["F24"]
c.Text = "bb"
c = sheet.Range["G24"]
c.Text = "ccc"
# This method implements the UniqueValues conditional formatting type.
def AddUnique(sheet):
conds = sheet.ConditionalFormats.Add()
conds.AddRange(sheet.Range["E21:G22"])
sheet.Range["E21:G22"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["E21:G22"].Style.Color = Color.get_LightSalmon()
cf = conds.AddUniqueValuesCondition()
cf.FillPattern = ExcelPatternType.Solid
cf.BackColor = Color.get_Yellow()
# Set values for the Unique condition
c = sheet.Range["E21"]
c.Text = "aa"
c = sheet.Range["F21"]
c.Text = "bb"
c = sheet.Range["G21"]
c.Text = "aa"
c = sheet.Range["E22"]
c.Text = "bbb"
c = sheet.Range["F22"]
c.Text = "bb"
c = sheet.Range["G22"]
c.Text = "ccc"
# This method implements the NotContainsError conditional formatting type.
def AddNotContainsError(sheet):
conds = sheet.ConditionalFormats.Add()
conds.AddRange(sheet.Range["E19:G20"])
sheet.Range["E19:G20"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["E19:G20"].Style.Color = Color.get_LightSeaGreen()
cf = conds.AddNotContainsErrorsCondition()
cf.FillPattern = ExcelPatternType.Solid
cf.BackColor = Color.get_Yellow()
# Set values for the NotContainsError condition
c = sheet.Range["E19"]
c.Text = "aa"
c = sheet.Range["F19"]
c.Text = "=Sum"
c = sheet.Range["G19"]
c.Text = "aa"
c = sheet.Range["E20"]
c.Text = "bbb"
c = sheet.Range["F20"]
c.Text = "sss"
c = sheet.Range["G20"]
c.Text = "=Max"
# This method implements the ContainsErrors conditional formatting type.
def AddContainsError(sheet):
conds = sheet.ConditionalFormats.Add()
conds.AddRange(sheet.Range["E17:G18"])
sheet.Range["E17:G18"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["E17:G18"].Style.Color = Color.get_LightSkyBlue()
cf = conds.AddContainsErrorsCondition()
cf.FillPattern = ExcelPatternType.Solid
cf.BackColor = Color.get_Yellow()
# Set values for the ContainsError condition
c = sheet.Range["E17"]
c.Text = "aa"
c = sheet.Range["F17"]
c.Text = "=Sum"
c = sheet.Range["G17"]
c.Text = "aa"
c = sheet.Range["E18"]
c.Text = "bbb"
c = sheet.Range["F18"]
c.Text = "sss"
c = sheet.Range["G18"]
c.Text = "=Max"
# This method implements the BeginWith conditional formatting type.
def AddBeginWith(sheet):
conds = sheet.ConditionalFormats.Add()
conds.AddRange(sheet.Range["E15:G16"])
sheet.Range["E15:G16"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["E15:G16"].Style.Color = Color.get_LightGoldenrodYellow()
cf = conds.AddBeginsWithCondition("ab")
cf.FillPattern = ExcelPatternType.Solid
cf.BackColor = Color.get_Pink()
# Set values for the BeginWith condition
c = sheet.Range["E15"]
c.Text = "aa"
c = sheet.Range["F15"]
c.Text = "abc"
c = sheet.Range["G15"]
c.Text = "aa"
c = sheet.Range["E16"]
c.Text = "bbb"
c = sheet.Range["F16"]
c.Text = "sss"
c = sheet.Range["G16"]
c.Text = "abcd"
# This method implements the EndWith conditional formatting type.
def AddEndWith(sheet):
conds = sheet.ConditionalFormats.Add()
conds.AddRange(sheet.Range["E13:G14"])
sheet.Range["E13:G14"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["E13:G14"].Style.Color = Color.get_LightGray()
cf = conds.AddEndsWithCondition("ab")
cf.FillPattern = ExcelPatternType.Solid
cf.BackColor = Color.get_Yellow()
# Set values for the EndWith condition
c = sheet.Range["E13"]
c.Text = "aa"
c = sheet.Range["F13"]
c.Text = "abc"
c = sheet.Range["G13"]
c.Text = "aab"
c = sheet.Range["E14"]
c.Text = "bbbc"
c = sheet.Range["F14"]
c.Text = "sab"
c = sheet.Range["G14"]
c.Text = "abcd"
# This method implements the NotContainsBlank conditional formatting type.
def AddNotContainsBlank(sheet):
conds = sheet.ConditionalFormats.Add()
conds.AddRange(sheet.Range["E11:G12"])
sheet.Range["E11:G12"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["E11:G12"].Style.Color = Color.get_LightCoral()
cf = conds.AddNotContainsBlanksCondition()
cf.FillPattern = ExcelPatternType.Solid
cf.BackColor = Color.get_Pink()
# Set values for the NotContainsBlank condition
c = sheet.Range["E11"]
c.Text = "aa"
c = sheet.Range["F11"]
c.Text = " "
c = sheet.Range["G11"]
c.Text = "aab"
c = sheet.Range["E12"]
c.Text = "abc"
c = sheet.Range["F12"]
c.Text = " "
c = sheet.Range["G12"]
c.Text = "abcd"
# This method implements the ContainsBlank conditional formatting type.
def AddContainsBlank(sheet):
conds = sheet.ConditionalFormats.Add()
conds.AddRange(sheet.Range["E9:G10"])
sheet.Range["E9:G10"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["E9:G10"].Style.Color = Color.get_LightCyan()
cf = conds.AddContainsBlanksCondition()
cf.FillPattern = ExcelPatternType.Solid
cf.BackColor = Color.get_Yellow()
# Set values for the ContainsBlank condition
c = sheet.Range["E9"]
c.Text = "aa"
c = sheet.Range["F9"]
c.Text = " "
c = sheet.Range["G9"]
c.Text = "aab"
c = sheet.Range["E10"]
c.Text = "abc"
c = sheet.Range["F10"]
c.Text = "dvdf"
c = sheet.Range["G10"]
c.Text = "abcd"
# This method implements the NotContainsText conditional formatting type.
def AddNotContainsText(sheet):
conds = sheet.ConditionalFormats.Add()
conds.AddRange(sheet.Range["E7:G8"])
sheet.Range["E7:G8"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["E7:G8"].Style.Color = Color.get_LightGreen()
cf = conds.AddNotContainsTextCondition("abc")
cf.FillPattern = ExcelPatternType.Solid
cf.BackColor = Color.get_Pink()
# Set values for the NotContainsText condition
c = sheet.Range["E7"]
c.Text = "aa"
c = sheet.Range["F7"]
c.Text = "abfd"
c = sheet.Range["G7"]
c.Text = "aab"
c = sheet.Range["E8"]
c.Text = "abc"
c = sheet.Range["F8"]
c.Text = "cedf"
c = sheet.Range["G8"]
c.Text = "abcd"
# This method implements the ContainsText conditional formatting type.
def AddContainsText(sheet):
conds = sheet.ConditionalFormats.Add()
conds.AddRange(sheet.Range["E5:G6"])
sheet.Range["E5:G6"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["E5:G6"].Style.Color = Color.get_LightBlue()
cf = conds.AddContainsTextCondition("abc")
cf.FillPattern = ExcelPatternType.Solid
cf.BackColor = Color.get_Yellow()
# Set values for the ContainsText condition
c = sheet.Range["E5"]
c.Text = "aa"
c = sheet.Range["F5"]
c.Text = "abfd"
c = sheet.Range["G5"]
c.Text = "aab"
c = sheet.Range["E6"]
c.Text = "abc"
c = sheet.Range["F6"]
c.Text = "cedf"
c = sheet.Range["G6"]
c.Text = "abcd"
# This method implements the DataBars conditional formatting type with
Percentile attribute.
def AddDataBar2(sheet):
# Add data bars
xcfs = sheet.ConditionalFormats.Add()
xcfs.AddRange(sheet.Range["E3:G4"])
sheet.Range["E3:G4"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["E3:G4"].Style.Color = Color.get_LightGreen()
cf = xcfs.AddCondition()
cf.FormatType = ConditionalFormatType.DataBar
cf.DataBar.BarColor = Color.get_Orange()
cf.DataBar.MinPoint.Type = ConditionValueType.Percentile
cf.DataBar.MinPoint.Value = Double(30.78)
cf.DataBar.ShowValue = False
# Set numeric values for data bars
c = sheet.Range["E3"]
c.NumberValue = 6
c = sheet.Range["F3"]
c.NumberValue = 9
c = sheet.Range["G3"]
c.NumberValue = 12
c = sheet.Range["E4"]
c.NumberValue = 8
c = sheet.Range["F4"]
c.NumberValue = 11
c = sheet.Range["G4"]
c.NumberValue = 14
# This method implements the DataBars conditional formatting type.
def AddDataBar1(sheet):
# Add data bars
xcfs = sheet.ConditionalFormats.Add()
xcfs.AddRange(sheet.Range["E1:G2"])
sheet.Range["E1:G2"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["E1:G2"].Style.Color = Color.get_YellowGreen()
cf = xcfs.AddCondition()
cf.FormatType = ConditionalFormatType.DataBar
cf.DataBar.BarColor = Color.get_Blue()
cf.DataBar.MinPoint.Type = ConditionValueType.Percent
cf.DataBar.ShowValue = True
# Set numeric values for data bars
c = sheet.Range["E1"]
c.NumberValue = 4
c = sheet.Range["F1"]
c.NumberValue = 7
c = sheet.Range["G1"]
c.NumberValue = 10
c = sheet.Range["E2"]
c.NumberValue = 6
c = sheet.Range["F2"]
c.NumberValue = 9
c = sheet.Range["G2"]
c.NumberValue = 14
# This method implements the IconSet conditional formatting type.
def AddDefaultIconSet(sheet):
xcfs = sheet.ConditionalFormats.Add()
xcfs.AddRange(sheet.Range["A1:C2"])
sheet.Range["A1:C2"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["A1:C2"].Style.Color = Color.get_Yellow()
cf = xcfs.AddCondition()
cf.FormatType = ConditionalFormatType.IconSet
# Set numeric values for the default icon set
sheet.Range["A1"].NumberValue = 0
sheet.Range["B1"].NumberValue = 3
sheet.Range["C1"].NumberValue = 6
sheet.Range["A2"].NumberValue = 2
sheet.Range["B2"].NumberValue = 5
sheet.Range["C2"].NumberValue = 8
# This method implements the ColorScale conditional formatting type.
def AddDefaultColorScale(sheet):
xcfs = sheet.ConditionalFormats.Add()
xcfs.AddRange(sheet.Range["A5:C6"])
sheet.Range["A5:C6"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["A5:C6"].Style.Color = Color.get_Pink()
cf = xcfs.AddCondition()
cf.FormatType = ConditionalFormatType.ColorScale
# Set numeric values for the color scale
sheet.Range["A5"].NumberValue = 4
sheet.Range["B5"].NumberValue = 7
sheet.Range["C5"].NumberValue = 10
sheet.Range["A6"].NumberValue = 6
sheet.Range["B6"].NumberValue = 9
sheet.Range["C6"].NumberValue = 12
# 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
# This method implements the AboveAverage conditional formatting type.
def AddAboveAverage(sheet):
conds = sheet.ConditionalFormats.Add()
conds.AddRange(sheet.Range["A11:C12"])
sheet.Range["A11:C12"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["A11:C12"].Style.Color = Color.get_Tomato()
cf = conds.AddAverageCondition(AverageType.Above)
cf.FillPattern = ExcelPatternType.Solid
cf.BackColor = Color.get_Pink()
# Set numeric values for the AboveAverage condition
sheet.Range["A11"].NumberValue = 10
sheet.Range["B11"].NumberValue = 13
sheet.Range["C11"].NumberValue = 16
sheet.Range["A12"].NumberValue = 12
sheet.Range["B12"].NumberValue = 15
sheet.Range["C12"].NumberValue = 18
# This method implements an BelowEqualAverage conditional formatting type
with some custom attributes.
def AddAboveAverage2(sheet):
conds = sheet.ConditionalFormats.Add()
conds.AddRange(sheet.Range["A13:C14"])
sheet.Range["A13:C14"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["A13:C14"].Style.Color = Color.get_LightPink()
cf = conds.AddAverageCondition(AverageType.BelowEqual)
cf.FillPattern = ExcelPatternType.Solid
cf.BackColor = Color.get_LightSkyBlue()
# Set numeric values for the AboveAverage2 condition
sheet.Range["A13"].NumberValue = 12
sheet.Range["B13"].NumberValue = 15
sheet.Range["C13"].NumberValue = 18
sheet.Range["A14"].NumberValue = 14
sheet.Range["B14"].NumberValue = 17
sheet.Range["C14"].NumberValue = 20
# This method implements an AboveStdDev3 conditional formatting type with
some custom attributes.
def AddAboveAverage3(sheet):
conds = sheet.ConditionalFormats.Add()
conds.AddRange(sheet.Range["A15:C16"])
sheet.Range["A15:C16"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["A15:C16"].Style.Color = Color.get_LightPink()
cf = conds.AddAverageCondition(AverageType.AboveStdDev3)
cf.FillPattern = ExcelPatternType.Solid
cf.BackColor = Color.get_LightSkyBlue()
# Set numeric values for the AboveAverage3 condition
sheet.Range["A15"].NumberValue = 12
sheet.Range["B15"].NumberValue = 15
sheet.Range["C15"].NumberValue = 18
sheet.Range["A16"].NumberValue = 14
sheet.Range["B16"].NumberValue = 17
sheet.Range["C16"].NumberValue = 20
# This method implements a Top10 conditional formatting type.
def AddTop10_1(sheet):
conds = sheet.ConditionalFormats.Add()
conds.AddRange(sheet.Range["A17:C20"])
sheet.Range["A17:C20"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["A17:C20"].Style.Color = Color.get_Gray()
cf = conds.AddTopBottomCondition(TopBottomType.Top, 10)
cf.FillPattern = ExcelPatternType.Solid
cf.BackColor = Color.get_Yellow()
# Set numeric values for the Top10 condition
sheet.Range["A17"].NumberValue = 16
sheet.Range["B17"].NumberValue = 21
sheet.Range["C17"].NumberValue = 26
sheet.Range["A18"].NumberValue = 18
sheet.Range["B18"].NumberValue = 23
sheet.Range["C18"].NumberValue = 28
sheet.Range["A19"].NumberValue = 20
sheet.Range["B19"].NumberValue = 25
sheet.Range["C19"].NumberValue = 30
sheet.Range["A20"].NumberValue = 22
sheet.Range["B20"].NumberValue = 27
sheet.Range["C20"].NumberValue = 32
# This method implements Bottom 10 conditional formatting type.
def AddTop10_2(sheet):
conds = sheet.ConditionalFormats.Add()
conds.AddRange(sheet.Range["A21:C24"])
sheet.Range["A21:C24"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["A21:C24"].Style.Color = Color.get_Green()
cf = conds.AddTopBottomCondition(TopBottomType.Bottom, 10)
cf.FillPattern = ExcelPatternType.Solid
cf.BackColor = Color.get_Pink()
# Set numeric values for the Top10_2 condition
sheet.Range["A21"].NumberValue = 20
sheet.Range["B21"].NumberValue = 25
sheet.Range["C21"].NumberValue = 30
sheet.Range["A22"].NumberValue = 22
sheet.Range["B22"].NumberValue = 27
sheet.Range["C22"].NumberValue = 32
sheet.Range["A23"].NumberValue = 24
sheet.Range["B23"].NumberValue = 29
sheet.Range["C23"].NumberValue = 34
sheet.Range["A24"].NumberValue = 24
sheet.Range["B24"].NumberValue = 31
sheet.Range["C24"].NumberValue = 36
# This method implements TopPercent 10 conditional formatting type with some
custom attributes.
def AddTop10_3(sheet):
conds = sheet.ConditionalFormats.Add()
conds.AddRange(sheet.Range["A25:C28"])
sheet.Range["A25:C28"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["A25:C28"].Style.Color = Color.get_Orange()
cf = conds.AddTopBottomCondition(TopBottomType.TopPercent, 10)
cf.FillPattern = ExcelPatternType.Solid
cf.BackColor = Color.get_Blue()
# Set numeric values for the Top10_3 condition
sheet.Range["A25"].NumberValue = 24
sheet.Range["B25"].NumberValue = 29
sheet.Range["C25"].NumberValue = 34
sheet.Range["A26"].NumberValue = 25
sheet.Range["B26"].NumberValue = 36
sheet.Range["C26"].NumberValue = 32
sheet.Range["A27"].NumberValue = 24
sheet.Range["B27"].NumberValue = 28
sheet.Range["C27"].NumberValue = 31
sheet.Range["A28"].NumberValue = 34
sheet.Range["B28"].NumberValue = 26
sheet.Range["C28"].NumberValue = 32
# This method implements BottomPercent 10 conditional formatting type with
some custom attributes.
def AddTop10_4(sheet):
conds = sheet.ConditionalFormats.Add()
conds.AddRange(sheet.Range["A29:C32"])
sheet.Range["A29:C32"].Style.FillPattern = ExcelPatternType.Solid
sheet.Range["A29:C32"].Style.Color = Color.get_Gold()
cf = conds.AddTopBottomCondition(TopBottomType.BottomPercent, 10)
cf.FillPattern = ExcelPatternType.Solid
cf.BackColor = Color.get_Green()
# Set numeric values for the Top10_4 condition
sheet.Range["A29"].NumberValue = 22
sheet.Range["B29"].NumberValue = 33
sheet.Range["C29"].NumberValue = 38
sheet.Range["A30"].NumberValue = 30
sheet.Range["B30"].NumberValue = 35
sheet.Range["C30"].NumberValue = 39
sheet.Range["A31"].NumberValue = 32
sheet.Range["B31"].NumberValue = 37
sheet.Range["C31"].NumberValue = 43
sheet.Range["A32"].NumberValue = 34
sheet.Range["B32"].NumberValue = 28
sheet.Range["C32"].NumberValue = 32
# Set the output file name
outputFile = "VariousConditionalFormatting.xlsx"
# Load the document from disk and create a new worksheet
workbook = Workbook()
workbook.CreateEmptySheets(1)
sheet = workbook.Worksheets[0]
# Apply conditional formatting to the new sheet
AddConditionalFormattingForNewSheet(sheet)
# Save the workbook to file
workbook.SaveToFile(outputFile, ExcelVersion.Version2016)
workbook.Dispose()
Add Numerous Conditional Formatting to Excel in Python
Conclusion
Mastering conditional formatting in Excel using Python can greatly
enhance your data analysis capabilities. With the Spire.XLS for Python
library, you can automate the application of various formatting rules,
making your reports more visually appealing and easier to interpret.
Additional Resource
Spire.XLS for Python Documentation
More Related Topics to Read
Apply or Remove Row and Column Grouping in Excel with Python
Python — How to Convert Excel XLS or XLSX to PDF
Apply or Remove Filters in Excel with Python (Comprehensive
Guide)
Create, Update and Remove Pivot Tables in Excel with Python