A paragraph, w:p, holds runs: stretches of text with the same formatting.
Text never sits directly in a paragraph; it is always in a w:t inside a
w:r.
An example
<w:p>
<w:pPr>
<w:pStyle w:val="Heading1"/>
<w:jc w:val="center"/>
</w:pPr>
<w:r>
<w:t xml:space="preserve">A plain and </w:t>
</w:r>
<w:r>
<w:rPr>
<w:b/>
<w:color w:val="C00000"/>
</w:rPr>
<w:t>bold red</w:t>
</w:r>
<w:r>
<w:t xml:space="preserve"> heading</w:t>
</w:r>
</w:p>
Paragraph properties: w:pPr
w:pPr is the first child of the paragraph. Its children have a fixed order
in the schema; the most used are:
| Element | Meaning |
|---|---|
w:pStyle | The paragraph’s style, by style ID. |
w:keepNext, w:keepLines | Keep with the next paragraph; keep the paragraph’s lines together. |
w:pageBreakBefore | Start a new page. |
w:framePr | Text frame properties. |
w:widowControl | Prevent widows and orphans. |
w:numPr | List membership: w:ilvl and w:numId. |
w:suppressLineNumbers | Do not number these lines. |
w:pBdr | Borders: w:top, w:left, w:bottom, w:right, w:between, w:bar. |
w:shd | Shading. |
w:tabs | Custom tab stops: w:tab with w:val and w:pos. |
w:spacing | w:before, w:after (twips), w:line, w:lineRule (auto, in 240ths of a line, exact or atLeast, in twips). |
w:ind | Indents: w:left, w:right (w:start, w:end in Strict), w:hanging, w:firstLine. |
w:contextualSpacing | Ignore spacing between paragraphs of the same style. |
w:mirrorIndents | Swap indents on facing pages. |
w:jc | Alignment: left, center, right, both and others (start, end in Strict). |
w:outlineLvl | Outline level, 0 to 8 for headings; 9 for body text. |
w:rPr | The formatting of the paragraph mark, and w:ins or w:del when the paragraph mark is tracked. |
w:sectPr | This paragraph ends a section. |
w:pPrChange | The previous paragraph properties, under tracked changes. |
The rest of the children of w:pPr (ECMA-376 Part 1, 17.3.1; all of them
appear in the schema type CT_PPr):
| Element | Section | Meaning |
|---|---|---|
w:suppressAutoHyphens | 17.3.1.34 | No automatic hyphenation in this paragraph. |
w:kinsoku, w:wordWrap, w:overflowPunct, w:topLinePunct | 17.3.1.16, 17.3.1.45, 17.3.1.21, 17.3.1.43 | East Asian line breaking: kinsoku rules for the first and last character of a line, breaking at character level, punctuation past the text extent, compressing punctuation at the start of a line. |
w:autoSpaceDE, w:autoSpaceDN | 17.3.1.2, 17.3.1.3 | Automatic spacing between Latin and East Asian text, and between East Asian text and numbers. |
w:bidi | 17.3.1.6 | Right-to-left paragraph layout. |
w:adjustRightInd, w:snapToGrid | 17.3.1.1, 17.3.1.32 | Adjust the right indent, and snap line spacing, to the document grid. |
w:suppressOverlap | 17.3.1.36 | Text frames of this paragraph must not overlap. |
w:textDirection, w:textAlignment | 17.3.1.41, 17.3.1.39 | Text flow direction; vertical alignment of characters on a line. |
w:textboxTightWrap | 17.3.1.40 | Surrounding paragraphs wrap tightly to a text box. |
w:divId | 17.3.1.10 | The ID of the HTML div the paragraph belongs to. |
w:cnfStyle | 17.3.1.8 | Which conditional table-style formatting applies to the paragraph. |
Order of the children
The children of w:pPr are a sequence, so they must come in this order:
pStyle, keepNext, keepLines, pageBreakBefore,
framePr, widowControl, numPr, suppressLineNumbers,
pBdr, shd, tabs, suppressAutoHyphens,
kinsoku, wordWrap, overflowPunct, topLinePunct,
autoSpaceDE, autoSpaceDN, bidi, adjustRightInd,
snapToGrid, spacing, ind, contextualSpacing,
mirrorIndents, suppressOverlap, jc, textDirection,
textAlignment, textboxTightWrap, outlineLvl, divId,
cnfStyle, rPr, sectPr, pPrChange. This is the order python-docx
writes (_tag_seq of CT_PPr, docx/oxml/text/parfmt.py lines 64 to 101,
read in this session) and it matches the list in 17.3.1.26.
Rules readers rely on
- Margin and spacing types. In a paragraph or table margin,
w:type="nil"is ignored (the inherited value stays) andpctorautocounts as 0 (MS-OI29500, 2.1.116, 2.1.124-125, 2.1.146-147, 2.1.152-154, 2.1.177). LibreOffice reads the number whatever the type; the viewer follows Word. See the tables page. - Word's differences (MS-OI29500). Word ignores
w:mirrorIndentsfor a paragraph inside a table (2.1.49 b) and ignoresw:widowControlinside a table (2.1.66 a). With a hanging indent, Word wrongly aligns the hanging indent to the left margin on odd-numbered pages whenmirrorIndentsis set (2.1.49 a). The page view does not swap the indents on facing pages (a paragraph is laid out before it is known on which page it lands), somirrorIndentsis read and listed but not applied. The row-split rule in tables keeps two lines on each side of a cut, whateverwidowControlsays. - Indents.
w:firstLineandw:hangingare mutually exclusive; if both are present,firstLineis ignored (17.3.1.12). python-docx reads one value, a hanging indent as the negative ofhangingand otherwisefirstLine(first_line_indent, parfmt.py 122 to 138), and writes a negative first-line indent back ashanging(140 to 151).leftandrightare signed twips,firstLineandhangingare not (parfmt.py 32 to 43). - Line spacing.
w:lineis a signed number; python-docx treats aw:linewithoutw:lineRuleas theautorule (a multiple of a line, parfmt.py 282 to 298). - Tab stops.
w:tabhas a requiredw:valand a required signedw:posin twips measured from the leading edge of the paragraph; a missingw:leadermeans no leader (17.3.1.37; python-docx maps it to the "spaces" leader, parfmt.py 362 to 364).w:val="clear"is a stop that is removed when the document is next edited (17.3.1.37).
Inside a paragraph
Besides runs, a paragraph can contain:
| Element | Meaning |
|---|---|
w:hyperlink | A hyperlink around runs. |
w:fldSimple | A simple field around its result runs. |
w:ins, w:del, w:moveFrom, w:moveTo | Runs under tracked changes. |
w:sdt | A content control around runs. |
w:smartTag | Runs recognised as a smart tag. |
w:bookmarkStart, w:bookmarkEnd | Bookmarks. |
w:commentRangeStart, w:commentRangeEnd | Comment anchors. |
m:oMath, m:oMathPara | Equations. |
w:proofErr | Spelling or grammar marks. |
Run properties: w:rPr
w:rPr is the first child of the run. Elements present set a property on the
run, and they override what the styles give.
| Element | Meaning |
|---|---|
w:rStyle | A character style, by style ID. |
w:rFonts | Fonts: w:ascii, w:hAnsi, w:eastAsia, w:cs, and theme fonts. |
w:b, w:bCs | Bold, for ordinary text and complex-script text. |
w:i, w:iCs | Italic. |
w:caps, w:smallCaps | Display capitals, or small capitals. |
w:strike, w:dstrike | Struck through, once or twice. |
w:outline, w:shadow, w:emboss, w:imprint | Text effects. |
w:vanish | Hidden text. |
w:color | Text colour: w:val (RGB hex or auto), optionally a theme colour. |
w:spacing | Character spacing in twips. |
w:position | Raised or lowered, in half-points. |
w:sz, w:szCs | Font size in half-points: 24 is 12 point. |
w:highlight | Highlight: a named colour. |
w:u | Underline: w:val single, double, dotted, none and others. |
w:shd, w:bdr | Shading and border of the run. |
w:vertAlign | superscript, subscript or baseline. |
w:lang | The language of the text, for proofing. |
w:rtl, w:cs | Right-to-left; complex script. |
w:rPrChange | The previous run properties, under tracked changes. |
On/off properties such as w:b follow one rule: the element alone means on,
and w:val can be 1, true or on for on, and
0, false or off for off. An element set to off is
different from an absent one: it turns off what a style would turn on. See
style resolution.
Run content
| Element | Meaning |
|---|---|
w:t | Text. xml:space="preserve" keeps leading and trailing spaces. |
w:delText, w:instrText, w:delInstrText | Deleted text, field instructions and deleted field instructions. |
w:tab, w:ptab | A tab; an absolute position tab. |
w:br | A break: w:type textWrapping (the default), page or column. |
w:cr | A carriage return, as a line break. |
w:noBreakHyphen, w:softHyphen | A non-breaking and an optional hyphen. |
w:sym | A symbol by font and character code: w:font, w:char. |
w:fldChar | The start, separator or end of a complex field. |
w:drawing, w:pict, w:object | Pictures and shapes, in DrawingML or VML, and OLE objects. |
w:footnoteReference, w:endnoteReference | A note reference by ID. |
w:commentReference | Where a comment is anchored. |
w:ruby | Phonetic guide text: w:rt above w:rubyBase. |
w:lastRenderedPageBreak | A note of where a page ended when the file was last laid out. It is not a break. |
A run’s elements are read in order: one run can hold text, tabs and line breaks. Word
splits text across several runs wherever formatting, spell-checking or editing history
differ, so the same word can appear in pieces, and a reader must join them. The
w:rsidR and w:rsidRPr attributes record the editing session
that last changed a run; they do not affect the text.