在docx中替换文本并用python docx保存更改的文件

2024-06-16 10:31:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用python-docx module替换文件中的一个单词,并保存新文件,但要注意新文件的格式必须与旧文件的格式完全相同,但要替换该单词。我该怎么做?

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

  • 文件
  • 岩芯支柱
  • 应用程序
  • 内容类型
  • 网络设置
  • 文字关系
  • 输出

除了替换的单词外,我如何保持原始文件中的所有内容相同?


Tags: 模块文件网络应用程序类型内容关系格式
3条回答

你在使用here中的docx模块吗?

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

这对我有效:

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()

看起来,Docx for Python并不意味着要存储一个包含图像、标题等的完整Docx。。。,但仅包含文档的内部内容。所以没什么简单的办法。

不过,你可以这样做:

首先,看看docx tag 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. advplace方法替换document.xml
  3. 将docx作为zip打开,并用新的xml内容替换document.xml内容。
  4. 关闭并输出压缩文件(将其重命名为output.docx)

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

相关问题 更多 >