Skip to content

Commit 851bfc1

Browse files
committed
tab_stops: Add paragraph_format.tab_stops object
1 parent 3fdf8f2 commit 851bfc1

File tree

6 files changed

+35
-156
lines changed

6 files changed

+35
-156
lines changed

docx/oxml/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def OxmlElement(nsptag_str, attrs=None, nsdecls=None):
181181
from .text.paragraph import CT_P
182182
register_element_cls('w:p', CT_P)
183183

184-
from .text.parfmt import CT_Ind, CT_Jc, CT_PPr, CT_Spacing
184+
from .text.parfmt import CT_Ind, CT_Jc, CT_PPr, CT_Spacing, CT_TabStops
185185
register_element_cls('w:ind', CT_Ind)
186186
register_element_cls('w:jc', CT_Jc)
187187
register_element_cls('w:keepLines', CT_OnOff)
@@ -190,6 +190,7 @@ def OxmlElement(nsptag_str, attrs=None, nsdecls=None):
190190
register_element_cls('w:pPr', CT_PPr)
191191
register_element_cls('w:pStyle', CT_String)
192192
register_element_cls('w:spacing', CT_Spacing)
193+
register_element_cls('w:tabs', CT_TabStops)
193194
register_element_cls('w:widowControl', CT_OnOff)
194195

195196
from .text.run import CT_Br, CT_R, CT_Text

docx/oxml/text/parfmt.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from ...shared import Length
99
from ..simpletypes import ST_SignedTwipsMeasure, ST_TwipsMeasure
1010
from ..xmlchemy import (
11-
BaseOxmlElement, OptionalAttribute, RequiredAttribute, ZeroOrOne
11+
BaseOxmlElement, OptionalAttribute, RequiredAttribute, ZeroOrOne, OneOrMore
1212
)
1313

1414

@@ -50,6 +50,7 @@ class CT_PPr(BaseOxmlElement):
5050
pageBreakBefore = ZeroOrOne('w:pageBreakBefore', successors=_tag_seq[4:])
5151
widowControl = ZeroOrOne('w:widowControl', successors=_tag_seq[6:])
5252
numPr = ZeroOrOne('w:numPr', successors=_tag_seq[7:])
53+
tabs = ZeroOrOne('w:tabs', successors=_tag_seq[11:])
5354
spacing = ZeroOrOne('w:spacing', successors=_tag_seq[22:])
5455
ind = ZeroOrOne('w:ind', successors=_tag_seq[23:])
5556
jc = ZeroOrOne('w:jc', successors=_tag_seq[27:])
@@ -311,3 +312,10 @@ class CT_Spacing(BaseOxmlElement):
311312
before = OptionalAttribute('w:before', ST_TwipsMeasure)
312313
line = OptionalAttribute('w:line', ST_SignedTwipsMeasure)
313314
lineRule = OptionalAttribute('w:lineRule', WD_LINE_SPACING)
315+
316+
317+
class CT_TabStops(BaseOxmlElement):
318+
"""
319+
``<w:tabs>`` element, specifying the sorted list of tab stops.
320+
"""
321+
tab = OneOrMore('w:tab', successors=())

docx/text/parfmt.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
absolute_import, division, print_function, unicode_literals
99
)
1010

11+
from .tabstops import TabStops
1112
from ..enum.text import WD_LINE_SPACING
1213
from ..shared import ElementProxy, Emu, Length, Pt, Twips
1314

@@ -243,6 +244,14 @@ def space_before(self):
243244
def space_before(self, value):
244245
self._element.get_or_add_pPr().spacing_before = value
245246

247+
@property
248+
def tab_stops(self):
249+
"""
250+
The |TabStops| object providing access to the tab_stop
251+
properties for this paragraph, such as position, alignment, and leader.
252+
"""
253+
return TabStops(self._element)
254+
246255
@property
247256
def widow_control(self):
248257
"""

features/steps/tabs.py

Lines changed: 0 additions & 75 deletions
This file was deleted.

features/tab-access-tabs.feature

Lines changed: 0 additions & 79 deletions
This file was deleted.

tests/text/test_parfmt.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
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

@@ -20,6 +21,10 @@
2021

2122
class DescribeParagraphFormat(object):
2223

24+
def it_provides_access_to_its_tab_stops(self, tab_stops_get_fixture):
25+
tab_stops = tab_stops_get_fixture
26+
assert isinstance(tab_stops, TabStops)
27+
2328
def it_knows_its_alignment_value(self, alignment_get_fixture):
2429
paragraph_format, expected_value = alignment_get_fixture
2530
assert paragraph_format.alignment == expected_value
@@ -104,6 +109,16 @@ def it_can_change_its_on_off_props(self, on_off_set_fixture):
104109

105110
# fixtures -------------------------------------------------------
106111

112+
@pytest.fixture(params=[
113+
('w:p'),
114+
('w:p/w:pPr/w:tabs/w:tab{w:pos=2880}'),
115+
])
116+
def tab_stops_get_fixture(self, request):
117+
p_cxml = request.param
118+
paragraph_format = ParagraphFormat(element(p_cxml))
119+
tab_stops = paragraph_format.tab_stops
120+
return tab_stops
121+
107122
@pytest.fixture(params=[
108123
('w:p', None),
109124
('w:p/w:pPr', None),

0 commit comments

Comments
 (0)