How to Style Cells with Aspose.Cells FOSS in Python

How to Style Cells with Aspose.Cells FOSS in Python

Aspose.Cells FOSS for Python lets you apply font styles and background fills to individual cells using the cell.style.font and cell.style.fill APIs. Colors are expressed as 8-digit AARRGGBB hex strings; for example "FFFF0000" for opaque red, with no # prefix. All styling is done in pure Python with no dependency on Microsoft Excel or any external rendering library.

Why Style Cells with Aspose.Cells FOSS?

  1. No Excel required: Formatting runs entirely in Python on any OS.
  2. Consistent color model: A single 8-digit AARRGGBB string covers font color and fill color with the same format.
  3. Readable property names: bold, italic, underline, strikethrough: no is_ prefix to remember.
  4. Reusable Font objects: Create a Font instance once and apply it to many cells for consistent branding.

Step-by-Step Guide

Step 1: Install Aspose.Cells FOSS for Python

pip install aspose-cells-foss

No additional system packages are required. Import the classes you need from aspose.cells_foss:

from aspose.cells_foss import Workbook, Cell, Font, SaveFormat

Step 2: Set Font Name and Size

Access a cell through ws.cells["address"] and write to cell.style.font.name and cell.style.font.size.

from aspose.cells_foss import Workbook

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

ws.cells["A1"].value = "Styled Header"

cell = ws.cells["A1"]
cell.style.font.name = "Arial"
cell.style.font.size = 14

workbook.save("styled.xlsx")

The default font is Calibri at 11pt. Any font name available on the system where the file will be opened can be used; specifying a font that is not installed does not cause an error but may render with a fallback font when the file is opened.


Step 3: Set Font Color Using an AARRGGBB Hex String

Font colors are set with an 8-digit hexadecimal string in AARRGGBB order (Alpha, Red, Green, Blue). Do not include a # prefix.

from aspose.cells_foss import Workbook

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

ws.cells["A1"].value = "Red text"
ws.cells["A1"].style.font.color = "FFFF0000"   # opaque red

ws.cells["A2"].value = "Blue text"
ws.cells["A2"].style.font.color = "FF0000FF"   # opaque blue

ws.cells["A3"].value = "Green text"
ws.cells["A3"].style.font.color = "FF00FF00"   # opaque green

workbook.save("colored_text.xlsx")

Common AARRGGBB color reference:

ColorAARRGGBB stringNotes
BlackFF000000Default font color
WhiteFFFFFFFFUse on dark backgrounds
RedFFFF0000Alert or highlight text
BlueFF0000FFLinks or emphasis
GreenFF00FF00Positive values or success
OrangeFFFF8000Warning text
GrayFF808080Subdued or disabled text
NavyFF1E64C8Corporate / brand blue

The first two hex digits are the alpha channel. Use FF for fully opaque. Values below FF produce semi-transparent results in renderers that support alpha blending.


Step 4: Apply Bold, Italic, Underline, and Strikethrough

Set each attribute directly as a boolean. The property names are bold, italic, underline, and strikethrough; do not use is_bold or is_italic (those names do not exist on the FOSS API and will raise AttributeError).

from aspose.cells_foss import Workbook

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

ws.cells["A1"].value = "Bold"
ws.cells["A1"].style.font.bold = True

ws.cells["A2"].value = "Italic"
ws.cells["A2"].style.font.italic = True

ws.cells["A3"].value = "Underline"
ws.cells["A3"].style.font.underline = True

ws.cells["A4"].value = "Strikethrough"
ws.cells["A4"].style.font.strikethrough = True

ws.cells["A5"].value = "Bold + Italic"
ws.cells["A5"].style.font.bold = True
ws.cells["A5"].style.font.italic = True

workbook.save("font_styles.xlsx")

All four flags default to False. They can be combined freely on the same cell.


Step 5: Set a Solid Background Fill Color

Use cell.style.fill.set_solid_fill("AARRGGBB") to apply a background fill. The color format is the same 8-digit AARRGGBB hex string as font color.

To remove a fill entirely, call cell.style.fill.set_no_fill().

from aspose.cells_foss import Workbook

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

ws.cells["A1"].value = "Navy background"
ws.cells["A1"].style.fill.set_solid_fill("FF1E64C8")   # opaque navy blue

ws.cells["A2"].value = "Light yellow background"
ws.cells["A2"].style.fill.set_solid_fill("FFFFFFCC")   # pale yellow

ws.cells["A3"].value = "No fill"
ws.cells["A3"].style.fill.set_no_fill()                 # remove any fill

workbook.save("cell_fills.xlsx")

set_solid_fill() and set_no_fill() are mutually exclusive; calling one overrides the other on the same cell.


Step 6: Combine Multiple Styles on One Cell

You can chain any number of style properties on the same cell reference. There is no limit on how many style attributes are set before saving.

from aspose.cells_foss import Workbook

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

ws.cells["B2"].value = "Branded Header"

cell = ws.cells["B2"]

##Font
cell.style.font.name  = "Arial"
cell.style.font.size  = 16
cell.style.font.bold  = True
cell.style.font.color = "FFFFFFFF"      # white text

##Background
cell.style.fill.set_solid_fill("FF1E64C8")   # navy fill

workbook.save("branded_header.xlsx")
print("Branded header cell saved.")

This produces a white, bold, 16pt Arial label on a navy background, a common pattern for column headers.


Step 7: Use the Font Constructor for Reusable Styles

The Font class can be instantiated with all its properties in one call, then assigned to multiple cells. This is useful when you want a consistent house style applied across many cells without repeating the same property assignments.

from aspose.cells_foss import Workbook, Font

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

##Define a reusable heading font
heading_font = Font(
    name="Arial",
    size=14,
    color="FF1E64C8",   # navy
    bold=True,
    italic=False,
    underline=False,
    strikethrough=False
)

##Define a reusable body font
body_font = Font(
    name="Calibri",
    size=11,
    color="FF000000",   # black (default)
    bold=False,
    italic=False,
    underline=False,
    strikethrough=False
)

##Apply heading font to header row
headers = ["Product", "SKU", "Price", "Stock"]
for col, header in enumerate(headers):
    addr = f"{chr(65 + col)}1"
    ws.cells[addr].value = header
    ws.cells[addr].style.font = heading_font
    ws.cells[addr].style.fill.set_solid_fill("FFE8EFF9")  # light blue tint

##Apply body font to data rows
data = [
    ("Widget A", "WGT-001", 9.99,  150),
    ("Widget B", "WGT-002", 14.99, 87),
    ("Widget C", "WGT-003", 4.49,  320),
]

for row_idx, (name, sku, price, stock) in enumerate(data, start=2):
    ws.cells[f"A{row_idx}"].value = name
    ws.cells[f"B{row_idx}"].value = sku
    ws.cells[f"C{row_idx}"].value = price
    ws.cells[f"D{row_idx}"].value = stock
    for col in "ABCD":
        ws.cells[f"{col}{row_idx}"].style.font = body_font

workbook.save("product_table.xlsx")
print("Product table with reusable fonts saved.")

Font constructor default values (all parameters are optional):

ParameterDefault
name"Calibri"
size11
color"FF000000"
boldFalse
italicFalse
underlineFalse
strikethroughFalse

Complete Working Example

The following self-contained script creates a workbook with a styled header row, colored data rows, and a summary cell demonstrating every styling API covered above:

from aspose.cells_foss import Workbook, Cell, Font, SaveFormat

workbook = Workbook()
ws = workbook.worksheets[0]
ws.name = "Sales Report"

##--- Header row (bold, white text on navy background) ---
headers = ["Region", "Q1", "Q2", "Q3", "Q4", "Total"]
for col, text in enumerate(headers):
    addr = f"{chr(65 + col)}1"
    ws.cells[addr].value = text
    ws.cells[addr].style.font.name  = "Arial"
    ws.cells[addr].style.font.size  = 12
    ws.cells[addr].style.font.bold  = True
    ws.cells[addr].style.font.color = "FFFFFFFF"            # white
    ws.cells[addr].style.fill.set_solid_fill("FF1E64C8")    # navy

##--- Data rows ---
data = [
    ("North", 42000, 47500, 53000, 61000),
    ("South", 31000, 28500, 35000, 39000),
    ("East",  55000, 62000, 58000, 71000),
    ("West",  27000, 30000, 33000, 41000),
]

for row_idx, (region, q1, q2, q3, q4) in enumerate(data, start=2):
    ws.cells[f"A{row_idx}"].value = region
    ws.cells[f"B{row_idx}"].value = q1
    ws.cells[f"C{row_idx}"].value = q2
    ws.cells[f"D{row_idx}"].value = q3
    ws.cells[f"E{row_idx}"].value = q4
    # Total formula
    ws.cells[f"F{row_idx}"] = Cell(None, f"=SUM(B{row_idx}:E{row_idx})")
    # Alternate row shading
    if row_idx % 2 == 0:
        for col in "ABCDEF":
            ws.cells[f"{col}{row_idx}"].style.fill.set_solid_fill("FFE8EFF9")

##--- Italic note in a footer cell ---
ws.cells["A7"].value = "All values in USD"
ws.cells["A7"].style.font.italic = True
ws.cells["A7"].style.font.color  = "FF808080"   # gray
ws.cells["A7"].style.font.size   = 9

##--- Strikethrough on a deprecated label ---
ws.cells["A8"].value = "Old metric (deprecated)"
ws.cells["A8"].style.font.strikethrough = True
ws.cells["A8"].style.font.color         = "FF808080"

workbook.save("sales_report_styled.xlsx", SaveFormat.XLSX)
print("Styled sales report saved.")

Common Issues

Wrong color format: using #RRGGBB instead of AARRGGBB

A leading # (e.g. "#FF0000") or a 6-digit RGB string will not produce the expected color. The property expects exactly 8 hex digits with no prefix: "FFFF0000". The first two digits are the alpha channel; use FF for fully opaque.

AttributeError: 'Font' object has no attribute 'is_bold'

The FOSS API uses bold, italic, underline, and strikethrough as the property names. The is_bold / is_italic naming convention belongs to a different library and does not exist here. Replace any is_bold reference with bold.

Style changes not visible after saving

Ensure you set style properties on the cell object before calling workbook.save(). Setting a property on a cell after the save call has no effect on the file already written. If you reuse a cell variable, confirm it still refers to the correct cell address.

Fill and font on the same cell conflict visually

There is no API conflict; you can always set both cell.style.font.color and cell.style.fill.set_solid_fill() independently. Make sure the text color has sufficient contrast against the background color. White text (FFFFFFFF) on a dark fill such as navy (FF1E64C8) is a reliable combination.


Frequently Asked Questions

How do I remove a background fill and return a cell to no fill?

Call cell.style.fill.set_no_fill(). This removes any previously set solid fill from the cell.

How do I reset a cell to the default font (Calibri 11pt black)?

Re-assign the default values explicitly:

cell.style.font.name          = "Calibri"
cell.style.font.size          = 11
cell.style.font.color         = "FF000000"
cell.style.font.bold          = False
cell.style.font.italic        = False
cell.style.font.underline     = False
cell.style.font.strikethrough = False

Alternatively, create a Font() instance with no arguments (all defaults) and assign it: cell.style.font = Font().

Can I apply the same font style to a range of cells in a loop?

Yes. Iterate over the cell addresses and set the same properties on each. If you use the Font constructor to create a shared Font object, assign it to each cell with ws.cells[addr].style.font = my_font.

Does styling affect cell values or formulas?

No. Styling is purely visual metadata. cell.style.font and cell.style.fill do not touch .value or .formula.

Which save formats preserve styles?

Styles are fully preserved when saving to SaveFormat.XLSX. The SaveFormat.CSV, SaveFormat.TSV, SaveFormat.JSON, and SaveFormat.MARKDOWN formats are plain-text or structured-text formats and do not carry styling information.


Related Resources: