簡體   English   中英

如何創建元素的 OXML 樹?

[英]How I can create OXML tree of elements?

我知道我可以像這樣創建 OxmlElement:

OxmlElement("w:r")

但我不知道如何創建 OXML 元素樹。 例如,我需要復制 OXML 樹:

<mc:AlternateContent>

<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 xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
<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>

我不想逐行手動創建所有標簽。 我可以通過 function 來做嗎? 我需要像這樣 function :

element = CreateOxmlElementByString("""
<w:r>
    <w:t>text<w:t>
</w:r>
"""

print(element.xml) # result of print: "<w:r><w:t>text<w:t></w:r>"


也許python-docx已經有一個可以通過OXML字符串創建OxmlElement的function? 或者也許我需要自己創建這個 function? 謝謝!

parse_xml() function 在python-docx中完成這項工作:

from docx.oxml import parse_xml
from docx.oxml.ns import nsdecls

r = parse_xml(
    "<w:r %s>\n"
    "  <w:t>text<w:t>\n"
    "</w:r>" % nsdecls("w")
)
"""

要解析 XML,您需要使用 docx.oxml 庫中的 parse_xml。 並使用此代碼:

DOCUMENT_NS = '<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mo="http://schemas.microsoft.com/office/mac/office/2008/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="urn:schemas-microsoft-com:mac:vml" 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:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" 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:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 wp14">\n<w:body>'
SET_XML_INTO_NAMESPACES = lambda xml: "{}{}</w:body></w:document>".format(DOCUMENT_NS,xml)  # for creating XML format which lxml can 


xml = "<w:r><w:t>text</w:t></w:r>"
xml = SET_XML_INTO_NAMESPACES(xml)
print(xml)  # <CT_Document '<w:document>' at 0x2160b3179f0>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM