docx_plus.layout.breaks¶
Insert a section break at an arbitrary paragraph. python-docx's
Document.add_section only appends a new section at the end of the
document; this module handles the mid-document case by cloning the
trailing <w:sectPr> into the chosen paragraph's pPr and setting
<w:type> to the requested start kind.
docx_plus.layout.breaks ¶
Insert a section break at an arbitrary paragraph.
python-docx's :meth:Document.add_section only appends a new section at
the end of the document. Inserting a section break mid-document requires
moving the existing trailing <w:sectPr>'s properties into the chosen
paragraph and writing a fresh <w:type> value to mark the break style.
:func:insert_section_break does that in one call.
This module imports only from docx_plus.core and python-docx
internals via the section proxy (SPEC §9.1).
SectionStartType
module-attribute
¶
insert_section_break ¶
insert_section_break(
paragraph: Paragraph, *, start_type: SectionStartType = "nextPage"
) -> Section
Insert a section break at paragraph.
The break splits the document so that the chosen paragraph becomes
the last paragraph of a new section. Section properties (page size,
margins, header/footer references) are copied from the document's
trailing <w:sectPr>, then <w:type> is set on the inserted
copy so Word knows how the following section starts.
If paragraph already carries a section break, this call replaces
it (idempotent re-application is supported, e.g. to switch the
break type from nextPage to continuous).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
paragraph
|
Paragraph
|
A python-docx :class: |
required |
start_type
|
SectionStartType
|
How the following section begins. |
'nextPage'
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
Section
|
class: |
Section
|
|
|
Section
|
(margins, columns, headers). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Example
from docx import Document from docx_plus.layout import insert_section_break doc = Document() doc.add_paragraph("intro") p = doc.add_paragraph("split here") doc.add_paragraph("after the break") new_section = insert_section_break(p, start_type="continuous")