You need to enable JavaScript to run this app. 如何通过OXML字符串快速创建OxmlElement元素树?
优惠活动
大模型
产品
解决方案
定价
更多

如何通过OXML字符串快速创建OxmlElement元素树?

问题解答

python-docx 已经内置了从OXML字符串直接创建元素树的功能,不需要自行实现,使用docx.oxml模块下的parse_xml()函数即可。

使用示例

基础用法

符合你需求的调用方式如下:

from docx.oxml import parse_xml

# 传入格式正确的OXML字符串
element = parse_xml("""
<w:r xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
    <w:t>text</w:t>
</w:r>
""")

# 输出序列化后的XML内容
print(element.xml)
# 输出结果为: <w:r><w:t>text</w:t></w:r>

你提供的复杂OXML树生成示例

注意需要在根节点声明所有用到的命名空间,避免解析错误:

from docx.oxml import parse_xml

oxml_str = """
<mc:AlternateContent xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
    xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
    xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing"
    xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
    xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape">

<mc:Choice Requires="wps">
<w:drawing>
<wp:inline distT="0" distB="20320" distL="0" distR="28575" wp14:anchorId="6F69A487">
<wp:extent cx="5573395" cy="742950"/>
<wp:effectExtent l="0" t="0" r="28575" b="20320"/>
<wp:docPr id="6" name="Shape3"/>
<a:graphic>
<a:graphicData uri="http://schemas.microsoft.com/office/word/2010/wordprocessingShape">
<wps:wsp>
<wps:cNvSpPr/>
<wps:spPr>
<a:xfrm>
<a:off x="0" y="0"/>
<a:ext cx="5572800" cy="742320"/>
</a:xfrm>
<a:prstGeom prst="roundRect">
<a:avLst>
<a:gd name="adj" fmla="val 4167"/>
</a:avLst>
</a:prstGeom>
<a:noFill/>
<a:ln w="19050">
<a:solidFill>
<a:srgbClr val="4472c4"/>
</a:solidFill>
</a:ln>
</wps:spPr>
<wps:style>
<a:lnRef idx="2">
<a:schemeClr val="accent1">
<a:shade val="50000"/>
</a:schemeClr>
</a:lnRef>
<a:fillRef idx="1">
<a:schemeClr val="accent1"/>
</a:fillRef>
<a:effectRef idx="0">
<a:schemeClr val="accent1"/>
</a:effectRef>
<a:fontRef idx="minor"/>
</wps:style>
<wps:txbx>
<w:txbxContent>
<w:p>
<w:pPr>
<w:pStyle w:val="FrameContents"/>
<w:spacing w:before="0" w:after="0"/>
<w:rPr>
<w:rStyle w:val="BookTitle"/>
</w:rPr>
</w:pPr>
<w:sdt>
<w:sdtPr>
<w:id w:val="1401119907"/>
</w:sdtPr>
<w:sdtContent>
<w:r>
<w:rPr>
<w:rStyle w:val="BookTitle"/>
</w:rPr>
<w:t xml:space="preserve">Click </w:t>
</w:r>
<w:r>
<w:rPr>
<w:rStyle w:val="BookTitle"/>
</w:rPr>
<w:t>and/or paste to write your block text</w:t>
</w:r>
</w:sdtContent>
</w:sdt>
</w:p>
<w:p>
<w:pPr>
<w:pStyle w:val="FrameContents"/>
<w:spacing w:before="0" w:after="0"/>
<w:rPr>
<w:color w:val="4472C4" w:themeColor="accent1"/>
</w:rPr>
</w:pPr>
<w:r>
<w:rPr>
<w:rStyle w:val="Style11"/>
<w:color w:val="7F7F7F" w:themeColor="text1" w:themeTint="80"/>
<w:sz w:val="20"/>
<w:szCs w:val="20"/>
</w:rPr>
<w:t>Paste url here</w:t>
</w:r>
</w:p>
</w:txbxContent>
</wps:txbx>
<wps:bodyPr anchor="ctr">
<a:prstTxWarp prst="textNoShape"/>
<a:noAutofit/>
</wps:bodyPr>
</wps:wsp>
</a:graphicData>
</a:graphic>
</wp:inline>
</w:drawing>
</mc:Choice>
<mc:Fallback>
<w:pict/>
</mc:Fallback>
</mc:AlternateContent>
"""

alternate_content = parse_xml(oxml_str)
# 生成的对象可直接插入到文档对应位置,例如插入到段落中
# doc.paragraphs[0]._p.append(alternate_content)

注意事项

  • 传入的XML字符串必须是格式良好的,标签需要正确闭合,否则会解析报错
  • 所有用到的命名空间前缀必须在根节点提前声明,避免未知前缀的解析错误
  • parse_xml返回的对象是OxmlElement子类的实例,完全兼容python-docx内部的元素操作逻辑,可以直接调用append、find等方法进行操作

内容的提问来源于stack exchange,提问作者Always-prog

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.19 03:06:00