|
| 1 | + |
| 2 | +Tab Stops |
| 3 | +========= |
| 4 | + |
| 5 | +WordprocessingML allows for custom specification of tab stops at the |
| 6 | +paragraph level. Tab stop spacing is a subset of paragraph formatting in |
| 7 | +this system, so will be implemented within the |
| 8 | +docx.text.parfmt.ParagraphFormatting object. Tab stops will be handled as |
| 9 | +a List-like TabStops object made up of TabStop objects. |
| 10 | + |
| 11 | +A TabStop object has three properties, alignment, leader, and position. |
| 12 | +Alignment is a WD_TAB_ALIGNMENT member and position is a Length() object. |
| 13 | + |
| 14 | +Tab stops are always sorted in position order. Alignment defaults to |
| 15 | +WD_TAB_ALIGNMENT.LEFT, and leader defaults to WD_TAB_LEADER.SPACES. |
| 16 | + |
| 17 | +Tab stops specify how tab characters in a paragraph are rendered. Insertion |
| 18 | +of tab characters is accomplished using the Run object. |
| 19 | + |
| 20 | + |
| 21 | +Protocol |
| 22 | +-------- |
| 23 | + |
| 24 | +.. highlight:: python |
| 25 | + |
| 26 | +Getting and setting tab stops:: |
| 27 | + |
| 28 | + >>> tab_stops = paragraph.paragraph_format.tab_stops |
| 29 | + >>> tab_stops |
| 30 | + <docx.text.parfmt.TabStops object at 0x104ea8c30> |
| 31 | + |
| 32 | + >>> tab_stop = tab_stops.add_tab_stop(Inches(2), WD_TAB_ALIGNMENT.LEFT, WD_TAB_LEADER.DOTS) |
| 33 | + |
| 34 | + # add_tab_stop defaults to WD_TAB_ALIGNMENT.LEFT, WD_TAB_LEADER.SPACES |
| 35 | + |
| 36 | + >>> tab_stop = tab_stops.add_tab_stop(Inches(0.5)) |
| 37 | + >>> tab_stop.alignment |
| 38 | + WD_TAB_ALIGNMENT.LEFT |
| 39 | + >>> tab_stop.leader |
| 40 | + WD_TAB_LEADER.SPACES |
| 41 | + |
| 42 | + # TabStop properties are read/write |
| 43 | + |
| 44 | + >>> tab_stop.position = Inches(2.5) |
| 45 | + >>> tab_stop.alignment = WD_TAB_ALIGNMENT.CENTER |
| 46 | + >>> tab_stop.leader = WD_TAB_LEADER.DASHES |
| 47 | + |
| 48 | + # Tab stops are sorted into position order as created or modified |
| 49 | + |
| 50 | + >>> [(t.position, t.alignment) for t in tab_stops] |
| 51 | + [(914400, WD_TAB_ALIGNMENT.LEFT), (2286000, WD_TAB_ALIGNMENT.CENTER)] |
| 52 | + |
| 53 | + # A tab stop is deleted using del statement |
| 54 | + |
| 55 | + >>> len(tab_stops) |
| 56 | + 2 |
| 57 | + >>> del tab_stops[1] |
| 58 | + >>> len(tab_stops) |
| 59 | + 1 |
| 60 | + |
| 61 | + # Restore default tabs |
| 62 | + |
| 63 | + >>> tab_stops.clear() |
| 64 | + |
| 65 | + |
| 66 | +Word Behavior |
| 67 | +------------- |
| 68 | + |
| 69 | +When the w:tabs element is empty or not present, Word uses default tab stops |
| 70 | +(typically every half inch). |
| 71 | + |
| 72 | +Word resumes using default tab stops following the last specified tab stop. |
| 73 | + |
| 74 | +TabStops must be in position order within the XML. If they are not, the out- |
| 75 | +of-order tab stop will appear in the ruler and in the properties dialog, but |
| 76 | +will not actually be used by Word. |
| 77 | + |
| 78 | + |
| 79 | +XML Semantics |
| 80 | +------------- |
| 81 | + |
| 82 | +* Both "num" and "list" alignment are a legacy from early versions of Word |
| 83 | + before hanging indents were available. Both are deprecated. |
| 84 | + |
| 85 | +* "start" alignment is equivalent to "left", and "end" alignment are equivalent |
| 86 | + to "right". (Confirmed with manually edited XML.) |
| 87 | + |
| 88 | +* A "clear" tab stop is not shown in Word's tab bar and default tab behavior |
| 89 | + is followed in the document. That is, Word ignores that tab stop |
| 90 | + specification completely, acting as if it were not there at all. This |
| 91 | + allows a tab stop inherited from a style, for example, to be ignored. |
| 92 | + |
| 93 | +* The w:pos attribute uses twips rather than EMU. |
| 94 | + |
| 95 | +* The w:tabs element must be removed when empty. If present, it must contain |
| 96 | + at least one w:tab element. |
| 97 | + |
| 98 | + |
| 99 | +Specimen XML |
| 100 | +------------ |
| 101 | + |
| 102 | +.. highlight:: xml |
| 103 | + |
| 104 | +:: |
| 105 | + |
| 106 | + <w:pPr> |
| 107 | + <w:tabs> |
| 108 | + <w:tab w:val="left" w:leader="dot" w:pos="2880"/> |
| 109 | + <w:tab w:val="decimal" w:pos="6480"/> |
| 110 | + </w:tabs> |
| 111 | + </w:pPr> |
| 112 | + |
| 113 | + |
| 114 | +Enumerations |
| 115 | +------------ |
| 116 | + |
| 117 | +* `WdTabAlignment Enumeration on MSDN`_ |
| 118 | + |
| 119 | +.. _WdTabAlignment Enumeration on MSDN: |
| 120 | + https://msdn.microsoft.com/EN-US/library/office/ff195609.aspx |
| 121 | + |
| 122 | +================= ======== ===== |
| 123 | +Name XML Value |
| 124 | +================= ======== ===== |
| 125 | +wdAlignTabBar bar 4 |
| 126 | +wdAlignTabCenter center 1 |
| 127 | +wdAlignTabDecimal decimal 3 |
| 128 | +wdAlignTabLeft left 0 |
| 129 | +wdAlignTabList list 6 |
| 130 | +wdAlignTabRight right 2 |
| 131 | +================= ======== ===== |
| 132 | + |
| 133 | +Additional Enumeration values not appearing in WdTabAlignment |
| 134 | + |
| 135 | +=============== ======== ===== |
| 136 | +Name XML Value |
| 137 | +=============== ======== ===== |
| 138 | +wdAlignTabClear clear 101 |
| 139 | +wdAlignTabEnd end 102 |
| 140 | +wdAlignTabNum num 103 |
| 141 | +wdAlignTabStart start 104 |
| 142 | +=============== ======== ===== |
| 143 | + |
| 144 | + |
| 145 | +* `WdTabLeader Enumeration on MSDN`_ |
| 146 | + |
| 147 | +.. _WdTabLeader Enumeration on MSDN: |
| 148 | + https://msdn.microsoft.com/en-us/library/office/ff845050.aspx |
| 149 | + |
| 150 | +==================== ========== ===== |
| 151 | +Name XML Value |
| 152 | +==================== ========== ===== |
| 153 | +wdTabLeaderDashes hyphen 2 |
| 154 | +wdTabLeaderDots dot 1 |
| 155 | +wdTabLeaderHeavy heavy 4 |
| 156 | +wdTabLeaderLines underscore 3 |
| 157 | +wdTabLeaderMiddleDot middleDot 5 |
| 158 | +wdTabLeaderSpaces none 0 |
| 159 | +==================== ========== ===== |
| 160 | + |
| 161 | + |
| 162 | +MS API Protocol |
| 163 | +--------------- |
| 164 | + |
| 165 | +The MS API defines a `TabStops object`_ which is a collection of |
| 166 | +`TabStop objects`_. |
| 167 | + |
| 168 | +.. _TabStops object: |
| 169 | + https://msdn.microsoft.com/EN-US/library/office/ff192806.aspx |
| 170 | + |
| 171 | +.. _TabStop objects: |
| 172 | + https://msdn.microsoft.com/EN-US/library/office/ff195736.aspx |
| 173 | + |
| 174 | + |
| 175 | +Schema excerpt |
| 176 | +-------------- |
| 177 | + |
| 178 | +:: |
| 179 | + |
| 180 | + <xsd:complexType name="CT_PPr"> <!-- denormalized --> |
| 181 | + <xsd:sequence> |
| 182 | + <xsd:element name="pStyle" type="CT_String" minOccurs="0"/> |
| 183 | + <xsd:element name="keepNext" type="CT_OnOff" minOccurs="0"/> |
| 184 | + <xsd:element name="keepLines" type="CT_OnOff" minOccurs="0"/> |
| 185 | + <xsd:element name="pageBreakBefore" type="CT_OnOff" minOccurs="0"/> |
| 186 | + <xsd:element name="framePr" type="CT_FramePr" minOccurs="0"/> |
| 187 | + <xsd:element name="widowControl" type="CT_OnOff" minOccurs="0"/> |
| 188 | + <xsd:element name="numPr" type="CT_NumPr" minOccurs="0"/> |
| 189 | + <xsd:element name="suppressLineNumbers" type="CT_OnOff" minOccurs="0"/> |
| 190 | + <xsd:element name="pBdr" type="CT_PBdr" minOccurs="0"/> |
| 191 | + <xsd:element name="shd" type="CT_Shd" minOccurs="0"/> |
| 192 | + <xsd:element name="tabs" type="CT_Tabs" minOccurs="0"/> |
| 193 | + <xsd:element name="suppressAutoHyphens" type="CT_OnOff" minOccurs="0"/> |
| 194 | + <xsd:element name="kinsoku" type="CT_OnOff" minOccurs="0"/> |
| 195 | + <xsd:element name="wordWrap" type="CT_OnOff" minOccurs="0"/> |
| 196 | + <xsd:element name="overflowPunct" type="CT_OnOff" minOccurs="0"/> |
| 197 | + <xsd:element name="topLinePunct" type="CT_OnOff" minOccurs="0"/> |
| 198 | + <xsd:element name="autoSpaceDE" type="CT_OnOff" minOccurs="0"/> |
| 199 | + <xsd:element name="autoSpaceDN" type="CT_OnOff" minOccurs="0"/> |
| 200 | + <xsd:element name="bidi" type="CT_OnOff" minOccurs="0"/> |
| 201 | + <xsd:element name="adjustRightInd" type="CT_OnOff" minOccurs="0"/> |
| 202 | + <xsd:element name="snapToGrid" type="CT_OnOff" minOccurs="0"/> |
| 203 | + <xsd:element name="spacing" type="CT_Spacing" minOccurs="0"/> |
| 204 | + <xsd:element name="ind" type="CT_Ind" minOccurs="0"/> |
| 205 | + <xsd:element name="contextualSpacing" type="CT_OnOff" minOccurs="0"/> |
| 206 | + <xsd:element name="mirrorIndents" type="CT_OnOff" minOccurs="0"/> |
| 207 | + <xsd:element name="suppressOverlap" type="CT_OnOff" minOccurs="0"/> |
| 208 | + <xsd:element name="jc" type="CT_Jc" minOccurs="0"/> |
| 209 | + <xsd:element name="textDirection" type="CT_TextDirection" minOccurs="0"/> |
| 210 | + <xsd:element name="textAlignment" type="CT_TextAlignment" minOccurs="0"/> |
| 211 | + <xsd:element name="textboxTightWrap" type="CT_TextboxTightWrap" minOccurs="0"/> |
| 212 | + <xsd:element name="outlineLvl" type="CT_DecimalNumber" minOccurs="0"/> |
| 213 | + <xsd:element name="divId" type="CT_DecimalNumber" minOccurs="0"/> |
| 214 | + <xsd:element name="cnfStyle" type="CT_Cnf" minOccurs="0"/> |
| 215 | + <xsd:element name="rPr" type="CT_ParaRPr" minOccurs="0"/> |
| 216 | + <xsd:element name="sectPr" type="CT_SectPr" minOccurs="0"/> |
| 217 | + <xsd:element name="pPrChange" type="CT_PPrChange" minOccurs="0"/> |
| 218 | + </xsd:sequence> |
| 219 | + </xsd:complexType> |
| 220 | + |
| 221 | + <xsd:complexType name="CT_Tabs"> |
| 222 | + <xsd:sequence> |
| 223 | + <xsd:element name="tab" type="CT_TabStop" maxOccurs="unbounded"/> |
| 224 | + </xsd:sequence> |
| 225 | + </xsd:complexType> |
| 226 | + |
| 227 | + <xsd:complexType name="CT_TabStop"> |
| 228 | + <xsd:attribute name="val" type="ST_TabJc" use="required"/> |
| 229 | + <xsd:attribute name="leader" type="ST_TabTlc" use="optional"/> |
| 230 | + <xsd:attribute name="pos" type="ST_SignedTwipsMeasure" use="required"/> |
| 231 | + </xsd:complexType> |
| 232 | + |
| 233 | + <xsd:simpleType name="ST_TabJc"> |
| 234 | + <xsd:restriction base="xsd:string"> |
| 235 | + <xsd:enumeration value="clear"/> |
| 236 | + <xsd:enumeration value="start"/> |
| 237 | + <xsd:enumeration value="center"/> |
| 238 | + <xsd:enumeration value="end"/> |
| 239 | + <xsd:enumeration value="decimal"/> |
| 240 | + <xsd:enumeration value="bar"/> |
| 241 | + <xsd:enumeration value="num"/> |
| 242 | + <xsd:enumeration value="left"/> |
| 243 | + <xsd:enumeration value="right"/> |
| 244 | + </xsd:restriction> |
| 245 | + </xsd:simpleType> |
| 246 | + |
| 247 | + <xsd:simpleType name="ST_TabTlc"> |
| 248 | + <xsd:restriction base="xsd:string"> |
| 249 | + <xsd:enumeration value="none"/> |
| 250 | + <xsd:enumeration value="dot"/> |
| 251 | + <xsd:enumeration value="hyphen"/> |
| 252 | + <xsd:enumeration value="underscore"/> |
| 253 | + <xsd:enumeration value="heavy"/> |
| 254 | + <xsd:enumeration value="middleDot"/> |
| 255 | + </xsd:restriction> |
| 256 | + </xsd:simpleType> |
0 commit comments