|
8 | 8 |
|
9 | 9 | from . import parse_xml |
10 | 10 | from .ns import nsdecls |
11 | | -from .simpletypes import ST_TblLayoutType, ST_TwipsMeasure |
| 11 | +from ..shared import Emu, Twips |
| 12 | +from .simpletypes import ( |
| 13 | + ST_TblLayoutType, ST_TblWidth, ST_TwipsMeasure, XsdInt |
| 14 | +) |
12 | 15 | from .xmlchemy import ( |
13 | | - BaseOxmlElement, OneAndOnlyOne, OneOrMore, OptionalAttribute, ZeroOrOne, |
14 | | - ZeroOrMore |
| 16 | + BaseOxmlElement, OneAndOnlyOne, OneOrMore, OptionalAttribute, |
| 17 | + RequiredAttribute, ZeroOrOne, ZeroOrMore |
15 | 18 | ) |
16 | 19 |
|
17 | 20 |
|
@@ -129,6 +132,33 @@ def style(self, value): |
129 | 132 | self._add_tblStyle(val=value) |
130 | 133 |
|
131 | 134 |
|
| 135 | +class CT_TblWidth(BaseOxmlElement): |
| 136 | + """ |
| 137 | + Used for ``<w:tblW>`` and ``<w:tcW>`` elements and many others, to |
| 138 | + specify a table-related width. |
| 139 | + """ |
| 140 | + # the type for `w` attr is actually ST_MeasurementOrPercent, but using |
| 141 | + # XsdInt for now because only dxa (twips) values are being used. It's not |
| 142 | + # entirely clear what the semantics are for other values like -01.4mm |
| 143 | + w = RequiredAttribute('w:w', XsdInt) |
| 144 | + type = RequiredAttribute('w:type', ST_TblWidth) |
| 145 | + |
| 146 | + @property |
| 147 | + def width(self): |
| 148 | + """ |
| 149 | + Return the EMU length value represented by the combined ``w:w`` and |
| 150 | + ``w:type`` attributes. |
| 151 | + """ |
| 152 | + if self.type != 'dxa': |
| 153 | + return None |
| 154 | + return Twips(self.w) |
| 155 | + |
| 156 | + @width.setter |
| 157 | + def width(self, value): |
| 158 | + self.type = 'dxa' |
| 159 | + self.w = Emu(value).twips |
| 160 | + |
| 161 | + |
132 | 162 | class CT_Tc(BaseOxmlElement): |
133 | 163 | """ |
134 | 164 | ``<w:tc>`` table cell element |
@@ -170,3 +200,42 @@ def new(cls): |
170 | 200 | ' <w:p/>\n' |
171 | 201 | '</w:tc>' % nsdecls('w') |
172 | 202 | ) |
| 203 | + |
| 204 | + @property |
| 205 | + def width(self): |
| 206 | + """ |
| 207 | + Return the EMU length value represented in the ``./w:tcPr/w:tcW`` |
| 208 | + child element or |None| if not present. |
| 209 | + """ |
| 210 | + tcPr = self.tcPr |
| 211 | + if tcPr is None: |
| 212 | + return None |
| 213 | + return tcPr.width |
| 214 | + |
| 215 | + |
| 216 | +class CT_TcPr(BaseOxmlElement): |
| 217 | + """ |
| 218 | + ``<w:tcPr>`` element, defining table cell properties |
| 219 | + """ |
| 220 | + tcW = ZeroOrOne('w:tcW', successors=( |
| 221 | + 'w:gridSpan', 'w:hMerge', 'w:vMerge', 'w:tcBorders', 'w:shd', |
| 222 | + 'w:noWrap', 'w:tcMar', 'w:textDirection', 'w:tcFitText', 'w:vAlign', |
| 223 | + 'w:hideMark', 'w:headers', 'w:cellIns', 'w:cellDel', 'w:cellMerge', |
| 224 | + 'w:tcPrChange' |
| 225 | + )) |
| 226 | + |
| 227 | + @property |
| 228 | + def width(self): |
| 229 | + """ |
| 230 | + Return the EMU length value represented in the ``<w:tcW>`` child |
| 231 | + element or |None| if not present or its type is not 'dxa'. |
| 232 | + """ |
| 233 | + tcW = self.tcW |
| 234 | + if tcW is None: |
| 235 | + return None |
| 236 | + return tcW.width |
| 237 | + |
| 238 | + @width.setter |
| 239 | + def width(self, value): |
| 240 | + tcW = self.get_or_add_tcW() |
| 241 | + tcW.width = value |
0 commit comments