Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docx2everything/converters/markdown_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ def parse_paragraph_to_markdown(p_elem, numbering_info=None, hyperlinks=None, im
# Check for section break
has_section_break = False
sectPr = p_elem.find(qn('w:sectPr'))
if sectPr is not None:
nested_sectPr = pPr.find(qn('w:sectPr')) if pPr is not None else None
if sectPr is not None or nested_sectPr is not None:
has_section_break = True

# Check for heading
Expand Down
46 changes: 46 additions & 0 deletions tests/test_markdown_section_break.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import xml.etree.ElementTree as ET

from docx2everything.converters.markdown_converter import parse_paragraph_to_markdown


W_NS = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"


def paragraph_with_nested_section_break():
xml = f"""
<w:p xmlns:w="{W_NS}">
<w:pPr>
<w:sectPr/>
</w:pPr>
<w:r><w:t>Body text</w:t></w:r>
</w:p>
"""
return ET.fromstring(xml)


def paragraph_with_direct_section_break():
xml = f"""
<w:p xmlns:w="{W_NS}">
<w:sectPr/>
<w:r><w:t>Body text</w:t></w:r>
</w:p>
"""
return ET.fromstring(xml)


def test_nested_section_break_outputs_section_break_marker():
paragraph = paragraph_with_nested_section_break()

markdown = parse_paragraph_to_markdown(paragraph)

assert markdown.startswith("<!-- Section Break -->")
assert markdown.endswith("Body text")


def test_direct_section_break_still_outputs_section_break_marker():
paragraph = paragraph_with_direct_section_break()

markdown = parse_paragraph_to_markdown(paragraph)

assert markdown.startswith("<!-- Section Break -->")
assert markdown.endswith("Body text")