Cómo agregar formato condicional de texto
Escenarios de uso posibles
Usar formato condicional basado en texto en hojas de cálculo es útil para resaltar celdas que cumplen con criterios textuales específicos. Esto puede mejorar el análisis de datos y facilitar la búsqueda de información clave en un conjunto de datos grande. Aquí algunas razones para usar formato condicional de texto:
- Resaltar Texto Específico: Puedes aplicar formato en función de palabras, frases o caracteres específicos. Por ejemplo, quizás quieras resaltar todas las celdas que contienen la palabra “Urgente” o “Completado” para diferenciar tareas en un proyecto fácilmente.
- Identificar patrones o tendencias: Si trabajas con categorías o estados (como “Alto”, “Medio”, “Bajo”), el formato condicional de texto puede distinguir visualmente entre ellos, facilitando el seguimiento del progreso o priorización de tareas.
- Alertas de errores o entrada de datos: El formato de texto puede marcar entradas inconsistentes o erróneas, como palabras mal escritas, texto incompleto o valores incorrectos. Esto es especialmente útil en conjuntos de datos con mucha entrada textual.
- Mejora de la legibilidad: Codificar por colores el texto o cambiar su estilo (negrita, cursiva, etc.) ayuda a que la información importante destaque, mejorando la legibilidad general de tu hoja.
- Retroalimentación dinámica: Puedes establecer reglas que ajusten automáticamente el formato cuando el texto coincida con ciertas condiciones. Esto significa que no tienes que actualizar manualmente el formato a medida que cambian los datos.
En esencia, el formato condicional de texto te ayuda a detectar rápidamente información relevante, errores y tendencias, convirtiéndolo en una herramienta poderosa para gestionar e interpretar datos textuales.
Cómo agregar formato condicional de texto usando Excel
Para agregar formato condicional basado en texto en Excel, sigue estos pasos:
- Selecciona el rango de celdas: Resalta las celdas donde deseas aplicar el formato condicional.
- Abre el menú de Formato condicional: Ve a la pestaña Inicio en la cinta de opciones de Excel. Haz clic en Formato condicional en el grupo “Estilos”.
- Elige “Nueva regla”: Desde el menú desplegable, selecciona Nueva regla.
- Selecciona “Formatear solo celdas que contengan”: En el cuadro de diálogo Nueva regla de formato, elige Formatear solo celdas que contengan en la sección “Seleccionar un tipo de regla”.
- Establece los criterios de la regla: En la sección “Formatear celdas con”, elige Texto específico en el menú desplegable. Selecciona si contiene, empieza con o termina con, dependiendo de la condición que deseas aplicar. Ingresa el texto que quieres formatear (por ejemplo, una palabra específica como “Urgente” o “Completado”).
- Elige el formato: Haz clic en el botón de Formato. En el cuadro de diálogo de Formato de celdas, puedes seleccionar el color de fuente, color de relleno u otras opciones de formato que prefieras.
- Aplica la regla: Una vez que hayas establecido el formato deseado, haz clic en Aceptar para aplicar la regla. Haz clic en Aceptar nuevamente en el cuadro de diálogo Nueva regla de formato para cerrarlo.
- Ver los resultados: Las celdas que contienen el texto especificado tendrán ahora el formato aplicado, facilitando la identificación de la información relevante.
Cómo agregar formato condicional de Texto usando Aspose.Cells para Python via .NET
Aspose.Cells para Python via .NET soporta completamente el formato condicional proporcionado por Microsoft Excel 2007 y versiones posteriores en formato XLSX en tiempo de ejecución. Este ejemplo demuestra un ejercicio para tipos avanzados de formato condicional incluyendo ComienzaCon, ContieneEnBlanco, ContieneTexto, entre otros.
Formate la celda cuando el valor comience con texto especificado
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
Formate la celda cuando el valor contenga en blanco
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(" ")
Formate la celda cuando el valor contenga errores
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(" ")
Formate la celda cuando el valor contenga texto especificado
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"
Formate la celda cuando el valor contenga valores duplicados
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")
Formate la celda cuando el valor termine con texto especificado
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")
Formate la celda cuando el valor no contenga en blanco
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(" ")
Formate la celda cuando el valor no contenga errores
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(" ")
Formate la celda cuando el valor no contenga texto especificado
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"
Formate la celda cuando el valor contenga valores únicos
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")