docx_plus.layout.borders¶
Page borders via <w:pgBorders> with a per-side Border dataclass.
Each side carries style (e.g. "single", "double"), thickness in
eighths of a point, RGB hex color, and a space gap in points
(range 0-31). set_page_borders also takes offset_from ("page"
default, matching Word's UI, or "text"). All four sides default to
None; passing all-None removes the element rather than emitting an
empty container.
Architecture walkthrough: Layout.
docx_plus.layout.borders ¶
Page borders (<w:pgBorders>).
python-docx does not abstract <w:pgBorders> — the section-scoped
control for the decorative box around a page that formal documents
(certificates, awards, contract title pages) frequently want. This
module fills the gap with a single :func:set_page_borders helper over
the shared :class:~docx_plus.core.borders.Border dataclass.
:class:Border was defined here in v0.2 and moved to
:mod:docx_plus.core.borders in v0.5, when table and cell borders became
a second consumer of the identical CT_Border shape. It is re-exported
below, so from docx_plus.layout import Border is unchanged.
ECMA-376 §17.6.10: pgBorders is a container element whose four
optional children (top, left, bottom, right — in
schema order) each declare their style (w:val), thickness in
eighths of a point (w:sz), color (w:color), and the gap from
the reference edge (w:space, in points). The container also takes
an offsetFrom attribute that selects whether w:space is
measured from the page edge or the body text.
This module imports only from docx_plus.core (SPEC §9.1).
Border
dataclass
¶
One border edge.
Attributes:
| Name | Type | Description |
|---|---|---|
style |
str
|
ECMA-376 17.18.2 border style name. Common values:
|
size |
int
|
Border thickness in eighths of a point (so |
color |
str
|
|
space |
int
|
Gap between the reference edge and the border, in points. ECMA-376 caps this at 31. The default |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
__post_init__ ¶
Validate the fields against their ECMA-376 simple types.
Source code in docx_plus/core/borders.py
set_page_borders ¶
set_page_borders(
section: Section,
*,
top: Border | None = None,
bottom: Border | None = None,
left: Border | None = None,
right: Border | None = None,
offset_from: OffsetFrom = "page",
) -> None
Configure the page border for section.
Idempotent: replaces any existing <w:pgBorders>. Passing all
four sides as None removes the element instead of writing an
empty container. Child sides are written in the schema-required
order top → left → bottom → right per ECMA-376 17.6.10.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
section
|
Section
|
A python-docx :class: |
required |
top
|
Border | None
|
Border for the top edge, or |
None
|
bottom
|
Border | None
|
Border for the bottom edge. |
None
|
left
|
Border | None
|
Border for the left edge. |
None
|
right
|
Border | None
|
Border for the right edge. |
None
|
offset_from
|
OffsetFrom
|
|
'page'
|
Example
from docx import Document from docx_plus.layout import Border, set_page_borders doc = Document() rule = Border(style="single", size=8, color="2F5496") set_page_borders(doc.sections[0], top=rule, bottom=rule, ... left=rule, right=rule)