如何实现本docx内表格复制粘贴至原docx指定段落?(已解决)

文档docx内有一个表格,若干段落。先需将表格复制粘贴到原docx文档的段落2处。

(表格复制粘贴功能已经实现,但只是粘贴到最后段落处)
from copy import deepcopy
from docx import Document

filename = ‘D:/file-sample_500kB.docx’

document = Document(filename)
table = document.tables[0] # For 1st table
new_table = deepcopy(table)
paragraph = document.add_paragraph()
paragraph._p.addnext(new_table._element)
document.save(filename)

Jason990420
最佳答案
new_paragraph = paragraph.insert_paragraph_before()
print(“aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa”)

new_paragraph._p.addnext(new_table._element)
3年前 评论
讨论数量: 3
Jason990420

python-docx 0.8.10 documentation

所有的段落

document.paragraphs

A list of |Paragraph| instances corresponding to the paragraphs in the document, in document order. Note that paragraphs within revision marks such as <w:ins> or <w:del> do not appear in this list.

插入在段落之前

paragraph.insert_paragraph_before(text=None, style=None)

Return a newly created paragraph, inserted directly before this paragraph. If text is supplied, the new paragraph contains that text in a single run. If style is provided, that style is assigned to the new paragraph.

3年前 评论
hkflyman (楼主) 3年前
Jason990420
new_paragraph = paragraph.insert_paragraph_before()
print(“aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa”)

new_paragraph._p.addnext(new_table._element)
3年前 评论

谢谢老师!

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!