我使用下面的代码来保存一个表,修改这个表,然后制作一个表的副本。我从Here那里得到了copy_table_after()。
def copy_table_after(table, paragraph):
tbl, p = table._tbl, paragraph._p
new_tbl = deepcopy(tbl)
p.addnext(new_tbl)
def replaceText(document, search, replace):
for table in document.tables:
for row in table.rows:
for paragraph in row.cells:
if search in paragraph.text:
paragraph.text = replace
document = Document('Test.docx')
template = document.tables[0]
replaceText(document, '<<VALUE_TO_FIND>>', 'New value')
paragraph = document.add_paragraph()
copy_table_after(template, paragraph)我的问题是,当我运行copy_table_after时,它会用新文本复制表。有没有一种方法可以“保存”表,然后在我对其进行更改后复制原始表?
发布于 2018-02-10 06:26:58
是的,这应该是这样的:
(注意,我删除了copy_table_after,因为我们只想复制表)
def replaceText(document, search, replace):
for table in document.tables:
for row in table.rows:
for paragraph in row.cells:
if search in paragraph.text:
paragraph.text = replace
document = Document('Test.docx')
template = document.tables[0]
tbl = template._tbl
# Here we do the copy of the table
new_tbl = deepcopy(tbl)
# Then we do the replacement
replaceText(document, '<<VALUE_TO_FIND>>', 'New value')
paragraph = document.add_paragraph()
# After that, we add the previously copied table
paragraph._p.addnext(new_tbl)https://stackoverflow.com/questions/48713465
复制相似问题