ExcelStyle
ExcelStyle is the high-level styling façade of
ExcelTableKit. It wraps openpyxl style objects (PatternFill, Border,
Side, Font, Alignment) so you never have to construct them
yourself.
Every method in ExcelStyle accepts plain Python values
(hex strings, booleans, floats) and internally converts them to the correct
openpyxl objects before delegating to
ExcelManager.
Creating an ExcelStyle instance
ExcelStyle wraps an existing
ExcelManager:
from exceltablekit import ExcelManager, ExcelStyle
excel = ExcelManager("output.xlsx")
excel.set_table("A1", "D10")
excel.define_header(rows=1)
style = ExcelStyle(excel)
Important
Call set_table (and optionally define_header) on the manager
before creating the style instance, or before calling any style method.
The style object does not keep its own copy of the table — it reads through
the manager on every call.
Body styling
The following methods apply styles to the body rows of the table (all rows that are not headers).
set_background
Applies a solid background fill colour to all body cells.
style.set_background("E8F4FD") # light blue
style.set_background("FFFACD") # lemon chiffon
style.set_background("FFFFFF") # white
Parameter |
Type |
Description |
|---|---|---|
|
|
6-character RGB hex string without the leading |
set_font
Applies a font style to all body cells.
style.set_font(
type="Calibri",
size=11.0,
bold=False,
italic=False,
hex_color="000000",
)
Parameter |
Type |
Default |
Description |
|---|---|---|---|
|
|
|
Font family name. |
|
|
|
Font size in points. |
|
|
|
Bold text. |
|
|
|
Italic text. |
|
|
|
6- or 8-character ARGB hex string for the font colour. The leading
|
Note
openpyxl uses ARGB colour values for fonts (8 hex digits). If you supply
a 6-digit value, omit the alpha prefix — openpyxl will apply it as-is.
Use "FF000000" for opaque black or just "000000" depending on
the openpyxl version in use.
set_alignment
Applies cell text alignment to all body cells.
style.set_alignment(
horizontal="center",
vertical="center",
wrap_text=True,
)
Parameter |
Type |
Default |
Description |
|---|---|---|---|
|
|
|
Horizontal alignment. One of: |
|
|
|
Vertical alignment. One of: |
|
|
|
Wrap long text within the cell. |
set_border
Applies a border to all body cells. Each of the four edges (left, right, top, bottom) can be enabled or disabled independently and given its own border style.
# All four edges, thin black border (defaults)
style.set_border()
# Custom: only left and right edges, medium style, grey colour
style.set_border(
left=True, left_style="medium",
right=True, right_style="medium",
top=False,
bottom=False,
hex_color="888888",
)
Parameter |
Type |
Default |
Description |
|---|---|---|---|
|
|
|
Enable left edge border. |
|
|
|
Style for the left edge. |
|
|
|
Enable right edge border. |
|
|
|
Style for the right edge. |
|
|
|
Enable top edge border. |
|
|
|
Style for the top edge. |
|
|
|
Enable bottom edge border. |
|
|
|
Style for the bottom edge. |
|
|
|
Border colour as a 6-character hex string. |
See Available border styles for the full list of accepted BorderStyle values.
Header styling
Every body method has a set_header_* counterpart that targets header
rows only. Their signatures are identical to their body equivalents.
define_header must have been called on the manager before these methods
are used, otherwise HeaderTableNotDefinedError
is raised.
set_header_background
style.set_header_background("1F4E79") # dark blue
style.set_header_background("2E75B6") # medium blue
set_header_font
style.set_header_font(
type="Calibri",
size=12.0,
bold=True,
hex_color="FFFFFF", # white text
)
set_header_alignment
style.set_header_alignment(
horizontal="center",
vertical="center",
)
set_header_border
style.set_header_border(bottom_style="medium") # emphasise the header bottom
freeze_header
Freezes the worksheet pane immediately below the last header row.
excel.set_table("A1", "D20")
excel.define_header(rows=2)
style = ExcelStyle(excel)
style.freeze_header()
# Freeze pane is set at A3
The freeze cell is {first_column}{first_row + total_header_rows}. For a
table starting at B3 with 1 header row the freeze pane is B4.
Column auto-fit
auto_fit_columns
Adjusts each column’s width to fit the longest cell value in that column (plus 2 characters of padding).
style.auto_fit_columns()
This iterates over all cells (header + body) so even header text influences the final width.
Available border styles
The BorderStyle type is a Literal union of the following 13 values:
Value |
Visual result |
|---|---|
|
Thin solid line (default) |
|
Medium-weight solid line |
|
Thick solid line |
|
Double solid line |
|
Hairline (thinnest possible) |
|
Short dashes |
|
Dots |
|
Alternating dash–dot |
|
Alternating dash–dot–dot |
|
Medium-weight dashes |
|
Medium-weight dash–dot |
|
Medium-weight dash–dot–dot |
|
Slanted dash–dot |
Error handling
Error |
When it is raised |
|---|---|
|
|
|
|
Background fill failed on the body |
|
Font style failed on the body |
|
Alignment failed on the body |
|
Border failed on the body |
|
Background fill failed on the header |
|
Font style failed on the header |
|
Alignment failed on the header |
|
Border failed on the header |
|
Freeze panes failed |
|
Auto-fit column widths failed |