Return a new `p:tbl` element tree.
(
cls, rows: int, cols: int, width: int, height: int, tableStyleId: str | None = None
)
| 102 | |
| 103 | @classmethod |
| 104 | def new_tbl( |
| 105 | cls, rows: int, cols: int, width: int, height: int, tableStyleId: str | None = None |
| 106 | ) -> CT_Table: |
| 107 | """Return a new `p:tbl` element tree.""" |
| 108 | # working hypothesis is this is the default table style GUID |
| 109 | if tableStyleId is None: |
| 110 | tableStyleId = "{5C22544A-7EE6-4342-B048-85BDC9FD1C3A}" |
| 111 | |
| 112 | xml = cls._tbl_tmpl() % (tableStyleId) |
| 113 | tbl = cast(CT_Table, parse_xml(xml)) |
| 114 | |
| 115 | # add specified number of rows and columns |
| 116 | rowheight = height // rows |
| 117 | colwidth = width // cols |
| 118 | |
| 119 | for col in range(cols): |
| 120 | # adjust width of last col to absorb any div error |
| 121 | if col == cols - 1: |
| 122 | colwidth = width - ((cols - 1) * colwidth) |
| 123 | tbl.tblGrid.add_gridCol(width=Emu(colwidth)) |
| 124 | |
| 125 | for row in range(rows): |
| 126 | # adjust height of last row to absorb any div error |
| 127 | if row == rows - 1: |
| 128 | rowheight = height - ((rows - 1) * rowheight) |
| 129 | tr = tbl.add_tr(height=Emu(rowheight)) |
| 130 | for col in range(cols): |
| 131 | tr.add_tc() |
| 132 | |
| 133 | return tbl |
| 134 | |
| 135 | def tc(self, row_idx: int, col_idx: int) -> CT_TableCell: |
| 136 | """Return `a:tc` element at `row_idx`, `col_idx`.""" |