Skip to content

Commit 1153e5b

Browse files
DKWoodsSteve Canny
authored and
Steve Canny
committed
parfmt: add ParagraphFormat.tab_stops
1 parent b8e3de8 commit 1153e5b

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
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, self)
255+
246256
@property
247257
def widow_control(self):
248258
"""

tests/text/test_parfmt.py

Lines changed: 38 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,17 @@ 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, paragraph_format)
111+
assert tab_stops is tab_stops_
112+
113+
def it_reuses_the_tab_stops_object(self, tab_stops_lazy_fixture):
114+
paragraph_format, initial_tab_stops = tab_stops_lazy_fixture
115+
tab_stops = paragraph_format.tab_stops
116+
assert tab_stops is initial_tab_stops
117+
105118
# fixtures -------------------------------------------------------
106119

107120
@pytest.fixture(params=[
@@ -393,3 +406,28 @@ def space_before_set_fixture(self, request):
393406
paragraph_format = ParagraphFormat(element(p_cxml))
394407
expected_xml = xml(expected_p_cxml)
395408
return paragraph_format, value, expected_xml
409+
410+
@pytest.fixture
411+
def tab_stops_fixture(self, TabStops_, tab_stops_):
412+
p = element('w:p/w:pPr')
413+
pPr = p.pPr
414+
paragraph_format = ParagraphFormat(p, None)
415+
return paragraph_format, TabStops_, pPr, tab_stops_
416+
417+
@pytest.fixture
418+
def tab_stops_lazy_fixture(self):
419+
paragraph_format = ParagraphFormat(element('w:p'), None)
420+
original_tab_stops = paragraph_format.tab_stops
421+
return paragraph_format, original_tab_stops
422+
423+
# fixture components ---------------------------------------------
424+
425+
@pytest.fixture
426+
def TabStops_(self, request, tab_stops_):
427+
return class_mock(
428+
request, 'docx.text.parfmt.TabStops', return_value=tab_stops_
429+
)
430+
431+
@pytest.fixture
432+
def tab_stops_(self, request):
433+
return instance_mock(request, TabStops)

0 commit comments

Comments
 (0)