numbering
numbering
¶
Custom element classes related to the numbering part.
The numbering model has two levels of indirection, and getting them the wrong way round is what most reimplementations of it do:
- A paragraph's
w:numPr/w:numIdnames aw:num, a concrete list instance. - The
w:numnames aw:abstractNumthroughw:abstractNumId. The abstract definition holds the formatting of each of the nine levels: the start value, the number format and the level text. - The
w:nummay carryw:lvlOverridechildren that override parts of the abstract definition for this instance,w:startOverridein particular.
Two w:num elements pointing at the same w:abstractNum are two independent sequences
that happen to look alike. That is precisely how Word restarts a list: it does not reset
a counter, it creates a second w:num with a w:startOverride.
Leaf values here are read from their w:val attribute rather than through registered
element classes, because the tag names are reused elsewhere in the schema with other
types — w:start is a table-cell border, and lxml resolves an element class by tag name
alone, so registering it would silently change the type of every table border in the
document.
CT_Lvl
¶
Bases: BaseOxmlElement
w:lvl element, the definition of one of the nine levels of a list.
start
property
writable
¶
The number this level counts from, or None when it does not say.
Word treats an unspecified start as 1.
num_fmt
property
writable
¶
num_fmt: WD_NUMBER_FORMAT | None
Member of WdNumberFormat this level renders its counter as.
None when the level does not say, or when it names a format outside the
enumeration — Word accepts vendor extensions here and a document using one
should still be readable.
lvl_restart
property
writable
¶
The one-based level whose increment restarts this one, or None.
None means the default: this level restarts whenever any higher level
increments. A value of 0 means it never restarts.
lvl_text
property
writable
¶
The pattern this level displays, e.g. "%1." or "%1.%2", or None.
A %n placeholder is replaced by the counter of the one-based level n. For a
bullet level the text is the bullet character itself and holds no placeholder.
is_lgl
property
writable
¶
True when this level renders all its placeholders as decimal.
The "legal numbering" option, which turns "1.a.i" into "1.1.1" without changing the underlying formats.
p_style
property
writable
¶
The style id this level is linked to, or None.
A paragraph with this style takes this numbering level even without its own
w:numPr, which is how the built-in "List Number" styles work.
suffix
property
writable
¶
What separates the number from the text: "tab", "space" or "nothing".
None when the level does not say, which Word treats as "tab". This is the
gap between the bullet or number and the paragraph text, and setting it to
"space" is the usual way to tighten up a compact list.
jc
property
writable
¶
Alignment of the number within its indent: "left", "center", "right".
None when the level does not say, which Word treats as left.
pPr
property
¶
pPr: BaseOxmlElement | None
The w:pPr of this level, or None when it has none.
This is where a level's indent lives, as an ordinary w:ind.
get_or_add_pPr
¶
get_or_add_pPr() -> BaseOxmlElement
The w:pPr of this level, added in schema order if not already there.
Source code in src/docx/oxml/numbering.py
CT_AbstractNum
¶
Bases: BaseOxmlElement
w:abstractNum element, the shared definition behind one or more w:num.
multi_level_type
property
writable
¶
"singleLevel", "multilevel" or "hybridMultilevel", or None.
num_style_link
property
¶
The style id this definition defers to, or None.
An abstract definition carrying this holds no levels of its own; the numbering actually comes from the definition the named style points at. Word writes this for a list style shared between several lists.
style_link
property
¶
The style id this definition is the numbering for, or None.
new
classmethod
¶
new(abstract_num_id: int) -> CT_AbstractNum
A new empty w:abstractNum with abstract_num_id.
w:nsid and w:tmpl are deliberately not written. They are what Word uses to
recognise a definition as one of its own list-gallery entries; inventing values
for them would claim a provenance this definition does not have, and Word opens
a document without them perfectly well.
Source code in src/docx/oxml/numbering.py
add_level
¶
add_level(ilvl: int) -> CT_Lvl
A w:lvl for level ilvl, newly added in ascending w:ilvl order.
Word rejects an abstract definition whose levels are out of order.
Source code in src/docx/oxml/numbering.py
CT_Num
¶
Bases: BaseOxmlElement
<w:num> element, which represents a concrete list definition instance, having
a required child
add_lvlOverride
¶
Return a newly added CT_NumLvl (ilvl
attribute set to ilvl.
lvlOverride_having_ilvl
¶
lvlOverride_having_ilvl(ilvl: int) -> CT_NumLvl | None
The w:lvlOverride child for level ilvl, or None when there is none.
new
classmethod
¶
Return a new <w:num> element having numId of num_id and having a
<w:abstractNumId> child with val attribute set to abstractNum_id.
Source code in src/docx/oxml/numbering.py
CT_NumLvl
¶
Bases: BaseOxmlElement
<w:lvlOverride> element, which identifies a level in a list definition to
override with settings it contains.
lvl
property
¶
lvl: CT_Lvl | None
The w:lvl override of this level, or None when it overrides only start.
start_override
property
¶
The number this level counts from in this instance, or None.
add_startOverride
¶
Return a newly added CT_DecimalNumber element having tagname
w:startOverride and val attribute set to val.
CT_NumPr
¶
Bases: BaseOxmlElement
A <w:numPr> element, a container for numbering properties applied to a
paragraph.
CT_Numbering
¶
Bases: BaseOxmlElement
<w:numbering> element, the root element of a numbering part, i.e.
numbering.xml.
add_num
¶
Return a newly added CT_Num (abstractNum_id.
Source code in src/docx/oxml/numbering.py
abstractNum_having_abstractNumId
¶
abstractNum_having_abstractNumId(
abstractNumId: int,
) -> CT_AbstractNum | None
The w:abstractNum child with abstractNumId, or None if not found.
Source code in src/docx/oxml/numbering.py
num_having_numId
¶
Return the <w:num> child element having numId attribute matching
numId.