Python:将内容从一个Word文档复制到另一个Word文档并保持格式?

Python:将内容从一个Word文档复制到另一个Word文档并保持格式?

问题描述:

正如标题所述,我想知道是否有任何模块可以让我通过python将内容从一个Microsoft Word文档解析到另一个文档并保持格式.

As the title says I would like to know if there is any module that will allow me to parse content from one Microsoft word document to another via python and keeping the format.

我想读取表数据并将其传输到另一个文档中的另一个表中.

I want to read table data and transfer it to another table in another document.

文档A和B都存在.我只希望能够浏览两个文档中的单元格(不一定同时)并复制内容,而不必担心文本是格式化的(字体,斜体,粗体)还是包含项目符号.

Both doc A and B exist. I just want to be able to walk through the cells in both docs (not necessarily at the same time) and copy content without having to worry about if the text is formatted (font, italic, bold) or contains bullets.

我要使用python,因为它是我最喜欢的语言...

I'm asking for python since it's my favorite language...

遵循Kasra建议使用python-docx:

Following Kasra advice to use python-docx :

粗糙的示例代码.

查询表格文档:

from docx import *

document = opendocx('xxxzzz.docx')
table = document.xpath('/w:document/w:body/w:tbl', namespaces=nsprefixes)[0]

写到另一个文档:

output = opendocx('yyywwww.docx')
body = output.xpath('/w:document/w:body', namespaces=nsprefixes)[0]

body.append(table)

output.save('new-file-name.docx')