Skip to content

Commit 8073bf7

Browse files
author
Steve Canny
committed
oxml: use templating for CT_Tbl.new()
1 parent 752af0d commit 8073bf7

File tree

4 files changed

+76
-71
lines changed

4 files changed

+76
-71
lines changed

docs/dev/analysis/features/table.rst

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Table
33
=====
44

5+
... column width is stored in twips (20ths of a point) ...
56

67
MS API
78
------
@@ -104,10 +105,10 @@ Schema Definitions
104105

105106
<xsd:complexType name="CT_Tbl">
106107
<xsd:sequence>
107-
<xsd:group ref="EG_RangeMarkupElements" minOccurs="0" maxOccurs="unbounded"/>
108-
<xsd:element name="tblPr" type="CT_TblPr"/>
109-
<xsd:element name="tblGrid" type="CT_TblGrid"/>
110-
<xsd:group ref="EG_ContentRowContent" minOccurs="0" maxOccurs="unbounded"/>
108+
<xsd:group ref="EG_RangeMarkupElements" minOccurs="0" maxOccurs="unbounded"/>
109+
<xsd:element name="tblPr" type="CT_TblPr"/>
110+
<xsd:element name="tblGrid" type="CT_TblGrid"/>
111+
<xsd:group ref="EG_ContentRowContent" minOccurs="0" maxOccurs="unbounded"/>
111112
</xsd:sequence>
112113
</xsd:complexType>
113114

@@ -138,6 +139,39 @@ Schema Definitions
138139
<xsd:attribute name="type" type="ST_TblWidth"/>
139140
</xsd:complexType>
140141

142+
<xsd:simpleType name="ST_MeasurementOrPercent">
143+
<xsd:union memberTypes="ST_DecimalNumberOrPercent s:ST_UniversalMeasure"/>
144+
</xsd:simpleType>
145+
146+
<xsd:simpleType name="ST_DecimalNumberOrPercent">
147+
<xsd:union memberTypes="ST_UnqualifiedPercentage s:ST_Percentage"/>
148+
</xsd:simpleType>
149+
150+
<xsd:simpleType name="ST_UniversalMeasure">
151+
<xsd:restriction base="xsd:string">
152+
<xsd:pattern value="-?[0-9]+(\.[0-9]+)?(mm|cm|in|pt|pc|pi)"/>
153+
</xsd:restriction>
154+
</xsd:simpleType>
155+
156+
<xsd:simpleType name="ST_UnqualifiedPercentage">
157+
<xsd:restriction base="xsd:integer"/>
158+
</xsd:simpleType>
159+
160+
<xsd:simpleType name="ST_Percentage">
161+
<xsd:restriction base="xsd:string">
162+
<xsd:pattern value="-?[0-9]+(\.[0-9]+)?%"/>
163+
</xsd:restriction>
164+
</xsd:simpleType>
165+
166+
<xsd:simpleType name="ST_TblWidth">
167+
<xsd:restriction base="xsd:string">
168+
<xsd:enumeration value="nil"/>
169+
<xsd:enumeration value="pct"/>
170+
<xsd:enumeration value="dxa"/>
171+
<xsd:enumeration value="auto"/>
172+
</xsd:restriction>
173+
</xsd:simpleType>
174+
141175
<xsd:complexType name="CT_TblLook">
142176
<xsd:attribute name="firstRow" type="s:ST_OnOff"/>
143177
<xsd:attribute name="lastRow" type="s:ST_OnOff"/>
@@ -159,6 +193,10 @@ Schema Definitions
159193
<xsd:attribute name="w" type="s:ST_TwipsMeasure"/>
160194
</xsd:complexType>
161195

196+
<xsd:simpleType name="ST_TwipsMeasure">
197+
<xsd:union memberTypes="ST_UnsignedDecimalNumber ST_PositiveUniversalMeasure"/>
198+
</xsd:simpleType>
199+
162200
<xsd:group name="EG_ContentRowContent">
163201
<xsd:choice>
164202
<xsd:element name="tr" type="CT_Row" minOccurs="0" maxOccurs="unbounded"/>
@@ -241,48 +279,6 @@ Schema Definitions
241279
</xsd:sequence>
242280
</xsd:complexType>
243281

244-
::
245-
246-
w_CT_Tc =
247-
attribute w:id { s_ST_String }?,
248-
element tcPr { w_CT_TcPr }?,
249-
w_EG_BlockLevelElts+
250-
251-
w_EG_BlockLevelElts = # denormalized
252-
element customXml { w_CT_CustomXmlBlock }
253-
| element p { w_CT_P }
254-
| element sdt { w_CT_SdtBlock }
255-
| element tbl { w_CT_Tbl }
256-
| element altChunk { w_CT_AltChunk }
257-
258-
| element proofErr { w_CT_ProofErr }
259-
| element permStart { w_CT_PermStart }
260-
| element permEnd { w_CT_Perm }
261-
| element ins { w_CT_RunTrackChange }
262-
| element del { w_CT_RunTrackChange }
263-
| element moveFrom { w_CT_RunTrackChange }
264-
| element moveTo { w_CT_RunTrackChange }
265-
266-
| element bookmarkStart { w_CT_Bookmark }
267-
| element bookmarkEnd { w_CT_MarkupRange }
268-
| element moveFromRangeStart { w_CT_MoveBookmark }
269-
| element moveFromRangeEnd { w_CT_MarkupRange }
270-
| element moveToRangeStart { w_CT_MoveBookmark }
271-
| element moveToRangeEnd { w_CT_MarkupRange }
272-
| element commentRangeStart { w_CT_MarkupRange }
273-
| element commentRangeEnd { w_CT_MarkupRange }
274-
| element customXmlInsRangeStart { w_CT_TrackChange }
275-
| element customXmlInsRangeEnd { w_CT_Markup }
276-
| element customXmlDelRangeStart { w_CT_TrackChange }
277-
| element customXmlDelRangeEnd { w_CT_Markup }
278-
| element customXmlMoveFromRangeStart { w_CT_TrackChange }
279-
| element customXmlMoveFromRangeEnd { w_CT_Markup }
280-
| element customXmlMoveToRangeStart { w_CT_TrackChange }
281-
| element customXmlMoveToRangeEnd { w_CT_Markup }
282-
283-
| element oMathPara { m_CT_OMathPara }
284-
| element oMath { m_CT_OMath }
285-
286282

287283
Resources
288284
---------

docx/oxml/table.py

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
from __future__ import absolute_import, print_function, unicode_literals
88

9-
from . import OxmlElement
9+
from . import parse_xml
10+
from .ns import nsdecls
1011
from .xmlchemy import (
1112
BaseOxmlElement, OneAndOnlyOne, OneOrMore, ZeroOrOne, ZeroOrMore
1213
)
@@ -36,13 +37,20 @@ def new(cls):
3637
Return a new ``<w:tbl>`` element, containing the required
3738
``<w:tblPr>`` and ``<w:tblGrid>`` child elements.
3839
"""
39-
tbl = OxmlElement('w:tbl')
40-
tblPr = CT_TblPr.new()
41-
tbl.append(tblPr)
42-
tblGrid = CT_TblGrid.new()
43-
tbl.append(tblGrid)
40+
tbl = parse_xml(cls._tbl_xml())
4441
return tbl
4542

43+
@classmethod
44+
def _tbl_xml(cls):
45+
return (
46+
'<w:tbl %s>\n'
47+
' <w:tblPr>\n'
48+
' <w:tblW w:type="auto" w:w="0"/>\n'
49+
' </w:tblPr>\n'
50+
' <w:tblGrid/>\n'
51+
'</w:tbl>' % nsdecls('w')
52+
)
53+
4654

4755
class CT_TblGrid(BaseOxmlElement):
4856
"""
@@ -51,13 +59,6 @@ class CT_TblGrid(BaseOxmlElement):
5159
"""
5260
gridCol = ZeroOrMore('w:gridCol', successors=('w:tblGridChange',))
5361

54-
@classmethod
55-
def new(cls):
56-
"""
57-
Return a new ``<w:tblGrid>`` element.
58-
"""
59-
return OxmlElement('w:tblGrid')
60-
6162

6263
class CT_TblGridCol(BaseOxmlElement):
6364
"""
@@ -80,13 +81,6 @@ def add_tblStyle(self, style_name):
8081
"""
8182
return self._add_tblStyle(val=style_name)
8283

83-
@classmethod
84-
def new(cls):
85-
"""
86-
Return a new ``<w:tblPr>`` element.
87-
"""
88-
return OxmlElement('w:tblPr')
89-
9084

9185
class CT_Tc(BaseOxmlElement):
9286
"""
@@ -124,6 +118,8 @@ def new(cls):
124118
Return a new ``<w:tc>`` element, containing an empty paragraph as the
125119
required EG_BlockLevelElt.
126120
"""
127-
tc = OxmlElement('w:tc')
128-
tc._add_p()
129-
return tc
121+
return parse_xml(
122+
'<w:tc %s>\n'
123+
' <w:p/>\n'
124+
'</w:tc>' % nsdecls('w')
125+
)

tests/oxml/unitdata/table.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ class CT_TblPrBuilder(BaseBuilder):
3838
__attrs__ = ()
3939

4040

41+
class CT_TblWidthBuilder(BaseBuilder):
42+
__tag__ = 'w:tblW'
43+
__nspfxs__ = ('w',)
44+
__attrs__ = ('w:w', 'w:type')
45+
46+
4147
class CT_TcBuilder(BaseBuilder):
4248
__tag__ = 'w:tc'
4349
__nspfxs__ = ('w',)
@@ -70,6 +76,10 @@ def a_tblStyle():
7076
return CT_StringBuilder('w:tblStyle')
7177

7278

79+
def a_tblW():
80+
return CT_TblWidthBuilder()
81+
82+
7383
def a_tc():
7484
return CT_TcBuilder()
7585

tests/parts/test_document.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from ..oxml.unitdata.dml import a_drawing, an_inline
2626
from ..oxml.parts.unitdata.document import a_body, a_document
2727
from ..oxml.unitdata.table import (
28-
a_gridCol, a_tbl, a_tblGrid, a_tblPr, a_tc, a_tr
28+
a_gridCol, a_tbl, a_tblGrid, a_tblPr, a_tblW, a_tc, a_tr
2929
)
3030
from ..oxml.unitdata.text import a_p, a_sectPr, an_r
3131
from ..unitutil import (
@@ -437,7 +437,10 @@ def _body_bldr(self, p_count=0, tbl_bldr=None, sectPr=False):
437437
return body_bldr
438438

439439
def _tbl_bldr(self, rows=1, cols=1):
440-
tblPr_bldr = a_tblPr()
440+
tblPr_bldr = (
441+
a_tblPr().with_child(
442+
a_tblW().with_type("auto").with_w(0))
443+
)
441444

442445
tblGrid_bldr = a_tblGrid()
443446
for i in range(cols):

0 commit comments

Comments
 (0)