<w:pict> Python-docx 代码未拾取 docx 元素

问题描述 投票:0回答:1

我正在使用https://github.com/kmrambo/Python-docx-Reading-paragraphs-tables-and-images-in-document-order-

给出的代码

此代码适用于段落和表格,甚至我自己粘贴到文档中的图像,但是,我尝试处理的文档有问题。运行此代码时,图像列表为空。经过进一步检查,我设法发现 XML 中图像存储方式的差异。

如果图像以以下格式存储,则代码可以正常工作。如果我手动将图像粘贴到 docx 中,就会发生这种情况:

                    <pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
                       <pic:nvPicPr>
                          <pic:cNvPr id="0" name="Picture 1"/>
                          <pic:cNvPicPr>
                             <a:picLocks noChangeArrowheads="1" noChangeAspect="1"/>
                          </pic:cNvPicPr>
                       </pic:nvPicPr>
                       <pic:blipFill>
                          <a:blip r:embed="rId4">
                             <a:extLst>
                                <a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}">
                                   <a14:useLocalDpi val="0" xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main"/>
                                </a:ext>
                             </a:extLst>
                          </a:blip>
                          <a:srcRect/>
                          <a:stretch>
                             <a:fillRect/>
                          </a:stretch>
                       </pic:blipFill>
                       <pic:spPr bwMode="auto">
                          <a:xfrm>
                             <a:off x="0" y="0"/>
                             <a:ext cx="3743325" cy="2962275"/>
                          </a:xfrm>
                          <a:prstGeom prst="rect">
                             <a:avLst/>
                          </a:prstGeom>
                          <a:noFill/>
                          <a:ln>
                             <a:noFill/>
                          </a:ln>
                       </pic:spPr>
                    </pic:pic>

但是,我正在使用的文档使用以下格式,Python 代码未采用该格式:

                 <w:pict w14:anchorId="71E2ADA8">
                    <v:shape id="_x0000_i1062" style="width:185.15pt;height:174.85pt" type="#_x0000_t75">
                       <v:imagedata o:title="" r:id="rId56"/>
                    </v:shape>
                 </w:pict>

我不确定如何配置代码来处理这种差异

我正在使用的示例 docx 文件可以在以下位置找到:https://www.3gpp.org/ftp//Specs/archive/21_series/21.914/21914-e00.zip

python-3.x xml python-docx
1个回答
0
投票

您可以尝试使用:

for paragraph in doc.paragraphs:
    pic2_list = paragraph._element.xpath('.//pic:pic') 
    pict_list = paragraph._element.xpath('.//w:pict')

使用 python-docx 从 docx 文件中获取这两种类型的图片。

© www.soinside.com 2019 - 2024. All rights reserved.