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: ARCHITECTURE.md §7.7.
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 :class:Border dataclass and a single
:func:set_page_borders helper.
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 side of a page border.
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 page edge (or text — see
:func: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
__post_init__ ¶
Validate color against ECMA-376 ST_HexColor at construction.
Source code in docx_plus/layout/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)