Définir des formats conditionnels des fichiers Excel et ODS.
Introduction
La mise en forme conditionnelle est une fonctionnalité avancée de Microsoft Excel qui vous permet d’appliquer des formats à une cellule ou à une plage de cellules et de faire en sorte que la mise en forme change en fonction de la valeur de la cellule ou de la valeur d’une formule. Par exemple, une cellule peut apparaître en gras uniquement lorsque sa valeur est supérieure à 500. Lorsque la valeur de la cellule satisfait la condition, le format spécifié est appliqué à la cellule. Si la valeur de la cellule ne satisfait pas la condition de format, la mise en forme par défaut de la cellule est utilisée. Dans Microsoft Excel, sélectionnez Format, puis Mise en forme conditionnelle pour ouvrir la boîte de dialogue Mise en forme conditionnelle.
Aspose.Cells pour Python via .NET supporte l’application de formats conditionnels aux cellules en temps réel. Cet article explique comment. Il explique également comment calculer la couleur utilisée par Excel pour le formatage conditionnel à échelle de couleurs.
Application de la mise en forme conditionnelle
Aspose.Cells pour Python via .NET supporte le formatage conditionnel de plusieurs façons :
- Utilisation de la feuille de calcul du concepteur
- Utilisation de la méthode de copie.
- Création de la mise en forme conditionnelle à l’exécution.
Utilisation de la feuille de calcul du concepteur
Les développeurs peuvent créer une feuille de calcul de conception contenant un formatage conditionnel dans Microsoft Excel, puis ouvrir cette feuille avec Aspose.Cells pour Python via .NET. Aspose.Cells pour Python via .NET charge et enregistre la feuille de concepteur, en conservant tout paramètre de formatage conditionnel.
Utilisation de la méthode de copie
Aspose.Cells pour Python via .NET permet aux développeurs de copier les paramètres de formatage conditionnel d’une cellule à une autre dans la feuille de calcul en appelant la méthode Range.copy().
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() |
Application de la mise en forme conditionnelle à l’exécution
Aspose.Cells pour Python via .NET vous permet à la fois d’ajouter et de supprimer le formatage conditionnel en temps réel. Les exemples de code ci-dessous montrent comment définir le formatage conditionnel :
- Instancier un classeur.
- Ajouter une mise en forme conditionnelle vide.
- Définir la plage à laquelle la mise en forme doit s’appliquer.
- Définir les conditions de formatage.
- Enregistrez le fichier.
Après cet exemple, voici un certain nombre d’autres exemples plus petits qui montrent comment appliquer les paramètres de la police, les paramètres des bordures et les motifs.
Microsoft Excel 2007 a ajouté un formatage conditionnel plus avancé que Aspose.Cells pour Python via .NET prend également en charge. Les exemples ici illustrent comment utiliser un formatage simple, les exemples de Microsoft Excel 2007 montrent comment appliquer un formatage conditionnel plus avancé.
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") |
Définir la police
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) |
Définir la bordure
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") |
Définir le motif
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") |
Sujets avancés
- Ajout de mises en forme conditionnelles à 2 couleurs et à 3 couleurs
- Appliquer une mise en forme conditionnelle dans les feuilles de calcul
- Appliquer un ombrage aux lignes et colonnes alternées avec une mise en forme conditionnelle
- Générer des images de barres de données pour la mise en forme conditionnelle
- Obtenir des ensembles d’icônes, des barres de données ou des objets de couleurs utilisés dans la mise en forme conditionnelle