scanny / python-pptx

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

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

Open DuskBelievers opened 1 year ago

DuskBelievers commented 1 year ago

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

nikozhoufromchina commented 1 year ago

@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

DuskBelievers commented 1 year ago

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: @.***>

nikozhoufromchina commented 1 year ago

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

DuskBelievers commented 1 year ago

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: @.***>

sedrew commented 1 year ago

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

nikozhoufromchina commented 1 year ago

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

DuskBelievers commented 1 year ago

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

1687937577005

nikozhoufromchina commented 1 year ago

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.

DuskBelievers commented 1 year ago

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

DuskBelievers commented 1 year ago

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

Thank you for your help, strange friend.

DuskBelievers commented 1 year ago

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

Thank you for your help, strange friend.

Dasc3er commented 1 year ago

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

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)
DuskBelievers commented 1 year ago

@Dasc3er thanks,guys

astafan8 commented 7 months ago

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

DuskBelievers commented 7 months ago

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

astafan8 commented 7 months ago

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?

DuskBelievers commented 7 months ago

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