docx_plus.layout.line_numbering¶
Section line numbering via <w:lnNumType>. python-docx does not
abstract the marginal-line-number element that legal and contract
documents commonly want. set_line_numbering emits the element with
schema-strict child ordering in <w:sectPr> and supports the four
ECMA-376 17.6.8 attributes: countBy, restart, start, and
distance.
Architecture walkthrough: ARCHITECTURE.md §7.7.
docx_plus.layout.line_numbering ¶
Section line numbering (<w:lnNumType>).
python-docx does not abstract <w:lnNumType>, the section-scoped
control for printing line numbers in the margin (the legal / contract
use case). This module fills the gap with a single
:func:set_line_numbering helper.
ECMA-376 §17.6.8: lnNumType is an attributes-only element with
countBy (interval between printed numbers), start (the first
line number for the section), distance (twips between text and
number), and restart controlling whether numbering restarts on
each page / each new section / never (continuous).
This module imports only from docx_plus.core (SPEC §9.1).
LineNumberRestart
module-attribute
¶
set_line_numbering ¶
set_line_numbering(
section: Section,
*,
count_by: int = 1,
restart: LineNumberRestart = "newPage",
start: int = 1,
distance: int | None = None,
) -> None
Enable line numbering for section.
Idempotent: replaces any existing <w:lnNumType> rather than
stacking elements. Schema-strict — the element lands in its
ECMA-376 17.6.17 slot regardless of which other <w:sectPr>
children are present.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
section
|
Section
|
A python-docx :class: |
required |
count_by
|
int
|
Interval at which line numbers are printed. |
1
|
restart
|
LineNumberRestart
|
When numbering restarts.
|
'newPage'
|
start
|
int
|
First line number for this section. Must be |
1
|
distance
|
int | None
|
Twips between the text and the printed number. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Example
from docx import Document from docx_plus.layout import set_line_numbering doc = Document() set_line_numbering(doc.sections[0], count_by=5, restart="continuous")