Set Conditional Formats of Excel and ODS files.

Introduction

Conditional formatting is an advanced Microsoft Excel feature that allows you to apply formats to a cell or range of cells and have that formatting change depending on the value of the cell or the value of a formula. For example, you can have a cell appear bold only when the value of the cell is greater than 500. When the value of the cell meets the condition, the specified format is applied to the cell. If the value of the cell does not meet the format condition, the cell’s default formatting is used. In Microsoft Excel, select Format, then Conditional Formatting to open the Conditional Formatting dialog.

Aspose.Cells for Python via .NET supports applying conditional formatting to cells at runtime. This article explains how. It also explains how to calculate the color used by Excel for color scale conditional formatting.

Applying Conditional Formatting

Aspose.Cells for Python via .NET supports conditional formatting in several ways:

  • Using designer spreadsheet
  • Using the copy method.
  • Creating conditional formatting at runtime.

Using Designer Spreadsheet

Developers can create a designer spreadsheet that contains conditional formatting in Microsoft Excel and then open that spreadsheet with Aspose.Cells for Python via .NET. Aspose.Cells for Python via .NET loads and saves the designer spreadsheet, keeping any conditional formatting setting.

Using the Copy Method

Aspose.Cells for Python via .NET allows developers to copy conditional format settings from one cell to another in the worksheet by calling the Range.copy() method.

from aspose.cells import Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Creating a file stream containing the Excel file to be opened
fstream = open(dataDir + "Book1.xlsx", "rb")
# Opening the Excel file through the file stream
workbook = Workbook(fstream)
# Accessing the first worksheet in the Excel file
worksheet = workbook.worksheets[0]
# Copying conditional format settings from cell "A1" to cell "B1"
# worksheet.CopyConditionalFormatting(0, 0, 0, 1);
TotalRowCount = 0
for i in range(len(workbook.worksheets)):
sourceSheet = workbook.worksheets[i]
sourceRange = sourceSheet.cells.max_display_range
destRange = worksheet.cells.create_range(sourceRange.first_row + TotalRowCount, sourceRange.first_column, sourceRange.row_count, sourceRange.column_count)
destRange.copy(sourceRange)
TotalRowCount = sourceRange.row_count + TotalRowCount
# Saving the modified Excel file
workbook.save(dataDir + "output.xls")
# Closing the file stream to free all resources
fstream.close()

Applying Conditional Formatting at Runtime

Aspose.Cells for Python via .NET lets you both add and remove conditional formatting at runtime. The code samples below show how to set conditional formatting:

  1. Instantiate a workbook.
  2. Add an empty conditional format.
  3. Set the range that the formatting should apply to.
  4. Define the formatting conditions.
  5. Save the file.

After this example comes a number of other smaller examples that show how to apply font settings, borders settings, and patterns.

Microsoft Excel 2007 added more advanced conditional formatting that Aspose.Cells for Python via .NET also support. The examples here, illustrate how to use simple formatting, the Microsoft Excel 2007 examples show how to apply more advanced conditional formatting.

from aspose.cells import CellArea, FormatConditionType, OperatorType, Workbook
from aspose.pydrawing import Color
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
filePath = dataDir + "Book1.xlsx"
# Instantiating a Workbook object
workbook = Workbook()
sheet = workbook.worksheets[0]
# Adds an empty conditional formatting
index = sheet.conditional_formattings.add()
fcs = sheet.conditional_formattings[index]
# Sets the conditional format range.
ca = CellArea()
ca.start_row = 0
ca.end_row = 0
ca.start_column = 0
ca.end_column = 0
fcs.add_area(ca)
ca = CellArea()
ca.start_row = 1
ca.end_row = 1
ca.start_column = 1
ca.end_column = 1
fcs.add_area(ca)
# Adds condition.
conditionIndex = fcs.add_condition(FormatConditionType.CELL_VALUE, OperatorType.BETWEEN, "=A2", "100")
# Adds condition.
conditionIndex2 = fcs.add_condition(FormatConditionType.CELL_VALUE, OperatorType.BETWEEN, "50", "100")
# Sets the background color.
fc = fcs[conditionIndex]
fc.style.background_color = Color.red
# Saving the Excel file
workbook.save(dataDir + "output.xls")

Set Font

from aspose.cells import SaveFormat, Workbook
from os import os, path
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create directory if it is not already present.
IsExists = path.isdir(dataDir)
if notIsExists:
os.makedirs(dataDir)
# Instantiating a Workbook object
workbook = Workbook()
# Adding a new worksheet to the Excel object
i = workbook.worksheets.add()
# Obtaining the reference of the newly added worksheet by passing its sheet index
worksheet = workbook.worksheets[i]
# Accessing the "A1" cell from the worksheet
cell = worksheet.cells.get("A1")
# Adding some value to the "A1" cell
cell.put_value("Hello Aspose!")
# Obtaining the style of the cell
style = cell.get_style()
# Setting the font weight to bold
style.font.is_bold = True
# Applying the style to the cell
cell.set_style(style)
# Saving the Excel file
workbook.save(dataDir + "book1.out.xls", SaveFormat.EXCEL_97_TO_2003)

Set Border

from aspose.cells import BorderType, CellArea, CellBorderType, FormatConditionType, OperatorType, Workbook
from aspose.pydrawing import Color
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Instantiating a Workbook object
workbook = Workbook()
sheet = workbook.worksheets[0]
# Adds an empty conditional formatting
index = sheet.conditional_formattings.add()
fcs = sheet.conditional_formattings[index]
# Sets the conditional format range.
ca = CellArea()
ca.start_row = 0
ca.end_row = 5
ca.start_column = 0
ca.end_column = 3
fcs.add_area(ca)
# Adds condition.
conditionIndex = fcs.add_condition(FormatConditionType.CELL_VALUE, OperatorType.BETWEEN, "50", "100")
# Sets the background color.
fc = fcs[conditionIndex]
fc.style.borders.get(BorderType.LEFT_BORDER).line_style = CellBorderType.DASHED
fc.style.borders.get(BorderType.RIGHT_BORDER).line_style = CellBorderType.DASHED
fc.style.borders.get(BorderType.TOP_BORDER).line_style = CellBorderType.DASHED
fc.style.borders.get(BorderType.BOTTOM_BORDER).line_style = CellBorderType.DASHED
fc.style.borders.get(BorderType.LEFT_BORDER).color = Color.from_argb(0, 255, 255)
fc.style.borders.get(BorderType.RIGHT_BORDER).color = Color.from_argb(0, 255, 255)
fc.style.borders.get(BorderType.TOP_BORDER).color = Color.from_argb(0, 255, 255)
fc.style.borders.get(BorderType.BOTTOM_BORDER).color = Color.from_argb(255, 255, 0)
workbook.save(dataDir + "output.xlsx")

Set Pattern

from aspose.cells import BackgroundType, CellArea, FormatConditionType, OperatorType, Workbook
from aspose.pydrawing import Color
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Instantiating a Workbook object
workbook = Workbook()
sheet = workbook.worksheets[0]
# Adds an empty conditional formatting
index = sheet.conditional_formattings.add()
fcs = sheet.conditional_formattings[index]
# Sets the conditional format range.
ca = CellArea()
ca.start_row = 0
ca.end_row = 5
ca.start_column = 0
ca.end_column = 3
fcs.add_area(ca)
# Adds condition.
conditionIndex = fcs.add_condition(FormatConditionType.CELL_VALUE, OperatorType.BETWEEN, "50", "100")
fc = fcs[conditionIndex]
fc.style.pattern = BackgroundType.REVERSE_DIAGONAL_STRIPE
fc.style.foreground_color = Color.from_argb(255, 255, 0)
fc.style.background_color = Color.from_argb(0, 255, 255)
workbook.save(dataDir + "output.xlsx")

Advance topics