feat: Section break insert and delete API#44
Conversation
Add `has_section_break` property, `insert_section_break()` method, and `remove_section_break()` method to the Paragraph class. These enable inserting a section break after any paragraph and removing an existing one, with proper section enumeration consistency. Co-Authored-By: Claude Opus 4.6 <[email protected]>
|
Security Agent Report SECURITY_PASS Security Review — PR #44Branch: agent/issue-21 SummaryNo security issues found. The changes are minimal, well-scoped, and follow established safe patterns in the codebase. Changed Files
|
|
Security Agent Report SECURITY_PASS Security Review — PR #44 (Section Break Insert/Delete API)Reviewed: 2026-04-03
SummaryNo security issues found. The changes are limited to adding Checks PerformedXML Injection / XXE
Path Traversal
New Dependencies
Sensitive Data / Secrets
Unsafe File Handling
NotesThe implementation correctly delegates mutation to the existing |
|
Review Agent REVIEW_APPROVED SummaryPR #21 adds Positive Notes
Minor Issues (non-blocking)1. Docstring typo —
|
Summary
Implements #21
This PR was automatically generated by the Developer Agent.
Original Issue
Feature Description
python-docx supports appending a new section at the end of a document via
Document.add_section(), and reading/modifyingsection.start_typeon existing sections. However, there is no support for inserting a section break at an arbitrary position within the document body, nor for deleting an existing section break. In Word, section breaks are stored as<w:sectPr>elements inside paragraph properties (<w:pPr>) — not at the document body level — which makes mid-document insertion and deletion non-trivial without a proper API.This issue adds high-level methods for inserting a section break after any paragraph and deleting an existing one.
Acceptance Criteria
Insertion
paragraph.insert_section_break(start_type=WD_SECTION.NEW_PAGE)inserts a section break after the given paragraph by adding a<w:sectPr><w:type w:val="..."/></w:sectPr>inside that paragraph's<w:pPr>start_typevalues:NEW_PAGE,ODD_PAGE,EVEN_PAGE,CONTINUOUS,NEXT_PAGE(alias forNEW_PAGE)Sectionobject representing the inserted sectionsectPrtype rather than duplicating itDeletion
paragraph.remove_section_break()removes the<w:sectPr>from the paragraph's<w:pPr>if one exists, merging that section into the nextparagraph.has_section_break(bool property) returnsTrueif the paragraph contains a section breaksectPrremove_section_break()on a paragraph without a section break is a no-op (no exception raised)sectPr(in<w:body>) is explicitly disallowed and raisesValueErrorwith a clear message — the document must always have at least one sectionSection enumeration consistency
document.sectionsreflects the updated section count correctlySuggested Implementation
docx/text/paragraph.py— add toParagraph:Tests — add to
tests/unit/text/test_paragraph.py:insert_section_breakadds correct XML for eachstart_typesectPrreplaces type, doesn't duplicatehas_section_breakreturns correct boolremove_section_breakremovessectPr, no-op when absentdocument.sectionscount updates after insert/removesectPrraisesValueErrorDependencies
None. (Builds on the existing
SectionandWD_SECTIONinfrastructure.)Out of Scope
Documentobject — can be added as a convenience wrapper in a follow-upGenerated by Developer Agent using Claude Code