Style — Aspose.Cells FOSS for Python API Reference

The Style class and its companion types control all visual and protective formatting applied to cells in aspose-cells-foss. A Style object is obtained from a cell via cell.get_style() and applied back with cell.apply_style(style).

Package: aspose.cells_foss

from aspose.cells_foss import Style, Font, Fill, Border, Borders, Alignment, NumberFormat, Protection

Style

Constructor

Style()

Creates a default Style with no explicit formatting applied.

Properties

PropertyTypeAccessDescription
fontFontReadFont settings (name, size, color, bold, italic, underline, strikethrough).
fillFillReadCell fill/background settings (pattern, foreground color, background color).
bordersBordersReadAll border settings for the cell (top, bottom, left, right, diagonal).
alignmentAlignmentReadText alignment settings (horizontal, vertical, wrap, indent, rotation).
number_formatstrRead/WriteThe active number format code string (e.g., "#,##0.00").
protectionProtectionReadCell protection settings (locked, formula hidden).

Methods

copy

style.copy() -> Style

Returns a deep copy of this style object.

set_fill_color

style.set_fill_color(color: str)

Sets the solid fill background color. color is an RRGGBB hex string (e.g., "FFFF00" for yellow).

set_fill_pattern

style.set_fill_pattern(pattern_type: str, fg_color: str = 'FFFFFFFF', bg_color: str = 'FFFFFFFF')

Sets a patterned fill. pattern_type values include 'solid', 'darkGray', 'mediumGray', 'lightGray', 'gray125', 'gray0625', 'darkHorizontal', 'darkVertical', 'darkDown', 'darkUp', 'darkGrid', 'darkTrellis', 'lightHorizontal', 'lightVertical', 'lightDown', 'lightUp', 'lightGrid', 'lightTrellis'.

set_no_fill

style.set_no_fill()

Clears any fill, setting the cell background to transparent (no fill).

set_border_color

style.set_border_color(side: str, color: str)

Sets the color of a single border side. side must be one of 'top', 'bottom', 'left', 'right', 'diagonal'. color is an RRGGBB hex string.

set_border_style

style.set_border_style(side: str, style: str)

Sets the line style of a border side. style values: 'none', 'thin', 'medium', 'thick', 'dashed', 'dotted', 'double'.

set_border_weight

style.set_border_weight(side: str, weight: int)

Sets the line weight (thickness) of a border side as an integer.

set_border

style.set_border(side: str, line_style='none', color='FF000000', weight=1)

Convenience method that sets line style, color, and weight for a single border side in one call.

set_diagonal_border

style.set_diagonal_border(line_style='none', color='FF000000', weight=1, up=False, down=False)

Sets the diagonal border. Pass up=True for an upward diagonal, down=True for a downward diagonal, or both to draw a cross.

set_horizontal_alignment

style.set_horizontal_alignment(alignment: str)

Sets horizontal text alignment. Values: 'general', 'left', 'center', 'right', 'fill', 'justify', 'centerContinuous', 'distributed'.

set_vertical_alignment

style.set_vertical_alignment(alignment: str)

Sets vertical text alignment. Values: 'top', 'center', 'bottom', 'justify', 'distributed'.

set_text_wrap

style.set_text_wrap(wrap=True)

Enables or disables text wrapping within the cell.

set_shrink_to_fit

style.set_shrink_to_fit(shrink=True)

Enables or disables shrink-to-fit. When enabled, text is scaled down to fit the cell width.

set_indent

style.set_indent(indent: int)

Sets the indentation level (0–15).

set_text_rotation

style.set_text_rotation(rotation: int)

Sets text rotation in degrees. Valid values are 0–180 (counter-clockwise from 0°) or 255 (vertical text stacked top-to-bottom).

set_reading_order

style.set_reading_order(order: int)

Sets the reading order. 0 = context-dependent, 1 = left-to-right, 2 = right-to-left.

set_number_format

style.set_number_format(format_code: str)

Applies a custom number format string (e.g., "DD/MM/YYYY", "#,##0.00 [$€-407]").

set_builtin_number_format

style.set_builtin_number_format(format_id: int)

Applies one of Excel’s built-in number formats by its integer ID (e.g., 9 = "0%", 14 = "m/d/yy").

set_locked

style.set_locked(locked=True)

Marks the cell as locked. Locked cells cannot be edited when worksheet protection is active.

set_formula_hidden

style.set_formula_hidden(hidden=True)

Hides formulas from the formula bar when worksheet protection is active.


Font

Constructor

Font(name='Calibri', size=11, color='FF000000', bold=False, italic=False, underline=False, strikethrough=False)

Properties

PropertyTypeDescription
namestrFont family name (e.g., "Calibri", "Arial").
sizeint | floatFont size in points.
colorstrFont color as an RRGGBB hex string (e.g., "FF0000" for red).
boldboolTrue to apply bold weight.
italicboolTrue to apply italic style.
underlineboolTrue to apply single underline.
strikethroughboolTrue to apply strikethrough.

Fill

Constructor

Fill(pattern_type='none', foreground_color='FFFFFFFF', background_color='FFFFFFFF')

Properties

PropertyTypeDescription
pattern_typestrFill pattern type (e.g., 'solid', 'none', 'darkGray').
foreground_colorstrForeground color as RRGGBB hex.
background_colorstrBackground color as RRGGBB hex.

Methods

set_solid_fill

fill.set_solid_fill(color: str)

Applies a solid fill with the given RRGGBB color.

set_gradient_fill

fill.set_gradient_fill(start_color: str, end_color: str)

Applies a two-color gradient fill.

set_pattern_fill

fill.set_pattern_fill(pattern_type: str, fg_color: str, bg_color: str)

Applies a patterned fill with explicit foreground and background colors.

set_no_fill

fill.set_no_fill()

Removes any fill from the cell.


Border

Constructor

Border(line_style='none', color='FF000000', weight=1)

Properties

PropertyTypeDescription
line_stylestrBorder line style: 'none', 'thin', 'medium', 'thick', 'dashed', 'dotted', 'double'.
colorstrBorder color as RRGGBB hex.
weightintBorder weight (thickness) as an integer.

Borders

Constructor

Borders()

A container for all four cell borders plus diagonal borders.

Properties

PropertyTypeDescription
topBorderTop border settings.
bottomBorderBottom border settings.
leftBorderLeft border settings.
rightBorderRight border settings.
diagonalBorderDiagonal border settings.
diagonal_upboolTrue to draw the upward diagonal line.
diagonal_downboolTrue to draw the downward diagonal line.

Alignment

Constructor

Alignment(horizontal='general', vertical='bottom', wrap_text=False, indent=0, text_rotation=0, shrink_to_fit=False)

Properties

PropertyTypeDescription
horizontalstrHorizontal alignment: 'general', 'left', 'center', 'right', 'fill', 'justify', 'centerContinuous', 'distributed'.
verticalstrVertical alignment: 'top', 'center', 'bottom', 'justify', 'distributed'.
wrap_textboolTrue to wrap text within the cell.
indentintIndentation level (0–15).
text_rotationintText rotation in degrees (0–180 or 255 for stacked).
shrink_to_fitboolTrue to shrink text to fit the cell.

NumberFormat

NumberFormat is a utility class with static methods only. It is not instantiated directly.

Static Methods

get_builtin_format

NumberFormat.get_builtin_format(format_id: int) -> str

Returns the format code string for the given built-in format ID (e.g., NumberFormat.get_builtin_format(9) returns "0%").

is_builtin_format

NumberFormat.is_builtin_format(format_code: str) -> bool

Returns True if the given format code matches a known built-in Excel format.

lookup_builtin_format

NumberFormat.lookup_builtin_format(format_code: str) -> int | None

Returns the built-in format ID for a format code string, or None if the code is not a built-in format.


Protection

Constructor

Protection(locked=True, hidden=False)

Properties

PropertyTypeDescription
lockedboolTrue means the cell is locked when worksheet protection is on (default).
hiddenboolTrue means any formula in the cell is hidden in the formula bar when worksheet protection is on.

Example

The following example styles a header cell with bold red text, a yellow background, and a thin black border on all sides.

from aspose.cells_foss import Workbook

wb = Workbook()
ws = wb.worksheets[0]

ws.cells["A1"].put_value("Total Revenue")

style = ws.cells["A1"].get_style()

# Bold red font
style.font.bold = True
style.font.color = "FF0000"   # red
style.font.size = 13

# Yellow solid fill
style.set_fill_color("FFFF00")

# Thin black border on all four sides
for side in ("top", "bottom", "left", "right"):
    style.set_border(side, line_style="thin", color="000000", weight=1)

# Center the text
style.set_horizontal_alignment("center")

ws.cells["A1"].apply_style(style)

wb.save("styled.xlsx")

See Also