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

Addition or Deletion of rows and columns in an existing table #192

Closed
anm-ranjan opened this issue Jan 21, 2016 · 6 comments
Closed

Addition or Deletion of rows and columns in an existing table #192

anm-ranjan opened this issue Jan 21, 2016 · 6 comments
Labels

Comments

@anm-ranjan
Copy link

Is there anyway to modify the number of rows and columns in an existing table present in a .pptx file?

Since I cannot edit the table style using the existing Python-PPTX module, I was trying to insert a table with a given style in a custom layout. I could just insert a new slide based on this layout and if only I could modify the number of rows and columns of the table in the newly created slide, my job would be done.

@smcpherson
Copy link

+1 on that - would be very helpful.

@scanny scanny added the table label Aug 18, 2016
@thoramgowtham
Copy link

Does this functionality added to the module?, I mean Adding or deleting a row from existing table. If yes how to do that?

@scanny
Copy link
Owner

scanny commented Sep 23, 2018

Duplicate of #86.

@scanny scanny closed this as completed Sep 23, 2018
@zhar97
Copy link

zhar97 commented Aug 9, 2021

Simply reiterating the solution idea in #86 that is applicable to this issue directly with the older main version of python-pptx.

from copy import deepcopy
from pptx.table import Table, _Row, _Column, _Cell

def add_row(table: Table) -> _Row:
    new_row = deepcopy(table._tbl.tr_lst[-1]) 
    # duplicating last row of the table as a new row to be added

    for tc in new_row.tc_lst:
        cell = _Cell(tc, new_row.tc_lst)
        cell.text = '' # defaulting cell contents to empty text

    table._tbl.append(new_row) 
    return table.rows[-1]

def remove_row(table: Table,
               row_to_delete: _Row) -> None:
    table._tbl.remove(row_to_delete._tr)

The current version may have the methods implemented for pptx.table._RowCollection object.
Original solution is provided by @Ignisor

@VaishnaviDaware
Copy link

Can we delete row from existing table from pptx developed by Python-Pptx.

Screenshot 2022-02-11 150725

I want to delete last 7th row from table. Is there anyway to do it?

And if we have use below function could you please provide any example of code for the same.

def remove_row(table: Table,
row_to_delete: _Row) -> None:
table._tbl.remove(row_to_delete._tr)

OR

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

row = table.rows[n]
remove_row(table, row)

Thank you

@astafan8
Copy link

Good that there are solutions using private attributes (which names start with underscore) - but can't there be a public API that supports this? Or perhaps since 2022 there is already?

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

No branches or pull requests

7 participants