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

Method add_section_break

src/docx/oxml/document.py:51–71  ·  view source on GitHub ↗

Return `w:sectPr` element for new section added at end of document. The last `w:sectPr` becomes the second-to-last, with the new `w:sectPr` being an exact clone of the previous one, except that all header and footer references are removed (and are therefore now "inherited" f

(self)

Source from the content-addressed store, hash-verified

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.

Callers 2

add_sectionMethod · 0.80

Calls 3

set_sectPrMethod · 0.80
cloneMethod · 0.80
xpathMethod · 0.80

Tested by 1