-
Notifications
You must be signed in to change notification settings - Fork 546
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
Comments
@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 |
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 I plan to encapsulate this functionality using ChatGPT and share it on the platform. lol, let's look forward to it together! |
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: ***@***.***>
|
Can you take a look at the fork https://github.com/AndreasSteiner/python-pptx |
|
|
Thank you for your help, strange friend. |
1 similar comment
Thank you for your help, strange friend. |
Hi all, I am sharing some functions which work for my use cases.
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 thanks,guys |
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 |
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? |
i think so, https://pypi.org/project/python-pptx-valutico/ |
I want to add a new row to an existing table, is there a way to do that
The text was updated successfully, but these errors were encountered: