Skip to content

Commit 3bac1e4

Browse files
committed
Add the column and row collection add methods
1 parent d043334 commit 3bac1e4

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

pptx/table.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from pptx.text.text import TextFrame
1010
from pptx.util import lazyproperty
1111

12+
import copy
1213

1314
class Table(object):
1415
"""A DrawingML table object.
@@ -491,6 +492,36 @@ def notify_width_changed(self):
491492
"""
492493
self._parent.notify_width_changed()
493494

495+
def add(self):
496+
"""
497+
Duplicates last column to keep formatting and resets it's cells text_frames
498+
(e.g. ``column = table.columns.add_column()``).
499+
Returns new |_Column| instance.
500+
"""
501+
new_col = copy.deepcopy(self._tbl.tblGrid.gridCol_lst[-1])
502+
self._tbl.tblGrid.append(new_col) # copies last grid element
503+
504+
for tr in self._tbl.tr_lst:
505+
# duplicate last cell of each row
506+
new_tc = copy.deepcopy(tr.tc_lst[-1])
507+
tr.append(new_tc)
508+
509+
cell = _Cell(new_tc, tr.tc_lst)
510+
cell.text = ''
511+
512+
return _Column(new_col, self)
513+
514+
def remove(self, column):
515+
"""
516+
Removes specified *column* (e.g. ``table.columns.remove(table.columns[0])``).
517+
"""
518+
col_idx = self._tbl.tblGrid.index(column._gridCol)
519+
520+
for tr in self._tbl.tr_lst:
521+
tr.remove(tr.tc_lst[col_idx])
522+
523+
self._tbl.tblGrid.remove(column._gridCol)
524+
494525

495526
class _RowCollection(Subshape):
496527
"""Sequence of table rows"""
@@ -519,3 +550,26 @@ def notify_height_changed(self):
519550
Called by a row when its height changes. Pass along to parent.
520551
"""
521552
self._parent.notify_height_changed()
553+
554+
555+
def add(self):
556+
"""
557+
Duplicates last row to keep formatting and resets it's cells text_frames
558+
(e.g. ``row = table.rows.add_row()``).
559+
Returns new |_Row| instance.
560+
"""
561+
new_row = copy.deepcopy(self._tbl.tr_lst[-1]) # copies last row element
562+
563+
for tc in new_row.tc_lst:
564+
cell = _Cell(tc, new_row.tc_lst)
565+
cell.text = ''
566+
567+
self._tbl.append(new_row)
568+
569+
return _Row(new_row, self)
570+
571+
def remove(self, row):
572+
"""
573+
Removes specified *row* (e.g. ``table.rows.remove(table.rows[0])``).
574+
"""
575+
self._tbl.remove(row._tr)

0 commit comments

Comments
 (0)