Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Samuel-Muldoon opened this issue Aug 5, 2022 · 3 comments
Open

Comments

@Samuel-Muldoon
Copy link

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
Copy link

bowespublishing commented Aug 7, 2022

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
Copy link

ArturDH commented Sep 21, 2022

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

@Dasc3er
Copy link

Dasc3er commented Aug 31, 2023

Related: #895

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants