Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python从Word中提取XML代码时出现的问题_Python_Xml_Docx_Zipfile - Fatal编程技术网

使用Python从Word中提取XML代码时出现的问题

使用Python从Word中提取XML代码时出现的问题,python,xml,docx,zipfile,Python,Xml,Docx,Zipfile,我试图用Python从Word文档中提取XML代码。以下是我尝试的代码: def getXml(docxFilename): zip = zipfile.ZipFile(open(docxFilename,"rb")) xmlString= str(zip.read("word/document.xml")) return xmlString 我创建了一个测试文档,并在其上运行函数getXML。结果如下: b'<?xml version="1.0" encodi

我试图用Python从Word文档中提取XML代码。以下是我尝试的代码:

def getXml(docxFilename):
    zip = zipfile.ZipFile(open(docxFilename,"rb"))
    xmlString= str(zip.read("word/document.xml"))
    return xmlString
我创建了一个测试文档,并在其上运行函数
getXML
。结果如下:

 b'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\r\n<w:document xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml"><w:body><w:p w:rsidR="00971B91" w:rsidRPr="00971B91" w:rsidRDefault="00B52719"><w:pPr><w:rPr><w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman"/><w:sz w:val="24"/><w:szCs w:val="24"/></w:rPr></w:pPr><w:r><w:t>Test</w:t></w:r></w:p><w:sectPr w:rsidR="00971B91" w:rsidRPr="00971B91" w:rsidSect="009C4305"><w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0"/><w:cols w:space="708"/><w:docGrid w:linePitch="360"/></w:sectPr></w:body></w:document>'
然后我运行代码
etree.tostring(getXmlTree(getXml(“test.docx”)),pretty\u print=True)
,并收到更合理的XML代码

当我试图创建一个新的Word文档时,问题就出现了。我创建了以下函数来将XML代码转换为Word文档(不知羞耻地从中窃取):

在尝试创建新的Word文档之前,我想看看是否可以通过将
xmlContent=getXmlTree(getXml(“test.docx”)
替换为上述函数中的参数来创建原始测试文档的副本。但是,当我运行代码时,我收到一条错误消息:

f.write(xmlString)

TypeError: must be str, not bytes
相反,使用
f.write(str(xmlString))
没有帮助;它创建了一个新的word文档,但如果我试图打开它,word将崩溃


EDIT2:尝试使用
f.write(xmlString.decode(“utf-8”))
运行上述代码,但没有帮助;Word仍然崩溃。

我猜XML编码不正确。首先,使用
“wb”
作为模式以二进制形式编写文档文件。其次,说明编码是什么,并包含XML声明

with open(os.path.join(tmpDir, "word/document.xml"), "wb") as f:
    xmlBytes = etree.tostring(xmlContent, encoding="UTF-8", xml_declaration=True, pretty_print=True)
    f.write(xmlBytes)

那是一根绳子!下一步可能是使用python中的一个来解析它。许多xml解析器尝试查找@dm03514。请查看我文章的编辑。谢谢。我没有遵循你的确切解决方案;相反,我只是将
getXml
中的
xmlString=str(zip.read(“word/document.xml”)
替换为
xmlString=zip.read(“word/document.xml”).decode(“uft-8”)
,这就成功了。我仍然有一些问题,但是,这是概述;如果你能看一看,我将不胜感激。
f.write(xmlString)

TypeError: must be str, not bytes
with open(os.path.join(tmpDir, "word/document.xml"), "wb") as f:
    xmlBytes = etree.tostring(xmlContent, encoding="UTF-8", xml_declaration=True, pretty_print=True)
    f.write(xmlBytes)