MCPcopy Index your code
hub / github.com/python-openxml/python-docx / CT_Body

Class CT_Body

src/docx/oxml/document.py:35–88  ·  view source on GitHub ↗

`w:body`, the container element for the main document story in `document.xml`.

Source from the content-addressed store, hash-verified

33
34
35class CT_Body(BaseOxmlElement):
36 """`w:body`, the container element for the main document story in `document.xml`."""
37
38 add_p: Callable[[], CT_P]
39 get_or_add_sectPr: Callable[[], CT_SectPr]
40 p_lst: List[CT_P]
41 tbl_lst: List[CT_Tbl]
42
43 _insert_tbl: Callable[[CT_Tbl], CT_Tbl]
44
45 p = ZeroOrMore("w:p", successors=("w:sectPr",))
46 tbl = ZeroOrMore("w:tbl", successors=("w:sectPr",))
47 sectPr: CT_SectPr | None = ZeroOrOne( # pyright: ignore[reportAssignmentType]
48 "w:sectPr", successors=()
49 )
50
51 def add_section_break(self) -> CT_SectPr:
52 """Return `w:sectPr` element for new section added at end of document.
53
54 The last `w:sectPr` becomes the second-to-last, with the new `w:sectPr` being an
55 exact clone of the previous one, except that all header and footer references
56 are removed (and are therefore now "inherited" from the prior section).
57
58 A copy of the previously-last `w:sectPr` will now appear in a new `w:p` at the
59 end of the document. The returned `w:sectPr` is the sentinel `w:sectPr` for the
60 document (and as implemented, `is` the prior sentinel `w:sectPr` with headers
61 and footers removed).
62 """
63 # ---get the sectPr at file-end, which controls last section (sections[-1])---
64 sentinel_sectPr = self.get_or_add_sectPr()
65 # ---add exact copy to new `w:p` element; that is now second-to last section---
66 self.add_p().set_sectPr(sentinel_sectPr.clone())
67 # ---remove any header or footer references from "new" last section---
68 for hdrftr_ref in sentinel_sectPr.xpath("w:headerReference|w:footerReference"):
69 sentinel_sectPr.remove(hdrftr_ref)
70 # ---the sentinel `w:sectPr` now controls the new last section---
71 return sentinel_sectPr
72
73 def clear_content(self):
74 """Remove all content child elements from this <w:body> element.
75
76 Leave the <w:sectPr> element if it is present.
77 """
78 for content_elm in self.xpath("./*[not(self::w:sectPr)]"):
79 self.remove(content_elm)
80
81 @property
82 def inner_content_elements(self) -> List[CT_P | CT_Tbl]:
83 """Generate all `w:p` and `w:tbl` elements in this document-body.
84
85 Elements appear in document order. Elements shaded by nesting in a `w:ins` or
86 other "wrapper" element will not be included.
87 """
88 return self.xpath("./w:p | ./w:tbl")

Callers

nothing calls this directly

Calls 2

ZeroOrMoreClass · 0.90
ZeroOrOneClass · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…