Skip to content

Commit 4ecf198

Browse files
author
Steve Canny
committed
acpt: add par-clear-paragraph.feature
With scenario for Paragraph.clear()
1 parent c89674d commit 4ecf198

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Feature: Clear paragraph content
2+
In order to change paragraph content while retaining its formatting
3+
As a developer using python-docx
4+
I need a way to remove the content of a paragraph
5+
6+
7+
@wip
8+
Scenario: Clear paragraph content
9+
Given a paragraph with content and formatting
10+
When I clear the paragraph content
11+
Then the paragraph has no content
12+
But the paragraph formatting is preserved

features/steps/paragraph.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
from behave import given, then, when
88

99
from docx import Document
10+
from docx.oxml import parse_xml
11+
from docx.oxml.ns import nsdecls
12+
from docx.text import Paragraph
1013

1114
from helpers import saved_docx_path, test_text
1215

@@ -25,6 +28,21 @@ def given_a_document_containing_three_paragraphs(context):
2528
context.document = document
2629

2730

31+
@given('a paragraph with content and formatting')
32+
def given_a_paragraph_with_content_and_formatting(context):
33+
p_xml = """\
34+
<w:p %s>
35+
<w:pPr>
36+
<w:pStyle w:val="%s"/>
37+
</w:pPr>
38+
<w:r>
39+
<w:t>foobar</w:t>
40+
</w:r>
41+
</w:p>""" % (nsdecls('w'), TEST_STYLE)
42+
p = parse_xml(p_xml)
43+
context.paragraph = Paragraph(p)
44+
45+
2846
# when ====================================================
2947

3048
@when('I add a run to the paragraph')
@@ -33,10 +51,15 @@ def when_add_new_run_to_paragraph(context):
3351

3452

3553
@when('I add text to the run')
36-
def when_add_new_text_to_run(context):
54+
def when_I_add_text_to_the_run(context):
3755
context.r.add_text(test_text)
3856

3957

58+
@when('I clear the paragraph content')
59+
def when_I_clear_the_paragraph_content(context):
60+
context.paragraph.clear()
61+
62+
4063
@when('I insert a paragraph above the second paragraph')
4164
def when_I_insert_a_paragraph_above_the_second_paragraph(context):
4265
paragraph = context.document.paragraphs[1]
@@ -65,6 +88,16 @@ def then_document_contains_text_I_added(context):
6588
assert r.text == test_text
6689

6790

91+
@then('the paragraph formatting is preserved')
92+
def then_the_paragraph_formatting_is_preserved(context):
93+
assert context.paragraph.style == TEST_STYLE
94+
95+
96+
@then('the paragraph has no content')
97+
def then_the_paragraph_has_no_content(context):
98+
assert context.paragraph.text == ''
99+
100+
68101
@then('the paragraph has the style I set')
69102
def then_the_paragraph_has_the_style_I_set(context):
70103
paragraph = Document(saved_docx_path).paragraphs[-1]

0 commit comments

Comments
 (0)