docx_plus.layout.columns¶
Multi-column page layout via <w:cols>. python-docx exposes
orientation, margins, page size, and headers/footers on
docx.section.Section, but does not abstract column layout. This
module's single set_columns helper fills the gap, supporting equal
columns, unequal columns via the widths argument, and the optional
vertical separator line (w:sep="1").
Architecture walkthrough: ARCHITECTURE.md §7.7.
docx_plus.layout.columns ¶
Multi-column page layout for a section (<w:cols>).
python-docx exposes orientation, margins, page size, and header/footer
settings on :class:docx.section.Section, but does not provide an
abstraction for the <w:cols> child of <w:sectPr>. This module
fills that gap with a single :func:set_columns helper.
ECMA-376 §17.6.4: cols controls the multi-column layout of a
section. w:num is the column count, w:space is the spacing
between columns (twips), w:sep enables vertical separator lines,
and child <w:col w:w=... w:space=.../> elements set unequal widths.
This module imports only from docx_plus.core (SPEC §9.1).
set_columns ¶
set_columns(
section: Section,
num: int,
*,
space: int = 720,
separator: bool = False,
widths: Sequence[int] | None = None,
) -> None
Configure multi-column layout for section.
Idempotent: a call replaces any existing <w:cols> rather than
stacking elements. Schema-strict — the element lands in its
ECMA-376 17.6.17 slot regardless of which other <w:sectPr>
children (e.g. <w:docGrid>) are already present.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
section
|
Section
|
A python-docx :class: |
required |
num
|
int
|
Number of columns. Must be |
required |
space
|
int
|
Spacing between columns in twips (1/1440 inch). Default
720 twips = 0.5 inch. Ignored when |
720
|
separator
|
bool
|
When |
False
|
widths
|
Sequence[int] | None
|
Optional unequal-width spec, in twips. Length must equal
|
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Example
from docx import Document from docx_plus.layout import set_columns doc = Document() set_columns(doc.sections[0], 2, space=720, separator=True)