|
| 1 | +# encoding: utf-8 |
| 2 | + |
| 3 | +""" |
| 4 | +Custom element classes for tables |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import absolute_import, print_function, unicode_literals |
| 8 | + |
| 9 | +from docx.oxml.shared import OxmlBaseElement, OxmlElement, qn |
| 10 | + |
| 11 | +from . import ValidationError |
| 12 | +from .text import CT_P |
| 13 | + |
| 14 | + |
| 15 | +class CT_Row(OxmlBaseElement): |
| 16 | + """ |
| 17 | + ``<w:tr>`` element |
| 18 | + """ |
| 19 | + def add_tc(self): |
| 20 | + """ |
| 21 | + Return a new <w:tc> element that has been added at the end of any |
| 22 | + existing tc elements. |
| 23 | + """ |
| 24 | + tc = CT_Tc.new() |
| 25 | + return self._append_tc(tc) |
| 26 | + |
| 27 | + def _append_tc(self, tc): |
| 28 | + """ |
| 29 | + Return *tc* after appending it to end of tc sequence. |
| 30 | + """ |
| 31 | + self.append(tc) |
| 32 | + return tc |
| 33 | + |
| 34 | + |
| 35 | +class CT_Tbl(OxmlBaseElement): |
| 36 | + """ |
| 37 | + ``<w:tbl>`` element |
| 38 | + """ |
| 39 | + @property |
| 40 | + def tblGrid(self): |
| 41 | + tblGrid = self.find(qn('w:tblGrid')) |
| 42 | + if tblGrid is None: |
| 43 | + raise ValidationError('required w:tblGrid child not found') |
| 44 | + return tblGrid |
| 45 | + |
| 46 | + @property |
| 47 | + def tr_lst(self): |
| 48 | + """ |
| 49 | + Sequence containing the ``<w:tr>`` child elements in this |
| 50 | + ``<w:tbl>``. |
| 51 | + """ |
| 52 | + return self.findall(qn('w:tr')) |
| 53 | + |
| 54 | + |
| 55 | +class CT_TblGrid(OxmlBaseElement): |
| 56 | + """ |
| 57 | + ``<w:tblGrid>`` element, child of ``<w:tbl>``, holds ``<w:gridCol>`` |
| 58 | + elements that define column count, width, etc. |
| 59 | + """ |
| 60 | + def add_gridCol(self): |
| 61 | + """ |
| 62 | + Return a new <w:gridCol> element that has been added at the end of |
| 63 | + any existing gridCol elements. |
| 64 | + """ |
| 65 | + gridCol = CT_TblGridCol.new() |
| 66 | + return self._append_gridCol(gridCol) |
| 67 | + |
| 68 | + def _append_gridCol(self, gridCol): |
| 69 | + """ |
| 70 | + Return *gridCol* after appending it to end of gridCol sequence. |
| 71 | + """ |
| 72 | + successor = self.first_child_found_in('w:tblGridChange') |
| 73 | + if successor is not None: |
| 74 | + successor.addprevious(gridCol) |
| 75 | + else: |
| 76 | + self.append(gridCol) |
| 77 | + return gridCol |
| 78 | + |
| 79 | + def first_child_found_in(self, *tagnames): |
| 80 | + """ |
| 81 | + Return the first child found with tag in *tagnames*, or None if |
| 82 | + not found. |
| 83 | + """ |
| 84 | + for tagname in tagnames: |
| 85 | + child = self.find(qn(tagname)) |
| 86 | + if child is not None: |
| 87 | + return child |
| 88 | + return None |
| 89 | + |
| 90 | + |
| 91 | +class CT_TblGridCol(OxmlBaseElement): |
| 92 | + """ |
| 93 | + ``<w:gridCol>`` element, child of ``<w:tblGrid>``, defines a table |
| 94 | + column. |
| 95 | + """ |
| 96 | + @classmethod |
| 97 | + def new(cls): |
| 98 | + """ |
| 99 | + Return a new ``<w:gridCol>`` element. |
| 100 | + """ |
| 101 | + return OxmlElement('w:gridCol') |
| 102 | + |
| 103 | + |
| 104 | +class CT_Tc(OxmlBaseElement): |
| 105 | + """ |
| 106 | + ``<w:tc>`` table cell element |
| 107 | + """ |
| 108 | + @classmethod |
| 109 | + def new(cls): |
| 110 | + """ |
| 111 | + Return a new ``<w:tc>`` element, containing an empty paragraph as the |
| 112 | + required EG_BlockLevelElt. |
| 113 | + """ |
| 114 | + tc = OxmlElement('w:tc') |
| 115 | + p = CT_P.new() |
| 116 | + tc.append(p) |
| 117 | + return tc |
0 commit comments