尝试使用Python解析xml格式的docx文档以打印粗体单词

use*_*743 4 python xml

我有一个 word docx 文件,我想打印以粗体显示的单词,以 xml 格式浏览文档,似乎我要打印的单词具有以下属性。

<w:r w:rsidRPr="00510F21">
  <w:rPr><w:b/>
     <w:noProof/>
     <w:sz w:val="22"/>
     <w:szCs w:val="22"/>
  </w:rPr>
  <w:t>Print this Sentence</w:t>
</w:r>
Run Code Online (Sandbox Code Playgroud)

具体来说,w:rsidRPr="00510F21"指定文本为粗体的属性。下面是更多的 XML 文档,可以更好地了解其结构。

<w:p w14:paraId="64E19BC3" w14:textId="4D8C930F" w:rsidR="00FF6AD1" w:rsidRDefault="00FF6AD1" w:rsidP="00C11B48">
<w:pPr>
   <w:ind w:left="360" w:hanging="360"/>
   <w:jc w:val="both"/>
   <w:rPr>
       <w:sz w:val="22"/>
       <w:szCs w:val="22"/>
   </w:rPr>
 </w:pPr>
 <w:r>
    <w:rPr><w:b/>
       <w:noProof/><w:sz w:val="22"/>
       <w:szCs w:val="22"/>
    </w:rPr><w:t xml:space="preserve">Some text</w:t>
 </w:r>
 <w:r w:rsidRPr="0009466D">
     <w:rPr><w:i/><w:noProof/>
          <w:sz w:val="22"/><w:szCs w:val="22"/>
     </w:rPr>
     <w:t>For example</w:t>
 </w:r>
 <w:r>
     <w:rPr>
        <w:noProof/>
        <w:sz w:val="22"/>
        <w:szCs w:val="22"/>
     </w:rPr><w:t xml:space="preserve">
     </w:t>
 </w:r>
 <w:r w:rsidRPr="00510F21">
     <w:rPr>
         <w:b/>
         <w:noProof/>
         <w:sz w:val="22"/>
         <w:szCs w:val="22"/>
     </w:rPr>
     <w:t>Print this stuff</w:t>
 </w:r>
Run Code Online (Sandbox Code Playgroud)

在做了一些研究并尝试使用 Python-docx 库来做到这一点之后,我决定尝试使用lxml. 我收到有关命名空间的错误,并尝试添加该命名空间,但它返回一个空集。下面是文档中的一些命名空间内容。

<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:w15="http://schemas.microsoft.com/office/word/2012/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 w15 wp14">
Run Code Online (Sandbox Code Playgroud)

下面是我正在使用的代码。如果属性是 ,我想再次打印w:rsidRPr="00510F21"

from lxml import etree
root = etree.parse("document.xml")

namespaces = {'w':'http://schemas.openxmlformats.org/wordprocessingml/2006/main'}

wr_roots = root.findall('w:r', namespaces)
print wr_roots # prints empty set

for atype in wr_roots:
   if w:rsidRPr == '00510F21':
       print(atype.get('w:t'))
Run Code Online (Sandbox Code Playgroud)

mha*_*wke 6

如果您想查找所有粗体文本,可以findall()xpath表达式一起使用:

from lxml import etree

namespaces = {'w':'http://schemas.openxmlformats.org/wordprocessingml/2006/main'}

root = etree.parse('document.xml').getroot()
for e in root.findall('.//w:r/w:rPr/w:b/../../w:t', namespaces):
    print(e.text)
Run Code Online (Sandbox Code Playgroud)

不要寻找具有属性的w:r节点(我不相信它表示粗体文本),而是在运行属性标记 ( ) 中w:rsidRPr="00510F21"查找运行节点 ( ) ,然后访问其中的文本标记 ( )。标记是此处记录的粗体属性。w:rw:bw:rPrw:tw:b

xpath 表达式可以简化为,'.//w:b/../../w:t'尽管这不太严格并且可能会导致错误匹配。