MCPcopy Create free account
hub / github.com/scanny/python-pptx / new_tbl

Method new_tbl

src/pptx/oxml/table.py:104–133  ·  view source on GitHub ↗

Return a new `p:tbl` element tree.

(
        cls, rows: int, cols: int, width: int, height: int, tableStyleId: str | None = None
    )

Source from the content-addressed store, hash-verified

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`."""

Calls 6

parse_xmlFunction · 0.90
EmuClass · 0.90
_tbl_tmplMethod · 0.80
add_gridColMethod · 0.80
add_trMethod · 0.80
add_tcMethod · 0.80

Tested by 1