Skip to content

Commit bddd27a

Browse files
author
Steve Canny
committed
para: add Paragraph.paragraph_format
1 parent 3b0253b commit bddd27a

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

docx/text/paragraph.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Paragraph(Parented):
2020
"""
2121
def __init__(self, p, parent):
2222
super(Paragraph, self).__init__(parent)
23-
self._p = p
23+
self._p = self._element = p
2424

2525
def add_run(self, text=None, style=None):
2626
"""
@@ -76,6 +76,14 @@ def insert_paragraph_before(self, text=None, style=None):
7676
paragraph.style = style
7777
return paragraph
7878

79+
@property
80+
def paragraph_format(self):
81+
"""
82+
The |ParagraphFormat| object providing access to the formatting
83+
properties for this paragraph, such as line spacing and indentation.
84+
"""
85+
return ParagraphFormat(self._element)
86+
7987
@property
8088
def runs(self):
8189
"""

features/par-access-parfmt.feature

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

66

7-
@wip
87
Scenario: Get paragraph format object
98
Given a paragraph
109
Then paragraph.paragraph_format is its ParagraphFormat object

features/steps/paragraph.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def then_paragraph_paragraph_format_is_its_parfmt_object(context):
9696
paragraph = context.paragraph
9797
paragraph_format = paragraph.paragraph_format
9898
assert isinstance(paragraph_format, ParagraphFormat)
99-
assert paragraph_format.element is paragraph.element
99+
assert paragraph_format.element is paragraph._element
100100

101101

102102
@then('paragraph.style is {value_key}')

tests/text/test_paragraph.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ def it_can_change_its_alignment_value(self, alignment_set_fixture):
6363
paragraph.alignment = value
6464
assert paragraph._p.xml == expected_xml
6565

66+
def it_provides_access_to_its_paragraph_format(self, parfmt_fixture):
67+
paragraph, ParagraphFormat_, paragraph_format_ = parfmt_fixture
68+
paragraph_format = paragraph.paragraph_format
69+
ParagraphFormat_.assert_called_once_with(paragraph._element)
70+
assert paragraph_format is paragraph_format_
71+
6672
def it_provides_access_to_the_runs_it_contains(self, runs_fixture):
6773
paragraph, Run_, r_, r_2_, run_, run_2_ = runs_fixture
6874
runs = paragraph.runs
@@ -183,6 +189,11 @@ def _insert_before_fixture(self, request):
183189
expected_xml = xml(expected_cxml)
184190
return paragraph, body, expected_xml
185191

192+
@pytest.fixture
193+
def parfmt_fixture(self, ParagraphFormat_, paragraph_format_):
194+
paragraph = Paragraph(element('w:p'), None)
195+
return paragraph, ParagraphFormat_, paragraph_format_
196+
186197
@pytest.fixture
187198
def runs_fixture(self, p_, Run_, r_, r_2_, runs_):
188199
paragraph = Paragraph(p_, None)
@@ -258,6 +269,17 @@ def _insert_paragraph_before_(self, request):
258269
def p_(self, request, r_, r_2_):
259270
return instance_mock(request, CT_P, r_lst=(r_, r_2_))
260271

272+
@pytest.fixture
273+
def ParagraphFormat_(self, request, paragraph_format_):
274+
return class_mock(
275+
request, 'docx.text.paragraph.ParagraphFormat',
276+
return_value=paragraph_format_
277+
)
278+
279+
@pytest.fixture
280+
def paragraph_format_(self, request):
281+
return instance_mock(request, ParagraphFormat)
282+
261283
@pytest.fixture
262284
def part_prop_(self, request, document_part_):
263285
return property_mock(

0 commit comments

Comments
 (0)