Luke Bowes
Luke Bowes

Reputation: 372

How To Delete A Specific Table Row In PowerPoint Using Python-PPTX

How can we delete specific rows in a PowerPoint Presentation table using Python-PPTX? It is possible to loop through each row/column and cell but there doesn't appear to be a way to delete a specific row?

Upvotes: 0

Views: 1783

Answers (1)

Luke Bowes
Luke Bowes

Reputation: 372

There is no 'built in' way to do this but by editing the underlying XML we can get the result we want.

import pptx
from pptx import *

def remove_row(table, row):
    tbl = table._tbl
    tr = row._tr
    tbl.remove(tr)

# Establish read path
in_file_path = "input.pptx"

# Open slide-show presentation
pres = Presentation(in_file_path)

# Get Table
for slide in pres.slides:
    for shp in slide.shapes:
        if shp.has_table:
            table = shp.table
                
            row = table.rows[7]
            remove_row(table, row)

pres.save("output.pptx")

Upvotes: 1

Related Questions