11

我正在尝试使用python-docx 模块替换文件中的单词并保存新文件,但需要注意的是新文件的格式必须与旧文件完全相同,但替换了单词。我该怎么做?

docx 模块有一个 savedocx,它接受 7 个输入:

  • 文档
  • 核心道具
  • 应用道具
  • 内容类型
  • 网络设置
  • 词语关系
  • 输出

除了替换的单词外,如何使原始文件中的所有内容保持不变?

4

8 回答 8

10

这对我有用:

def docx_replace(old_file,new_file,rep):
    zin = zipfile.ZipFile (old_file, 'r')
    zout = zipfile.ZipFile (new_file, 'w')
    for item in zin.infolist():
        buffer = zin.read(item.filename)
        if (item.filename == 'word/document.xml'):
            res = buffer.decode("utf-8")
            for r in rep:
                res = res.replace(r,rep[r])
            buffer = res.encode("utf-8")
        zout.writestr(item, buffer)
    zout.close()
    zin.close()
于 2015-05-31T09:53:26.820 回答
8

看起来,Python 的 Docx 并不意味着存储包含图像、标题等的完整 Docx,而仅包含文档的内部内容。所以没有简单的方法可以做到这一点。

但是,您可以这样做:

首先,看看docx 标签 wiki

它解释了如何解压缩 docx 文件:这是典型文件的样子:

+--docProps
|  +  app.xml
|  \  core.xml
+  res.log
+--word //this folder contains most of the files that control the content of the document
|  +  document.xml //Is the actual content of the document
|  +  endnotes.xml
|  +  fontTable.xml
|  +  footer1.xml //Containst the elements in the footer of the document
|  +  footnotes.xml
|  +--media //This folder contains all images embedded in the word
|  |  \  image1.jpeg
|  +  settings.xml
|  +  styles.xml
|  +  stylesWithEffects.xml
|  +--theme
|  |  \  theme1.xml
|  +  webSettings.xml
|  \--_rels
|     \  document.xml.rels //this document tells word where the images are situated
+  [Content_Types].xml
\--_rels
   \  .rels

Docx 只获取文档的一部分,在opendocx方法中

def opendocx(file):
    '''Open a docx file, return a document XML tree'''
    mydoc = zipfile.ZipFile(file)
    xmlcontent = mydoc.read('word/document.xml')
    document = etree.fromstring(xmlcontent)
    return document

它只获取 document.xml 文件。

我建议你做的是:

  1. 使用 **opendocx* 获取文档的内容
  2. 用advReplace方法替换 document.xml
  3. 以 zip 格式打开 docx,并将 document.xml 内容替换为新的 xml 内容。
  4. 关闭并输出压缩文件(将其重命名为 output.docx)

如果您安装了 node.js,请注意我已经在DocxGenJS上工作,它是 docx 文档的模板引擎,该库正在积极开发中,并将很快作为节点模块发布。

于 2013-07-25T15:46:46.627 回答
3

你在使用这里的 docx 模块

如果是,那么 docx 模块已经公开了诸如 replace、advReplace 等可以帮助您完成任务的方法。有关公开方法的更多详细信息,请参阅源代码

于 2013-07-25T06:37:16.377 回答
2
from docx import Document
file_path = 'C:/tmp.docx'
document = Document(file_path)

def docx_replace(doc_obj, data: dict):
    """example: data=dict(order_id=123), result: {order_id} -> 123"""
    for paragraph in doc_obj.paragraphs:
        for key, val in data.items():
            key_name = '{{{}}}'.format(key)
            if key_name in paragraph.text:
                paragraph.text = paragraph.text.replace(key_name, str(val))
    for table in doc_obj.tables:
        for row in table.rows:
            for cell in row.cells:
                docx_replace(cell, data)

docx_replace(document, dict(order_id=123, year=2018, payer_fio='payer_fio', payer_fio1='payer_fio1'))
document.save(file_path)
于 2018-09-12T04:39:29.613 回答
1

我在这里派生了一个 python-docx 的 repo ,它保留了 docx 文件中的所有预先存在的数据,包括格式。希望这是您正在寻找的。

于 2013-11-03T06:05:52.083 回答
1

除了@ramil,您必须先转义一些字符,然后才能将它们作为字符串值放入 XML,所以这对我有用:

def escape(escapee):
  escapee = escapee.replace("&", "&")
  escapee = escapee.replace("<", "&lt;")
  escapee = escapee.replace(">", "&gt;")
  escapee = escapee.replace("\"", "&quot;")
  escapee = escapee.replace("'", "&apos;")
return escapee
于 2016-09-09T07:39:01.277 回答
1

上述方法的问题是它们丢失了现有的格式。请参阅我的答案,它执行替换并保留格式。

还有python-docx-template一个允许在 docx 模板中使用 jinja2 样式的模板。这是文档的链接

于 2019-04-18T08:24:39.863 回答
0

我们可以使用 python-docx 将图像保存在 docx 上。docx 将图像检测为段落。但是对于这一段,文本是空的。所以你可以像这样使用。 paragraphs = document.paragraphs for paragraph in paragraphs: if paragraph.text == '': continue

于 2020-10-21T18:48:23.020 回答