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

Is there a way to add rows or columns to the existing table

Open DuskBelievers opened this issue 1 year ago • 17 comments

I want to add a new row to an existing table, is there a way to do that

DuskBelievers avatar Jun 14 '23 02:06 DuskBelievers

@DuskBelievers I have encountered the same problem, and it seems that this feature is not currently encapsulated. But I can share an idea, which is to encapsulate a table processing object. After obtaining the table object, it is passed in to this table processing object. Two modules are set up in the table object. One is to record various parameters of the original table, such as coordinates, height, width, number of columns, and data in the table. Then, create a new table, add rows and columns, pass in various parameters, and output them to PPT. It is theoretically feasible. You can give it a try. If you have any questions, please feel free to communicate together

nikozhoufromchina avatar Jun 28 '23 02:06 nikozhoufromchina

Thank you for your reply. I am doing the same at present, but it is a little troublesome. I hope that the official will encapsulate this function in the future. Thank you again, stranger!  

    末屿 @.***

 

------------------ 原始邮件 ------------------ 发件人: @.>; 发送时间: 2023年6月28日(星期三) 上午10:33 收件人: @.>; 抄送: " @.>; @.>; 主题: Re: [scanny/python-pptx] Is there a way to add rows or columns to the existing table (Issue #895)

@DuskBelievers I have encountered the same problem, and it seems that this feature is not currently encapsulated. But I can share an idea, which is to encapsulate a table processing object. After obtaining the table object, it is passed in to this table processing object. Two modules are set up in the table object. One is to record various parameters of the original table, such as coordinates, height, width, number of columns, and data in the table. Then, create a new table, add rows and columns, pass in various parameters, and output them to PPT. It is theoretically feasible. You can give it a try. If you have any questions, please feel free to communicate together

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>

DuskBelievers avatar Jun 28 '23 02:06 DuskBelievers

@DuskBelievers I plan to encapsulate this functionality using ChatGPT and share it on the platform. lol, let's look forward to it together!

nikozhoufromchina avatar Jun 28 '23 03:06 nikozhoufromchina

It's a good idea. I'm looking forward to it  

    末屿 @.***

 

------------------ 原始邮件 ------------------ 发件人: @.>; 发送时间: 2023年6月28日(星期三) 中午11:00 收件人: @.>; 抄送: " @.>; @.>; 主题: Re: [scanny/python-pptx] Is there a way to add rows or columns to the existing table (Issue #895)

@DuskBelievers I plan to encapsulate this functionality using ChatGPT and share it on the platform. lol, let's look forward to it together!

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>

DuskBelievers avatar Jun 28 '23 05:06 DuskBelievers

Can you take a look at the fork https://github.com/AndreasSteiner/python-pptx

sedrew avatar Jun 28 '23 06:06 sedrew

Can you take a look at the fork https://github.com/AndreasSteiner/python-pptx Thank you for sedrew's response. It turns out that this issue has already been resolved very well. I just tested it, and it works great. Once again, thank you sedrew!!! image

nikozhoufromchina avatar Jun 28 '23 07:06 nikozhoufromchina

I also noticed the "add" method before, but I kept getting errors when I tested it.

1687937577005

DuskBelievers avatar Jun 28 '23 07:06 DuskBelievers

image As sedrew mentioned earlier, please use "pip install python-pptx-valutico" to install it. After installation, you should no longer encounter any errors. You can give it a try.

nikozhoufromchina avatar Jun 28 '23 07:06 nikozhoufromchina

That's true. That problem is solved. I really appreciate your help.

DuskBelievers avatar Jun 28 '23 08:06 DuskBelievers

Can you take a look at the fork https://github.com/AndreasSteiner/python-pptx

Thank you for your help, strange friend.

DuskBelievers avatar Jun 28 '23 08:06 DuskBelievers

Can you take a look at the fork https://github.com/AndreasSteiner/python-pptx

Thank you for your help, strange friend.

DuskBelievers avatar Jun 28 '23 08:06 DuskBelievers

Hi all, I am sharing some functions which work for my use cases. This aggregates the solution of multiple threads:

  • https://github.com/scanny/python-pptx/issues/192
  • https://github.com/scanny/python-pptx/issues/86
  • https://stackoverflow.com/questions/57866183/is-there-a-way-to-add-a-column-to-a-table-in-powerpoint-slide-through-python
  • https://stackoverflow.com/questions/64591452/using-copy-deepcopy-with-python-pptx-to-add-a-column-to-a-table-leads-to-cell-at

Reference GIST: https://gist.github.com/Dasc3er/2af5069afb728c39d54434cb28a1dbb8

table = shape.table

def add_column(table):
    """
    Duplicates the last column of the table and appends it to the end.
    """
    import copy
    from pptx.table import _Cell, _Column

    new_col = copy.deepcopy(table._tbl.tblGrid.gridCol_lst[-1])
    table._tbl.tblGrid.append(new_col)  # copies last grid element

    for tr in table._tbl.tr_lst:
        # duplicate last cell of each row
        new_tc = copy.deepcopy(tr.tc_lst[-1])

        # Fix for column styling
        last_tc = tr.xpath(".//a:tc")[-1]
        parent = last_tc.getparent()
        parent.insert(
            parent.index(last_tc) + 1,
            new_tc
        )

        # Clear new cell content
        cell = _Cell(new_tc, tr.tc_lst)
        cell.text_frame.clear()

    # Fix column not writable
    # https://stackoverflow.com/questions/64591452/using-copy-deepcopy-with-python-pptx-to-add-a-column-to-a-table-leads-to-cell-at
    from pptx import oxml
    for child in table._tbl.getchildren():
        if isinstance(child, oxml.table.CT_TableGrid):
            ws = set()
            for j in child:
                if j.w not in ws:
                    ws.add(j.w)
                else:
                    for elem in j:
                        j.remove(elem)

    # Create object in memory, in case some operations are done by the library
    col = _Column(new_col, table)

def remove_column(table, column_index: int):
    """
    Removes a specified column from the table.
    """
    column = list(table.columns)[column_index]

    col_idx = table._tbl.tblGrid.index(column._gridCol)

    for tr in table._tbl.tr_lst:
        tr.remove(tr.tc_lst[col_idx])

    table._tbl.tblGrid.remove(column._gridCol)

def add_row(table) -> None:
    """
    Duplicates the last row and appends it to the end.
    """
    import copy
    from pptx.table import _Cell, _Row
    from random import randrange

    new_row = copy.deepcopy(table._tbl.tr_lst[-1])  # copies last row element

    for tc in new_row.tc_lst:
        cell = _Cell(tc, new_row.tc_lst)
        cell.text = ''

    table._tbl.append(new_row)
    row = _Row(new_row, table)

    # Fix row not writable
    reference = row._tr.xpath(".//a:ext")[0]
    reference.getchildren()[0].set("val", str(randrange(10 ** 5, 10 ** 9)))

def remove_row(table, row_index: int) -> None:
    """
    Remove a specified row from the table.

    :return:
    """
    row = list(table.rows)[row_index]

    table._tbl.remove(row._tr)

Dasc3er avatar Jul 28 '23 07:07 Dasc3er

@Dasc3er thanks,guys

DuskBelievers avatar Jul 31 '23 03:07 DuskBelievers

Hi! Are there any news on this feature? Does this ability exist in the python-pptx original package and using public API (without using attrbiutes and methods that start with underscores)?

astafan8 avatar Feb 19 '24 13:02 astafan8

@astafan8 first,pip install python-pptx-valutico and then image

DuskBelievers avatar Feb 22 '24 08:02 DuskBelievers

thank you ! and is python-pptx-valutico a package that will be kept alive by it's authors and will used instead of python-pptx by all?

astafan8 avatar Feb 22 '24 12:02 astafan8

i think so, https://pypi.org/project/python-pptx-valutico/

DuskBelievers avatar Feb 23 '24 01:02 DuskBelievers