python-pptx icon indicating copy to clipboard operation
python-pptx copied to clipboard

Question: How would someone delete a specific row from a table?

Open Samuel-Muldoon opened this issue 3 years ago • 3 comments

Suppose that we have a table. How would we delete a row from it?

import pptx
from pptx import *

# Establish read path
in_file_path = "C:\\Users\\user\\Desktop\\power_point_pres.pptx"

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

# Get Table
slide = next(iter(pres.slides))
shp = next(iter(slide.shapes))
table = shp.table

After we have a reference to a table object, how do we delete a particular row?

Samuel-Muldoon avatar Aug 05 '22 17:08 Samuel-Muldoon

Something like this would suffice

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")

bowespublishing avatar Aug 07 '22 14:08 bowespublishing

Hi bowespublishing Can you provide, how we can do the same process for columns?

ArturDH avatar Sep 21 '22 07:09 ArturDH

Related: #895

Dasc3er avatar Aug 31 '23 19:08 Dasc3er