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

Skip to content

Commit 17e7e38

Browse files
DKWoodsSteve Canny
authored andcommitted
tab: add TabStops.add_tab_stop()
1 parent 2bdaf7e commit 17e7e38

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

docx/text/tabstops.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
)
1010

1111
from ..shared import ElementProxy
12+
from docx.enum.text import WD_TAB_ALIGNMENT, WD_TAB_LEADER
1213

1314

1415
class TabStops(ElementProxy):
@@ -51,6 +52,19 @@ def __len__(self):
5152
return 0
5253
return len(tabs.tab_lst)
5354

55+
def add_tab_stop(self, position, alignment=WD_TAB_ALIGNMENT.LEFT,
56+
leader=WD_TAB_LEADER.SPACES):
57+
"""
58+
Add a new tab stop at *position*. Tab alignment defaults to left, but
59+
may be specified by passing a member of the :ref:`WdTabAlignment`
60+
enumeration as *alignment*. An optional leader character can be
61+
specified by passing a member of the :ref:`WdTabLeader` enumeration
62+
as *leader*.
63+
"""
64+
tabs = self._pPr.get_or_add_tabs()
65+
tab = tabs.insert_tab_in_order(position, alignment, leader)
66+
return TabStop(tab)
67+
5468

5569
class TabStop(ElementProxy):
5670
"""

features/tab-access-tabs.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ Feature: Access TabStop objects
2020
And I can access a tab stop by index
2121

2222

23-
@wip
2423
Scenario Outline: TabStops.add_tab_stop()
2524
Given a tab_stops having <count> tab stops
2625
When I add a tab stop

tests/text/test_tabstops.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,35 @@ def it_raises_on_indexed_access_when_empty(self):
159159
with pytest.raises(IndexError):
160160
tab_stops[0]
161161

162+
def it_can_add_a_tab_stop(self, add_tab_fixture):
163+
tab_stops, position, kwargs, expected_xml = add_tab_fixture
164+
tab_stops.add_tab_stop(position, **kwargs)
165+
assert tab_stops._element.xml == expected_xml
166+
162167
# fixture --------------------------------------------------------
163168

169+
@pytest.fixture(params=[
170+
('w:pPr', Twips(42), {},
171+
'w:pPr/w:tabs/w:tab{w:pos=42,w:val=left}'),
172+
('w:pPr', Twips(72), {'alignment': WD_TAB_ALIGNMENT.RIGHT},
173+
'w:pPr/w:tabs/w:tab{w:pos=72,w:val=right}'),
174+
('w:pPr', Twips(24),
175+
{'alignment': WD_TAB_ALIGNMENT.CENTER,
176+
'leader': WD_TAB_LEADER.DOTS},
177+
'w:pPr/w:tabs/w:tab{w:pos=24,w:val=center,w:leader=dot}'),
178+
('w:pPr/w:tabs/w:tab{w:pos=42}', Twips(72), {},
179+
'w:pPr/w:tabs/(w:tab{w:pos=42},w:tab{w:pos=72,w:val=left})'),
180+
('w:pPr/w:tabs/w:tab{w:pos=42}', Twips(24), {},
181+
'w:pPr/w:tabs/(w:tab{w:pos=24,w:val=left},w:tab{w:pos=42})'),
182+
('w:pPr/w:tabs/w:tab{w:pos=42}', Twips(42), {},
183+
'w:pPr/w:tabs/(w:tab{w:pos=42},w:tab{w:pos=42,w:val=left})'),
184+
])
185+
def add_tab_fixture(self, request):
186+
pPr_cxml, position, kwargs, expected_cxml = request.param
187+
tab_stops = TabStops(element(pPr_cxml))
188+
expected_xml = xml(expected_cxml)
189+
return tab_stops, position, kwargs, expected_xml
190+
164191
@pytest.fixture(params=[
165192
('w:pPr/w:tabs/w:tab{w:pos=0}', 0),
166193
('w:pPr/w:tabs/(w:tab{w:pos=1},w:tab{w:pos=2},w:tab{w:pos=3})', 1),

0 commit comments

Comments
 (0)