Skip to content

Commit 70f0605

Browse files
author
Steve Canny
committed
para: add Paragraph.clear()
1 parent 4ecf198 commit 70f0605

4 files changed

Lines changed: 42 additions & 1 deletion

File tree

docx/oxml/text.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ def add_p_before(self):
4040
self.addprevious(new_p)
4141
return new_p
4242

43+
def clear_content(self):
44+
"""
45+
Remove all child elements, except the ``<w:pPr>`` element if present.
46+
"""
47+
for child in self[:]:
48+
if child.tag == qn('w:pPr'):
49+
continue
50+
self.remove(child)
51+
4352
def set_sectPr(self, sectPr):
4453
"""
4554
Unconditionally replace or add *sectPr* as a grandchild in the

docx/text.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ def add_run(self, text=None, style=None):
7575
run.style = style
7676
return run
7777

78+
def clear(self):
79+
"""
80+
Return this same paragraph after removing all its content.
81+
Paragraph-level formatting, such as style, is preserved.
82+
"""
83+
self._p.clear_content()
84+
return self
85+
7886
def insert_paragraph_before(self, text=None, style=None):
7987
"""
8088
Return a newly created paragraph, inserted directly before this

features/par-clear-paragraph.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ Feature: Clear paragraph content
44
I need a way to remove the content of a paragraph
55

66

7-
@wip
87
Scenario: Clear paragraph content
98
Given a paragraph with content and formatting
109
When I clear the paragraph content

tests/test_text.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ def it_can_insert_a_paragraph_before_itself(self, insert_before_fixture):
7373
assert new_paragraph.style == style
7474
assert body.xml == expected_xml
7575

76+
def it_can_remove_its_content_while_preserving_formatting(
77+
self, clear_fixture):
78+
paragraph, expected_xml = clear_fixture
79+
_paragraph = paragraph.clear()
80+
assert paragraph._p.xml == expected_xml
81+
assert _paragraph is paragraph
82+
7683
# fixtures -------------------------------------------------------
7784

7885
@pytest.fixture(params=[
@@ -90,6 +97,24 @@ def add_run_fixture(self, request, paragraph):
9097
expected_xml = a_p().with_nsdecls().with_child(r_bldr).xml()
9198
return paragraph, text, style, expected_xml
9299

100+
@pytest.fixture
101+
def clear_fixture(self, request):
102+
"""
103+
After XML should be before XML with content removed. So snapshot XML
104+
after adding formatting but before adding content to get after XML.
105+
"""
106+
style, text = ('Heading1', 'foo\tbar')
107+
p = OxmlElement('w:p')
108+
# expected_xml -----------------
109+
if style is not None:
110+
p.style = style
111+
expected_xml = p.xml
112+
# paragraph --------------------
113+
paragraph = Paragraph(p)
114+
if text is not None:
115+
paragraph.add_run(text)
116+
return paragraph, expected_xml
117+
93118
@pytest.fixture
94119
def insert_before_fixture(self):
95120
text, style = 'foobar', 'Heading1'

0 commit comments

Comments
 (0)