Python:将内容从一个Word文档复制到另一个Word文档并保持格式? [英] Python: Copy content from one word document to another word document and keeping format?

查看:6007
本文介绍了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')

这篇关于Python:将内容从一个Word文档复制到另一个Word文档并保持格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆