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

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

Open
DuskBelievers opened this issue Jun 14, 2023 · 17 comments
Open

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

DuskBelievers opened this issue Jun 14, 2023 · 17 comments
Labels

Comments

@DuskBelievers
Copy link

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

@nikozhoufromchina
Copy link

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

DuskBelievers commented Jun 28, 2023 via email

@nikozhoufromchina
Copy link

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

@DuskBelievers
Copy link
Author

DuskBelievers commented Jun 28, 2023 via email

@sedrew
Copy link

sedrew commented Jun 28, 2023

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

@nikozhoufromchina
Copy link

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

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

1687937577005

@nikozhoufromchina
Copy link

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

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

@DuskBelievers
Copy link
Author

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

Thank you for your help, strange friend.

1 similar comment
@DuskBelievers
Copy link
Author

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

Thank you for your help, strange friend.

@Dasc3er
Copy link

Dasc3er commented Jul 28, 2023

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

@Dasc3er thanks,guys

@astafan8
Copy link

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

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

@astafan8
Copy link

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

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

6 participants