关于python docx包中,如何对Word自身表格实现复制,并且粘贴到原docx文档中?(已解决)

开发办公工具,利用docx包,但不知如何复制其中一个表格tables[0],并且实现粘贴。

Jason990420
最佳答案

Here, copy new table to end of original file.

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)


Update: Copy a table from one docx file to another one docx file.

from copy import deepcopy
from docx import Document

filename1 = 'D:/table1.docx'
filename2 = 'D:/table2.docx'

# Get table from file table1.docx
document1 = Document(filename1)
table = document1.tables[0]  # For 1st table

# Get document from file table2.docx
# Create a paragraph, then add new table from table1.docx
document2 = Document(filename2)

# deepcopy required to create a new table
for i in range(5):
    new_table = deepcopy(table)
    paragraph2 = document2.add_paragraph()
    paragraph2._p.addnext(new_table._element)

# Save document to table2.docx
document2.save(filename2)
3年前 评论
hkflyman (楼主) 3年前
HFwuhome 2年前
Jason990420 (作者) 2年前
讨论数量: 8
Jason990420

Maybe like this,

from docx import Document

input_filename = 'D:/file-sample_500kB.docx'
output_filename = 'D:/Sample.txt'
document = Document(input_filename)
table = document.tables[0]  # For 1st table

text = ''
for i, row in enumerate(table.rows):
    if i != 0:
        text += '\n'
    for j, cell in enumerate(row.cells):
        if j != 0:
            text += '\t'
        text += cell.text

with open(output_filename, 'wt', encoding='utf-8') as f:
    f.write(text)
3年前 评论
hkflyman (楼主) 3年前
hkflyman (楼主) 3年前
Jason990420

Here, copy new table to end of original file.

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)


Update: Copy a table from one docx file to another one docx file.

from copy import deepcopy
from docx import Document

filename1 = 'D:/table1.docx'
filename2 = 'D:/table2.docx'

# Get table from file table1.docx
document1 = Document(filename1)
table = document1.tables[0]  # For 1st table

# Get document from file table2.docx
# Create a paragraph, then add new table from table1.docx
document2 = Document(filename2)

# deepcopy required to create a new table
for i in range(5):
    new_table = deepcopy(table)
    paragraph2 = document2.add_paragraph()
    paragraph2._p.addnext(new_table._element)

# Save document to table2.docx
document2.save(filename2)
3年前 评论
hkflyman (楼主) 3年前
HFwuhome 2年前
Jason990420 (作者) 2年前

首先非常感谢您的帮助,根据您给出的代码我稍作了修改,但是我还是遇到了文件损坏的问题,但是它是可修复的。最终我还是不确定是我修改代码的问题,还是我word版本的问题,在这里只能贴上我的代码以及问题出现的截图,很抱歉不能贴上word的版本。

from copy import deepcopy
from docx import Document

filename1 = "D:\word\练习2.docx"
filename2 = "D:\word\练习1.docx"

document1 = Document(filename1)
document2 = Document(filename2)
表数 = len(document1.tables)
N=0
while N<表数:

    table = document1.tables[N]  # For 1st table
    new_table = deepcopy(table)

    paragraph2 = document2.add_paragraph()
    paragraph2._p.addnext(new_table._element)
    N+=1

document2.save("D:\word\练习4.docx")

file

2年前 评论

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