How can I save an edited Word document with Python?

二次信任 提交于 2019-12-03 20:54:37

By casting "zip.read("word/document.xml")", you cast a byte to string so you keep the 'b' as a char.

def getXml(docxFilename):
zip = zipfile.ZipFile(open(docxFilename,"rb"))
xmlString = str(zip.read("word/document.xml"))
return xmlString

So that's why the "xmlString" has no attribute because it's a string. You have to remove you cast an decode before return:

def getXml(docxFilename):
zip = zipfile.ZipFile(open(docxFilename,"rb"))
xmlString = zip.read("word/document.xml")
return xmlString.decode('utf-8')

Hope it will be helpful for others !

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!