Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9113b49

Browse files
DKWoodsSteve Canny
authored andcommitted
parfmt: add ParagraphFormat.tab_stops
1 parent 7709787 commit 9113b49

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

docx/text/parfmt.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
)
1010

1111
from ..enum.text import WD_LINE_SPACING
12-
from ..shared import ElementProxy, Emu, Length, Pt, Twips
12+
from ..shared import ElementProxy, Emu, lazyproperty, Length, Pt, Twips
13+
from .tabstops import TabStops
1314

1415

1516
class ParagraphFormat(ElementProxy):
@@ -19,7 +20,7 @@ class ParagraphFormat(ElementProxy):
1920
control.
2021
"""
2122

22-
__slots__ = ()
23+
__slots__ = ('_tab_stops',)
2324

2425
@property
2526
def alignment(self):
@@ -243,6 +244,15 @@ def space_before(self):
243244
def space_before(self, value):
244245
self._element.get_or_add_pPr().spacing_before = value
245246

247+
@lazyproperty
248+
def tab_stops(self):
249+
"""
250+
|TabStops| object providing access to the tab stops defined for this
251+
paragraph format.
252+
"""
253+
pPr = self._element.get_or_add_pPr()
254+
return TabStops(pPr)
255+
246256
@property
247257
def widow_control(self):
248258
"""

docx/text/tabstops.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,9 @@ class TabStops(ElementProxy):
1818
using the `tab_stops` property of ParagraphFormat; it is not intended to
1919
be constructed directly.
2020
"""
21+
22+
__slots__ = ('_pPr')
23+
24+
def __init__(self, element):
25+
super(TabStops, self).__init__(element, None)
26+
self._pPr = element

features/txt-parfmt-props.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ Feature: Get or set paragraph formatting properties
44
I need a ParagraphFormat object with read/write formatting properties
55

66

7-
@wip
87
Scenario: Get tab stops
98
Given a paragraph format
109
Then paragraph_format.tab_stops is a TabStops object

tests/text/test_parfmt.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
from docx.enum.text import WD_ALIGN_PARAGRAPH, WD_LINE_SPACING
1313
from docx.shared import Pt
1414
from docx.text.parfmt import ParagraphFormat
15+
from docx.text.tabstops import TabStops
1516

1617
import pytest
1718

1819
from ..unitutil.cxml import element, xml
20+
from ..unitutil.mock import class_mock, instance_mock
1921

2022

2123
class DescribeParagraphFormat(object):
@@ -102,6 +104,12 @@ def it_can_change_its_on_off_props(self, on_off_set_fixture):
102104
setattr(paragraph_format, prop_name, value)
103105
assert paragraph_format._element.xml == expected_xml
104106

107+
def it_provides_access_to_its_tab_stops(self, tab_stops_fixture):
108+
paragraph_format, TabStops_, pPr, tab_stops_ = tab_stops_fixture
109+
tab_stops = paragraph_format.tab_stops
110+
TabStops_.assert_called_once_with(pPr)
111+
assert tab_stops is tab_stops_
112+
105113
# fixtures -------------------------------------------------------
106114

107115
@pytest.fixture(params=[
@@ -393,3 +401,22 @@ def space_before_set_fixture(self, request):
393401
paragraph_format = ParagraphFormat(element(p_cxml))
394402
expected_xml = xml(expected_p_cxml)
395403
return paragraph_format, value, expected_xml
404+
405+
@pytest.fixture
406+
def tab_stops_fixture(self, TabStops_, tab_stops_):
407+
p = element('w:p/w:pPr')
408+
pPr = p.pPr
409+
paragraph_format = ParagraphFormat(p, None)
410+
return paragraph_format, TabStops_, pPr, tab_stops_
411+
412+
# fixture components ---------------------------------------------
413+
414+
@pytest.fixture
415+
def TabStops_(self, request, tab_stops_):
416+
return class_mock(
417+
request, 'docx.text.parfmt.TabStops', return_value=tab_stops_
418+
)
419+
420+
@pytest.fixture
421+
def tab_stops_(self, request):
422+
return instance_mock(request, TabStops)

0 commit comments

Comments
 (0)