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, ProtectionStyle
Constructor
Style()Creates a default Style with no explicit formatting applied.
Properties
| Property | Type | Access | Description |
|---|---|---|---|
font | Font | Read | Font settings (name, size, color, bold, italic, underline, strikethrough). |
fill | Fill | Read | Cell fill/background settings (pattern, foreground color, background color). |
borders | Borders | Read | All border settings for the cell (top, bottom, left, right, diagonal). |
alignment | Alignment | Read | Text alignment settings (horizontal, vertical, wrap, indent, rotation). |
number_format | str | Read/Write | The active number format code string (e.g., "#,##0.00"). |
protection | Protection | Read | Cell protection settings (locked, formula hidden). |
Methods
copy
style.copy() -> StyleReturns 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
| Property | Type | Description |
|---|---|---|
name | str | Font family name (e.g., "Calibri", "Arial"). |
size | int | float | Font size in points. |
color | str | Font color as an RRGGBB hex string (e.g., "FF0000" for red). |
bold | bool | True to apply bold weight. |
italic | bool | True to apply italic style. |
underline | bool | True to apply single underline. |
strikethrough | bool | True to apply strikethrough. |
Fill
Constructor
Fill(pattern_type='none', foreground_color='FFFFFFFF', background_color='FFFFFFFF')Properties
| Property | Type | Description |
|---|---|---|
pattern_type | str | Fill pattern type (e.g., 'solid', 'none', 'darkGray'). |
foreground_color | str | Foreground color as RRGGBB hex. |
background_color | str | Background 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
| Property | Type | Description |
|---|---|---|
line_style | str | Border line style: 'none', 'thin', 'medium', 'thick', 'dashed', 'dotted', 'double'. |
color | str | Border color as RRGGBB hex. |
weight | int | Border weight (thickness) as an integer. |
Borders
Constructor
Borders()A container for all four cell borders plus diagonal borders.
Properties
| Property | Type | Description |
|---|---|---|
top | Border | Top border settings. |
bottom | Border | Bottom border settings. |
left | Border | Left border settings. |
right | Border | Right border settings. |
diagonal | Border | Diagonal border settings. |
diagonal_up | bool | True to draw the upward diagonal line. |
diagonal_down | bool | True 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
| Property | Type | Description |
|---|---|---|
horizontal | str | Horizontal alignment: 'general', 'left', 'center', 'right', 'fill', 'justify', 'centerContinuous', 'distributed'. |
vertical | str | Vertical alignment: 'top', 'center', 'bottom', 'justify', 'distributed'. |
wrap_text | bool | True to wrap text within the cell. |
indent | int | Indentation level (0–15). |
text_rotation | int | Text rotation in degrees (0–180 or 255 for stacked). |
shrink_to_fit | bool | True 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) -> strReturns 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) -> boolReturns True if the given format code matches a known built-in Excel format.
lookup_builtin_format
NumberFormat.lookup_builtin_format(format_code: str) -> int | NoneReturns 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
| Property | Type | Description |
|---|---|---|
locked | bool | True means the cell is locked when worksheet protection is on (default). |
hidden | bool | True 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")