docx_plus.tables.shading¶
Cell, row, and table background fills. python-docx has no CT_Shd
class and does not register the w:shd tag, so the single most common
thing anyone wants from a table beyond its text is unreachable.
ECMA-376 models shading as three attributes rather than one colour: a
w:fill (the background), a w:val pattern drawn over it, and a
w:color for that pattern's foreground. A solid fill — what nearly
everyone means — is pattern="clear" with only fill set, which is
what Shading defaults to.
Rows have no shading element
CT_TrPr has no w:shd child. Word implements "shade this row" by
writing the same <w:shd> into every cell in it, and so does
set_row_shading. It iterates the row's <w:tc> elements rather
than Row.cells, so a cell spanning several grid columns is
visited once rather than once per column.
Architecture walkthrough: Table formatting.
docx_plus.tables.shading ¶
Table, row, and cell shading — <w:shd>.
python-docx has no CT_Shd class and does not register the w:shd
tag, so cell background fills — the single most common thing anyone
wants from a table beyond its text — have to be written as raw XML.
ECMA-376 17.4.32 models shading as three attributes rather than one
colour: a w:fill (the background), a w:val pattern drawn over
it, and a w:color for that pattern's foreground. Solid fills, which
is what nearly everyone means, are pattern="clear" with only
fill set — so :class:Shading defaults to exactly that and a plain
Shading(fill="D9E2F3") does the obvious thing.
.. note::
Rows have no shading element. CT_TrPr (17.4.82) has no
w:shd child — Word implements "shade this row" by writing the
same <w:shd> into every cell in it, and so does
:func:set_row_shading.
This module imports only from docx_plus.core (SPEC §9.1).
Shading
dataclass
¶
A background fill, optionally with a pattern drawn over it.
Attributes:
| Name | Type | Description |
|---|---|---|
fill |
str
|
Background colour as |
pattern |
str
|
ECMA-376 17.18.78 |
color |
str
|
Foreground colour of |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Example
from docx_plus.tables import Shading header = Shading(fill="2F5496") hatched = Shading(fill="FFFFFF", pattern="pct25", color="808080")
__post_init__ ¶
Validate the fields against their ECMA-376 simple types.
Source code in docx_plus/tables/shading.py
set_table_shading ¶
Set the table-level shading on table.
Idempotent — replaces any existing <w:shd>. Passing None
removes it.
Table-level shading sits below row-banding from a table style and below any cell's own shading, so it reads as a default rather than an override.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
Table
|
A python-docx :class: |
required |
shading
|
Shading | None
|
The shading to apply, or |
required |
Example
from docx import Document from docx_plus.tables import Shading, set_table_shading doc = Document() set_table_shading(doc.add_table(rows=1, cols=1), Shading(fill="F2F2F2"))
Source code in docx_plus/tables/shading.py
set_row_shading ¶
Shade every cell in row.
There is no row-level shading element in the format — CT_TrPr
has no w:shd child — so this writes the same <w:shd> into
each of the row's cells, which is what Word's UI does for a selected
row.
Iterates the row's <w:tc> elements directly rather than
Row.cells, so a cell spanning several grid columns is visited
once rather than once per column it covers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
row
|
_Row
|
A python-docx :class: |
required |
shading
|
Shading | None
|
The shading to apply, or |
required |
Example
from docx import Document from docx_plus.tables import Shading, set_row_shading doc = Document() table = doc.add_table(rows=2, cols=3) set_row_shading(table.rows[0], Shading(fill="2F5496"))
Source code in docx_plus/tables/shading.py
set_cell_shading ¶
Set the shading on a single cell.
Idempotent — replaces any existing <w:shd>. Passing None
removes it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cell
|
_Cell
|
A python-docx :class: |
required |
shading
|
Shading | None
|
The shading to apply, or |
required |
Example
from docx import Document from docx_plus.tables import Shading, set_cell_shading doc = Document() table = doc.add_table(rows=2, cols=2) set_cell_shading(table.cell(0, 0), Shading(fill="2F5496"))
Source code in docx_plus/tables/shading.py
shading_attrs ¶
Serialize shading to the CT_Shd attribute mapping.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shading
|
Shading
|
The shading to serialize. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
A |
dict[str, str]
|
the attribute order Word writes. |