scanny / python-pptx

Create Open XML PowerPoint documents in Python
MIT License
2.4k stars 520 forks source link

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

Open Samuel-Muldoon opened 2 years ago

Samuel-Muldoon commented 2 years ago

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?

bowespublishing commented 2 years ago

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")
ArturDH commented 2 years ago

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

Dasc3er commented 1 year ago

Related: #895