Fill Settings

Colors and Background Patterns

Microsoft Excel can set the foreground (outline) and background (fill) colors of cells and background patterns.

Aspose.Cells for Python via .NET also supports these features in a flexible manner. In this topic, we learn to use these features using Aspose.Cells.

Setting Colors and Background Patterns

Aspose.Cells for Python via .NET provides a class, Workbook that represents a Microsoft Excel file. The Workbook class contains a worksheets collection that allows access to each worksheet in the Excel file. A worksheet is represented by the Worksheet class. The Worksheet class provides a Cells collection. Each item in the Cells collection represents an object of the Cell class.

The Cell has the get_style and set_style methods that are used to get and set a cell’s formatting. The Style class provides properties for setting the foreground and background colors of the cells. Aspose.Cells for Python via .NET provides a BackgroundType enumeration that contains a set of pre-defined types of background patterns which are given below.

Background Patterns Description
DIAGONAL_CROSSHATCH Represents diagonal crosshatch pattern
DIAGONAL_STRIPE Represents diagonal stripe pattern
GRAY6 Represents 6.25% gray pattern
GRAY12 Represents 12.5% gray pattern
GRAY25 Represents 25% gray pattern
GRAY50 Represents 50% gray pattern
GRAY75 Represents 75% gray pattern
HORIZONTAL_STRIPE Represents horizontal stripe pattern
NONE Represents no background
REVERSE_DIAGONAL_STRIPE Represents reverse diagonal stripe pattern
SOLID Represents solid pattern
THICK_DIAGONAL_CROSSHATCH Represents thick diagonal crosshatch pattern
THIN_DIAGONAL_CROSSHATCH Represents thin diagonal crosshatch pattern
THIN_DIAGONAL_STRIPE Represents thin diagonal stripe pattern
THIN_HORIZONTAL_CROSSHATCH Represents thin horizontal crosshatch pattern
THIN_HORIZONTAL_STRIPE Represents thin horizontal stripe pattern
THIN_REVERSE_DIAGONAL_STRIPE Represents thin reverse diagonal stripe pattern
THIN_VERTICAL_STRIPE Represents thin vertical stripe pattern
VERTICAL_STRIPE Represents vertical stripe pattern

In the example below, the foreground color of the A1 cell is set but A2 is configured to have both foreground and background colors with a vertical stripe background pattern.

from aspose.cells import BackgroundType, SaveFormat, Workbook
from aspose.pydrawing import Color
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 Workbook object
i = workbook.worksheets.add()
# Obtaining the reference of the newly added worksheet by passing its sheet index
worksheet = workbook.worksheets[i]
# Define a Style and get the A1 cell style
style = worksheet.cells.get("A1").get_style()
# Setting the foreground color to yellow
style.foreground_color = Color.yellow
# Setting the background pattern to vertical stripe
style.pattern = BackgroundType.VERTICAL_STRIPE
# Apply the style to A1 cell
worksheet.cells.get("A1").set_style(style)
# Get the A2 cell style
style = worksheet.cells.get("A2").get_style()
# Setting the foreground color to blue
style.foreground_color = Color.blue
# Setting the background color to yellow
style.background_color = Color.yellow
# Setting the background pattern to vertical stripe
style.pattern = BackgroundType.VERTICAL_STRIPE
# Apply the style to A2 cell
worksheet.cells.get("A2").set_style(style)
# Saving the Excel file
workbook.save(dataDir + "book1.out.xls", SaveFormat.EXCEL_97_TO_2003)

Important to Know

Applying Gradient Fill Effects

To apply your desired Gradient Fill Effects to the cell, use the Style object’s set_two_color_gradient method accordingly.

from aspose.cells import TextAlignmentType, Workbook
from aspose.cells.drawing import GradientStyleType
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(".")
# Instantiate a new Workbook
workbook = Workbook()
# Get the first worksheet (default) in the workbook
worksheet = workbook.worksheets[0]
# Input a value into B3 cell
worksheet.cells.get(2, 1).put_value("test")
# Get the Style of the cell
style = worksheet.cells.get("B3").get_style()
# Set Gradient pattern on
style.is_gradient = True
# Specify two color gradient fill effects
style.set_two_color_gradient(Color.from_argb(255, 255, 255), Color.from_argb(79, 129, 189), GradientStyleType.HORIZONTAL, 1)
# Set the color of the text in the cell
style.font.color = Color.red
# Specify horizontal and vertical alignment settings
style.horizontal_alignment = TextAlignmentType.CENTER
style.vertical_alignment = TextAlignmentType.CENTER
# Apply the style to the cell
worksheet.cells.get("B3").set_style(style)
# Set the third row height in pixels
worksheet.cells.set_row_height_pixel(2, 53)
# Merge the range of cells (B3:C3)
worksheet.cells.merge(2, 1, 1, 2)
# Save the Excel file
workbook.save(dataDir + "output.xlsx")

Colors and Palette

A palette is the number of colors available for use in creating an image. The use of a standardized palette in a presentation allows the user to create a consistent look. Each Microsoft Excel (97-2003) file has a palette of 56 colors that can be applied to cells, fonts, gridlines, graphic objects, fills and lines in a chart.

With Aspose.Cells for Python via .NET it is possible not only to use the palette’s existing colors but also custom colors. Before using a custom color, add it to the palette first.

This topic discusses how to add custom colors to the palette.

Adding Custom Colors to Palette

Aspose.Cells for Python via .NET supports Microsoft Excel’s 56 color palette. To use a custom color that is not defined in the palette, add the color to the palette.

Aspose.Cells for Python via .NET provides a class, Workbook, that represents a Microsoft Excel file. The Workbook class provides a change_palette method that takes the following parameters to add a custom color to modify the palette:

  • Custom Color, the custom color to be added.
  • Index, the index of the color in the palette that the custom color will replace. Should be between 0-55.

The example below adds a custom color (Orchid) to the palette before applying it on a font.

from aspose.cells import SaveFormat, Workbook
from aspose.pydrawing import Color
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 an Workbook object
workbook = Workbook()
# Adding Orchid color to the palette at 55th index
workbook.change_palette(Color.orchid, 55)
# 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!")
# Defining new Style object
styleObject = workbook.create_style()
# Setting the Orchid (custom) color to the font
styleObject.font.color = Color.orchid
# Applying the style to the cell
cell.set_style(styleObject)
# Saving the Excel file
workbook.save(dataDir + "book1.out.xls", SaveFormat.AUTO)