How to Add Text Conditional Formatting
Possible Usage Scenarios
Using text-based conditional formatting in spreadsheets is useful for highlighting cells that meet specific textual criteria. This can improve data analysis and make it easier to find key information in a large dataset. Here are some reasons to use text conditional formatting:
- Highlight Specific Text: You can apply formatting based on specific words, phrases, or characters. For example, you might want to highlight all cells that contain the word “Urgent” or “Completed” to easily differentiate tasks in a project.
- Identify Patterns or Trends: If you’re working with categories or statuses (like “High”, “Medium”, “Low”), text conditional formatting can visually distinguish between them, making it easier to track progress or prioritize tasks.
- Error or Data Entry Alerts: Text formatting can flag inconsistent or erroneous entries, such as misspelled words, incomplete text, or incorrect values. This is particularly useful in datasets with a lot of textual input.
- Enhanced Readability: Color-coding text or changing its style (bold, italics, etc.) helps make important information stand out, improving the overall readability of your sheet.
- Dynamic Feedback: You can set up rules that automatically adjust the formatting when text matches certain conditions. This means you don’t have to manually update the formatting as the data changes.
In essence, text conditional formatting helps you quickly spot relevant information, errors, and trends, making it a powerful tool for managing and interpreting textual data.
How to Add Text Conditional Formatting Using Excel
To add text-based conditional formatting in Excel, follow these steps:
- Select the Range of Cells: Highlight the cells where you want to apply the conditional formatting.
- Open the Conditional Formatting Menu: Go to the Home tab in the Excel ribbon. Click on Conditional Formatting in the “Styles” group.
- Choose “New Rule”: From the drop-down menu, select New Rule.
- Select “Format only cells that contain”: In the New Formatting Rule dialog, choose Format only cells that contain under the “Select a Rule Type” section.
- Set the Rule Criteria: In the “Format cells with” section, choose Specific Text from the drop-down. Select either containing, begins with, or ends with, depending on the condition you want to apply. Enter the text you want to format (e.g., a specific word like “Urgent” or “Completed”).
- Choose the Formatting: Click on the Format button. In the Format Cells dialog, you can select the font color, fill color, or any other formatting options you prefer.
- Apply the Rule: Once you’ve set your desired format, click OK to apply the rule. Click OK again in the New Formatting Rule dialog to close it.
- View the Results: The cells containing the text you specified will now have the formatting applied, making it easy to spot relevant information.
How to Add Text Conditional Formatting Using Aspose.Cells for Python via .NET
Aspose.Cells for Python via .NET fully supports the conditional formatting provided by Microsoft Excel 2007 and later versions in XLSX format on cells at runtime. This examples demonstrate an exercise for advanced conditional formatting types including BeginsWith, ContainsBlank, ContainsText and so on.
Format Cell When the Value Starts With Specified Text
from aspose.cells import Workbook
from aspose.cells import Workbook, Worksheet, CellArea, FormatConditionType, IconSetType, FormatConditionValueType, BackgroundType, TimePeriodType
from aspose.pydrawing import Color
from datetime import datetime
import aspose.cells
import os
import pytest
class ConditionalFormatting:
def __init__(self):
self._sheet = None
@staticmethod
def run():
# The path to the documents directory
current_dir = os.path.dirname(os.path.abspath(__file__))
data_dir = os.path.join(current_dir, "data")
obj = ConditionalFormatting()
obj.do_test(data_dir)
def do_test(self, data_dir):
book = Workbook()
sheet1 = book.worksheets[0]
self._sheet = sheet1
self.add_begin_with()
self._sheet.auto_fit_column(12)
output_dir = os.path.join(data_dir, "output")
if not os.path.exists(output_dir):
os.makedirs(output_dir)
out_fn = os.path.join(output_dir, "add_begin_with.out.xlsx")
book.save(out_fn, SaveFormat.XLSX)
def add_begin_with(self):
conds = self.get_format_condition("E15:G16", Color.light_goldenrod_yellow)
idx = conds.add_condition(FormatConditionType.BEGINS_WITH)
cond = conds[idx]
cond.style.background_color = Color.pink
cond.style.pattern = BackgroundType.SOLID
cond.text = "ab"
self._sheet.cells.get("E15").put_value("abc")
self._sheet.cells.get("G16").put_value("babx")
def get_format_condition(self, cell_area_name, color):
index = self._sheet.conditional_formattings.add()
format_conditions = self._sheet.conditional_formattings[index]
area = self.get_cell_area_by_name(cell_area_name)
format_conditions.add_area(area)
self.fill_cell(cell_area_name, color)
return format_conditions
def fill_cell(self, cell_area_name, color):
area = self.get_cell_area_by_name(cell_area_name)
k = 0
for i in range(area.start_column, area.end_column + 1):
for j in range(area.start_row, area.end_row + 1):
c = self._sheet.cells.get(j, i)
if color != Color.empty:
s = c.get_style()
s.foreground_color = color
s.pattern = BackgroundType.SOLID
c.set_style(s)
value = j + i + k
c.put_value(value)
k += 1
@staticmethod
def get_cell_area_by_name(s):
area = CellArea()
str_cell_range = s.replace("$", "").split(':')
start_row, start_col = CellsHelper.cell_name_to_index(str_cell_range[0])
area.start_row = start_row
area.start_column = start_col
if len(str_cell_range) == 1:
area.end_row = start_row
area.end_column = start_col
else:
end_row, end_col = CellsHelper.cell_name_to_index(str_cell_range[1])
area.end_row = end_row
area.end_column = end_col
return area
Format Cell When the Value Contains Blank
from aspose.cells import Workbook
from aspose.cells import Workbook, Worksheet, CellArea, FormatConditionType, IconSetType, FormatConditionValueType, BackgroundType, TimePeriodType
from aspose.pydrawing import Color
from datetime import datetime
import aspose.cells
import os
import pytest
class ConditionalFormatting:
def __init__(self):
self._sheet = None
@staticmethod
def run():
# The path to the documents directory
current_dir = os.path.dirname(os.path.abspath(__file__))
data_dir = os.path.join(current_dir, "data")
obj = ConditionalFormatting()
obj.do_test(data_dir)
def do_test(self, data_dir):
book = Workbook()
sheet1 = book.worksheets[0]
self._sheet = sheet1
self.add_contains_blank()
self._sheet.auto_fit_column(12)
output_dir = os.path.join(data_dir, "output")
if not os.path.exists(output_dir):
os.makedirs(output_dir)
out_fn = os.path.join(output_dir, "add_contains_blank.out.xlsx")
book.save(out_fn, SaveFormat.XLSX)
def add_contains_blank(self):
conds = self.get_format_condition("E9:G10", Color.light_blue)
idx = conds.add_condition(FormatConditionType.CONTAINS_BLANKS)
cond = conds[idx]
cond.style.background_color = Color.yellow
cond.style.pattern = BackgroundType.SOLID
self._sheet.cells.get("E9").put_value(" ")
self._sheet.cells.get("G10").put_value(" ")
Format Cell When the Value Contains Errors
from aspose.cells import Workbook
from aspose.cells import Workbook, Worksheet, CellArea, FormatConditionType, IconSetType, FormatConditionValueType, BackgroundType, TimePeriodType
from aspose.pydrawing import Color
from datetime import datetime
import aspose.cells
import os
import pytest
class ConditionalFormatting:
def __init__(self):
self._sheet = None
@staticmethod
def run():
# The path to the documents directory
current_dir = os.path.dirname(os.path.abspath(__file__))
data_dir = os.path.join(current_dir, "data")
obj = ConditionalFormatting()
obj.do_test(data_dir)
def do_test(self, data_dir):
book = Workbook()
sheet1 = book.worksheets[0]
self._sheet = sheet1
self.add_contains_error()
self._sheet.auto_fit_column(12)
output_dir = os.path.join(data_dir, "output")
if not os.path.exists(output_dir):
os.makedirs(output_dir)
out_fn = os.path.join(output_dir, "add_contains_error.out.xlsx")
book.save(out_fn, SaveFormat.XLSX)
def add_contains_error(self):
conds = self.get_format_condition("E17:G18", Color.light_sky_blue)
idx = conds.add_condition(FormatConditionType.CONTAINS_ERRORS)
cond = conds[idx]
cond.style.background_color = Color.yellow
cond.style.pattern = BackgroundType.SOLID
self._sheet.cells.get("E17").put_value(" ")
self._sheet.cells.get("G18").put_value(" ")
Format Cell When the Value Contains Specified Text
from aspose.cells import Workbook
from aspose.cells import Workbook, Worksheet, CellArea, FormatConditionType, IconSetType, FormatConditionValueType, BackgroundType, TimePeriodType
from aspose.pydrawing import Color
from datetime import datetime
import aspose.cells
import os
import pytest
class ConditionalFormatting:
def __init__(self):
self._sheet = None
@staticmethod
def run():
# The path to the documents directory
current_dir = os.path.dirname(os.path.abspath(__file__))
data_dir = os.path.join(current_dir, "data")
obj = ConditionalFormatting()
obj.do_test(data_dir)
def do_test(self, data_dir):
book = Workbook()
sheet1 = book.worksheets[0]
self._sheet = sheet1
self.add_contains_text()
self._sheet.auto_fit_column(12)
output_dir = os.path.join(data_dir, "output")
if not os.path.exists(output_dir):
os.makedirs(output_dir)
out_fn = os.path.join(output_dir, "add_contains_text.out.xlsx")
book.save(out_fn, SaveFormat.XLSX)
def add_contains_text(self):
conds = self.get_format_condition("E5:G6", Color.light_blue)
idx = conds.add_condition(FormatConditionType.CONTAINS_TEXT)
cond = conds[idx]
cond.style.background_color = Color.yellow
cond.style.pattern = BackgroundType.SOLID
cond.text = "1"
Format Cell When the Value Contains Duplicate Values
from aspose.cells import Workbook
from aspose.cells import Workbook, Worksheet, CellArea, FormatConditionType, IconSetType, FormatConditionValueType, BackgroundType, TimePeriodType
from aspose.pydrawing import Color
from datetime import datetime
import aspose.cells
import os
import pytest
class ConditionalFormatting:
def __init__(self):
self._sheet = None
@staticmethod
def run():
# The path to the documents directory
current_dir = os.path.dirname(os.path.abspath(__file__))
data_dir = os.path.join(current_dir, "data")
obj = ConditionalFormatting()
obj.do_test(data_dir)
def do_test(self, data_dir):
book = Workbook()
sheet1 = book.worksheets[0]
self._sheet = sheet1
self.add_duplicate()
self._sheet.auto_fit_column(12)
output_dir = os.path.join(data_dir, "output")
if not os.path.exists(output_dir):
os.makedirs(output_dir)
out_fn = os.path.join(output_dir, "add_duplicate.out.xlsx")
book.save(out_fn, SaveFormat.XLSX)
def add_duplicate(self):
conds = self.get_format_condition("E23:G24", Color.light_slate_gray)
idx = conds.add_condition(FormatConditionType.DUPLICATE_VALUES)
cond = conds[idx]
cond.style.background_color = Color.pink
cond.style.pattern = BackgroundType.SOLID
self._sheet.cells.get("E23").put_value("bb")
self._sheet.cells.get("G24").put_value("bb")
Format Cell When the Value Ends With Specified Text
from aspose.cells import Workbook
from aspose.cells import Workbook, Worksheet, CellArea, FormatConditionType, IconSetType, FormatConditionValueType, BackgroundType, TimePeriodType
from aspose.pydrawing import Color
from datetime import datetime
import aspose.cells
import os
import pytest
class ConditionalFormatting:
def __init__(self):
self._sheet = None
@staticmethod
def run():
# The path to the documents directory
current_dir = os.path.dirname(os.path.abspath(__file__))
data_dir = os.path.join(current_dir, "data")
obj = ConditionalFormatting()
obj.do_test(data_dir)
def do_test(self, data_dir):
book = Workbook()
sheet1 = book.worksheets[0]
self._sheet = sheet1
self.add_end_with()
self._sheet.auto_fit_column(12)
output_dir = os.path.join(data_dir, "output")
if not os.path.exists(output_dir):
os.makedirs(output_dir)
out_fn = os.path.join(output_dir, "add_end_with.out.xlsx")
book.save(out_fn, SaveFormat.XLSX)
def add_end_with(self):
conds = self.get_format_condition("E13:G14", Color.light_gray)
idx = conds.add_condition(FormatConditionType.ENDS_WITH)
cond = conds[idx]
cond.style.background_color = Color.yellow
cond.style.pattern = BackgroundType.SOLID
cond.text = "ab"
self._sheet.cells.get("E13").put_value("nnnab")
self._sheet.cells.get("G14").put_value("mmmabc")
Format Cell When the Value Not Contains Blank
from aspose.cells import Workbook
from aspose.cells import Workbook, Worksheet, CellArea, FormatConditionType, IconSetType, FormatConditionValueType, BackgroundType, TimePeriodType
from aspose.pydrawing import Color
from datetime import datetime
import aspose.cells
import os
import pytest
class ConditionalFormatting:
def __init__(self):
self._sheet = None
@staticmethod
def run():
# The path to the documents directory
current_dir = os.path.dirname(os.path.abspath(__file__))
data_dir = os.path.join(current_dir, "data")
obj = ConditionalFormatting()
obj.do_test(data_dir)
def do_test(self, data_dir):
book = Workbook()
sheet1 = book.worksheets[0]
self._sheet = sheet1
self.add_not_contains_blank()
self._sheet.auto_fit_column(12)
output_dir = os.path.join(data_dir, "output")
if not os.path.exists(output_dir):
os.makedirs(output_dir)
out_fn = os.path.join(output_dir, "add_not_contains_blank.out.xlsx")
book.save(out_fn, SaveFormat.XLSX)
def add_not_contains_blank(self):
conds = self.get_format_condition("E11:G12", Color.light_coral)
idx = conds.add_condition(FormatConditionType.NOT_CONTAINS_BLANKS)
cond = conds[idx]
cond.style.background_color = Color.pink
cond.style.pattern = BackgroundType.SOLID
self._sheet.cells.get("E11").put_value("abc")
self._sheet.cells.get("G12").put_value(" ")
Format Cell When the Value Not Contains Errors
from aspose.cells import Workbook
from aspose.cells import Workbook, Worksheet, CellArea, FormatConditionType, IconSetType, FormatConditionValueType, BackgroundType, TimePeriodType
from aspose.pydrawing import Color
from datetime import datetime
import aspose.cells
import os
import pytest
class ConditionalFormatting:
def __init__(self):
self._sheet = None
@staticmethod
def run():
# The path to the documents directory
current_dir = os.path.dirname(os.path.abspath(__file__))
data_dir = os.path.join(current_dir, "data")
obj = ConditionalFormatting()
obj.do_test(data_dir)
def do_test(self, data_dir):
book = Workbook()
sheet1 = book.worksheets[0]
self._sheet = sheet1
self.add_not_contains_error()
self._sheet.auto_fit_column(12)
output_dir = os.path.join(data_dir, "output")
if not os.path.exists(output_dir):
os.makedirs(output_dir)
out_fn = os.path.join(output_dir, "add_not_contains_error.out.xlsx")
book.save(out_fn, SaveFormat.XLSX)
def add_not_contains_error(self):
conds = self.get_format_condition("E19:G20", Color.light_sea_green)
idx = conds.add_condition(FormatConditionType.NOT_CONTAINS_ERRORS)
cond = conds[idx]
cond.style.background_color = Color.pink
cond.style.pattern = BackgroundType.SOLID
self._sheet.cells.get("E19").put_value(" ")
self._sheet.cells.get("G20").put_value(" ")
Format Cell When the Value Not Contains Specified Text
from aspose.cells import Workbook
from aspose.cells import Workbook, Worksheet, CellArea, FormatConditionType, IconSetType, FormatConditionValueType, BackgroundType, TimePeriodType
from aspose.pydrawing import Color
from datetime import datetime
import aspose.cells
import os
import pytest
class ConditionalFormatting:
def __init__(self):
self._sheet = None
@staticmethod
def run():
# The path to the documents directory
current_dir = os.path.dirname(os.path.abspath(__file__))
data_dir = os.path.join(current_dir, "data")
obj = ConditionalFormatting()
obj.do_test(data_dir)
def do_test(self, data_dir):
book = Workbook()
sheet1 = book.worksheets[0]
self._sheet = sheet1
self.add_not_contains_text()
self._sheet.auto_fit_column(12)
output_dir = os.path.join(data_dir, "output")
if not os.path.exists(output_dir):
os.makedirs(output_dir)
out_fn = os.path.join(output_dir, "add_not_contains_text.out.xlsx")
book.save(out_fn, SaveFormat.XLSX)
def add_not_contains_text(self):
conds = self.get_format_condition("E7:G8", Color.light_coral)
idx = conds.add_condition(FormatConditionType.NOT_CONTAINS_TEXT)
cond = conds[idx]
cond.style.background_color = Color.pink
cond.style.pattern = BackgroundType.SOLID
cond.text = "3"
Format Cell When the Value Contains Unique Values
from aspose.cells import Workbook
from aspose.cells import Workbook, Worksheet, CellArea, FormatConditionType, IconSetType, FormatConditionValueType, BackgroundType, TimePeriodType
from aspose.pydrawing import Color
from datetime import datetime
import aspose.cells
import os
import pytest
class ConditionalFormatting:
def __init__(self):
self._sheet = None
@staticmethod
def run():
# The path to the documents directory
current_dir = os.path.dirname(os.path.abspath(__file__))
data_dir = os.path.join(current_dir, "data")
obj = ConditionalFormatting()
obj.do_test(data_dir)
def do_test(self, data_dir):
book = Workbook()
sheet1 = book.worksheets[0]
self._sheet = sheet1
self.add_unique()
self._sheet.auto_fit_column(12)
output_dir = os.path.join(data_dir, "output")
if not os.path.exists(output_dir):
os.makedirs(output_dir)
out_fn = os.path.join(output_dir, "add_unique.out.xlsx")
book.save(out_fn, SaveFormat.XLSX)
def add_unique(self):
conds = self.get_format_condition("E21:G22", Color.light_salmon)
idx = conds.add_condition(FormatConditionType.UNIQUE_VALUES)
cond = conds[idx]
cond.style.background_color = Color.yellow
cond.style.pattern = BackgroundType.SOLID
self._sheet.cells.get("E21").put_value("aa")
self._sheet.cells.get("G22").put_value("aa")