栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在docx中进行文本替换,并使用python-docx保存更改的文件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在docx中进行文本替换,并使用python-docx保存更改的文件

看起来,Docx for Python并不意味着要存储带有图像,标头…的完整Docx,而仅包含文档的内部内容。因此,没有简单的方法可以做到这一点。

Howewer,这是您的操作方法:

首先,看看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,然后用新的xml内容替换document.xml内容。
  4. 关闭并输出压缩文件(将其重命名为output.docx)

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



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/626077.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号