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

Skip to content

Commit 5cb5820

Browse files
author
Steve Canny
committed
docs: document TabStops and TabStop
* fix some documentation bugs in the example code resulting from moving alignment and indent to ParagraphFormat.
1 parent 58e2151 commit 5cb5820

File tree

3 files changed

+95
-27
lines changed

3 files changed

+95
-27
lines changed

docs/api/text.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,19 @@ Text-related objects
3131

3232
.. autoclass:: docx.text.run.Font()
3333
:members:
34+
35+
36+
|TabStop| objects
37+
-----------------
38+
39+
.. autoclass:: docx.text.tabstops.TabStop()
40+
:members:
41+
42+
43+
|TabStops| objects
44+
------------------
45+
46+
.. autoclass:: docx.text.tabstops.TabStops()
47+
:members: clear_all
48+
49+
.. automethod:: docx.text.tabstops.TabStops.add_tab_stop(position, alignment=WD_TAB_ALIGNMENT.LEFT, leader=WD_TAB_LEADER.SPACES)

docs/user/text.rst

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -93,40 +93,87 @@ inheritance from the style hierarchy::
9393
>>> paragraph = document.add_paragraph()
9494
>>> paragraph_format = paragraph.paragraph_format
9595

96-
>>> paragraph.left_indent
96+
>>> paragraph_format.left_indent
9797
None # indicating indentation is inherited from the style hierarchy
98-
>>> paragraph.left_indent = Inches(0.5)
99-
>>> paragraph.left_indent
98+
>>> paragraph_format.left_indent = Inches(0.5)
99+
>>> paragraph_format.left_indent
100100
457200
101-
>>> paragraph.left_indent.inches
101+
>>> paragraph_format.left_indent.inches
102102
0.5
103103

104104

105105
Right-side indent works in a similar way::
106106

107107
>>> from docx.shared import Pt
108-
>>> paragraph.right_indent
108+
>>> paragraph_format.right_indent
109109
None
110-
>>> paragraph.right_indent = Pt(24)
111-
>>> paragraph.right_indent
110+
>>> paragraph_format.right_indent = Pt(24)
111+
>>> paragraph_format.right_indent
112112
304800
113-
>>> paragraph.right_indent.pt
113+
>>> paragraph_format.right_indent.pt
114114
24.0
115115

116116

117+
118+
117119
First-line indent is specified using the
118120
:attr:`~.ParagraphFormat.first_line_indent` property and is interpreted
119121
relative to the left indent. A negative value indicates a hanging indent::
120122

121-
>>> paragraph.first_line_indent
123+
>>> paragraph_format.first_line_indent
122124
None
123-
>>> paragraph.first_line_indent = Inches(-0.25)
124-
>>> paragraph.first_line_indent
125+
>>> paragraph_format.first_line_indent = Inches(-0.25)
126+
>>> paragraph_format.first_line_indent
125127
-228600
126-
>>> paragraph.first_line_indent.inches
128+
>>> paragraph_format.first_line_indent.inches
127129
-0.25
128130

129131

132+
Tab stops
133+
~~~~~~~~~
134+
135+
A tab stop determines the rendering of a tab character in the text of
136+
a paragraph. In particular, it specifies the position where the text
137+
following the tab character will start, how it will be aligned to that
138+
position, and an optional leader character that will fill the horizontal
139+
space spanned by the tab.
140+
141+
The tab stops for a paragraph or style are contained in a |TabStops| object
142+
accessed using the :attr:`~.ParagraphFormat.tab_stops` property on
143+
|ParagraphFormat|::
144+
145+
>>> tab_stops = paragraph_format.tab_stops
146+
>>> tab_stops
147+
<docx.text.tabstops.TabStops object at 0x106b802d8>
148+
149+
A new tab stop is added using the :meth:`~.TabStops.add_tab_stop` method::
150+
151+
>>> tab_stop = tab_stops.add_tab_stop(Inches(1.5))
152+
>>> tab_stop.position
153+
1371600
154+
>>> tab_stop.position.inches
155+
1.5
156+
157+
Alignment defaults to left, but may be specified by providing a member of the
158+
:ref:`WdTabAlignment` enumeration. The leader character defaults to spaces,
159+
but may be specified by providing a member of the :ref:`WdTabLeader`
160+
enumeration::
161+
162+
>>> from docx.enum.text import WD_TAB_ALIGNMENT, WD_TAB_LEADER
163+
>>> tab_stop = tab_stops.add_tab_stop(Inches(1.5), WD_TAB_ALIGNMENT.RIGHT, WD_TAB_LEADER.DOTS)
164+
>>> print(tab_stop.alignment)
165+
RIGHT (2)
166+
>>> print(tab_stop.leader)
167+
DOTS (1)
168+
169+
Existing tab stops are accessed using sequence semantics on |TabStops|::
170+
171+
>>> tab_stops[0]
172+
<docx.text.tabstops.TabStop object at 0x1105427e8>
173+
174+
More details are available in the |TabStops| and |TabStop| API documentation
175+
176+
130177
Paragraph spacing
131178
~~~~~~~~~~~~~~~~~
132179

@@ -177,7 +224,7 @@ of the :ref:`WdLineSpacing` enumeration or |None|::
177224
None
178225

179226
>>> paragraph_format.line_spacing = Pt(18)
180-
>>> isinstance(Length, paragraph_format.line_spacing)
227+
>>> isinstance(paragraph_format.line_spacing, Length)
181228
True
182229
>>> paragraph_format.line_spacing.pt
183230
18.0

docx/text/tabstops.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@
1414

1515
class TabStops(ElementProxy):
1616
"""
17-
A sequence providing access to the tab stops of a paragraph or paragraph
18-
style. Supports iteration, indexed access, del, and len(). It is accesed
19-
using the `tab_stops` property of ParagraphFormat; it is not intended to
20-
be constructed directly.
17+
A sequence of |TabStop| objects providing access to the tab stops of
18+
a paragraph or paragraph style. Supports iteration, indexed access, del,
19+
and len(). It is accesed using the :attr:`~.ParagraphFormat.tab_stops`
20+
property of ParagraphFormat; it is not intended to be constructed
21+
directly.
2122
"""
2223

2324
__slots__ = ('_pPr')
@@ -68,11 +69,13 @@ def __len__(self):
6869
def add_tab_stop(self, position, alignment=WD_TAB_ALIGNMENT.LEFT,
6970
leader=WD_TAB_LEADER.SPACES):
7071
"""
71-
Add a new tab stop at *position*. Tab alignment defaults to left, but
72-
may be specified by passing a member of the :ref:`WdTabAlignment`
73-
enumeration as *alignment*. An optional leader character can be
74-
specified by passing a member of the :ref:`WdTabLeader` enumeration
75-
as *leader*.
72+
Add a new tab stop at *position*, a |Length| object specifying the
73+
location of the tab stop relative to the paragraph edge. A negative
74+
*position* value is valid and appears in hanging indentation. Tab
75+
alignment defaults to left, but may be specified by passing a member
76+
of the :ref:`WdTabAlignment` enumeration as *alignment*. An optional
77+
leader character can be specified by passing a member of the
78+
:ref:`WdTabLeader` enumeration as *leader*.
7679
"""
7780
tabs = self._pPr.get_or_add_tabs()
7881
tab = tabs.insert_tab_in_order(position, alignment, leader)
@@ -87,8 +90,8 @@ def clear_all(self):
8790

8891
class TabStop(ElementProxy):
8992
"""
90-
An individual tab stop applying to a paragraph or style. Each of these is
91-
a member of a set held in a |TabStops| object.
93+
An individual tab stop applying to a paragraph or style. Accessed using
94+
list semantics on its containing |TabStops| object.
9295
"""
9396

9497
__slots__ = ('_tab')
@@ -101,7 +104,7 @@ def __init__(self, element):
101104
def alignment(self):
102105
"""
103106
A member of :ref:`WdTabAlignment` specifying the alignment setting
104-
for this tab stop.
107+
for this tab stop. Read/write.
105108
"""
106109
return self._tab.val
107110

@@ -115,6 +118,7 @@ def leader(self):
115118
A member of :ref:`WdTabLeader` specifying a repeating character used
116119
as a "leader", filling in the space spanned by this tab. Assigning
117120
|None| produces the same result as assigning `WD_TAB_LEADER.SPACES`.
121+
Read/write.
118122
"""
119123
return self._tab.leader
120124

@@ -125,8 +129,9 @@ def leader(self, value):
125129
@property
126130
def position(self):
127131
"""
128-
The distance (in EMU) of this tab stop from the inside edge of the
129-
paragraph. May be positive or negative.
132+
A |Length| object representing the distance of this tab stop from the
133+
inside edge of the paragraph. May be positive or negative.
134+
Read/write.
130135
"""
131136
return self._tab.pos
132137

0 commit comments

Comments
 (0)