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()
+--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
你在使用here中的docx模块吗?
如果是,那么docx模块已经公开了replace、advReplace等方法,这些方法可以帮助您完成任务。有关公开方法的更多详细信息,请参阅source code。
这对我有效:
看起来,Docx for Python并不意味着要存储一个包含图像、标题等的完整Docx。。。,但仅包含文档的内部内容。所以没什么简单的办法。
不过,你可以这样做:
首先,看看docx tag wiki:
它解释了如何解压缩docx文件:下面是一个典型文件的外观:
Docx只获取文档的一部分,方法是opendocx
它只获取document.xml文件。
我建议你做的是:
如果您安装了node.js,请注意我已经在DocxGenJS上工作过,它是docx文档的模板引擎,该库正在进行活动开发,并且将作为节点模块发布。
相关问题 更多 >
编程相关推荐