python-docx:读取表的特定单元格中的下拉列表

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

我有几个表,它们在.docx文件中包含下拉列表,我想在某些特定的单元格中获取这些列表的值。多亏了这个线程:python-docx get info from dropdownlist (in table),我能够使用以下代码获取文档的所有下拉列表的值:

from zipfile import ZipFile
from bs4 import BeautifulSoup

file_name = 'document.docx'

# open docx file as a zip file and store its relevant xml data
zip_file = ZipFile(file_name)
xml_data = zip_file.read('word/document.xml')
zip_file.close()

# parse the xml data with BeautifulSoup
soup = BeautifulSoup(xml_data, 'xml')

# look for all values of dropdown lists in the data and store them
list_of_value = []
dd_lists_content = soup.find_all('sdtContent')
for i in dd_lists_content:
    list_of_value.append(i.find('t').string)

现在,我只想获取特定单元格中包含的某些下拉列表的值,而不是拥有所有值的列表。由于我是xml的初学者,因此我真的不知道该如何处理该问题。有什么办法可以使用python-docx来做到这一点?

[这里是我从一个文档获得的xml,该文档包含一个带有两个单元格的表(一行,两列)。第二个单元格(第二列)中有一个下拉列表。

<w:body>
    <w:tbl>
        <w:tblPr>
            <w:tblStyle w:val="TableGrid"/>
            <w:tblW w:w="0" w:type="auto"/>
            <w:tblLook w:val="04A0" w:firstRow="1" w:lastRow="0" w:firstColumn="1" w:lastColumn="0" w:noHBand="0" w:noVBand="1"/>
        </w:tblPr>
        <w:tblGrid>
            <w:gridCol w:w="4530"/>
            <w:gridCol w:w="4531"/>
        </w:tblGrid>
        <w:tr w:rsidR="00D87065" w14:paraId="72A579CB" w14:textId="77777777" w:rsidTr="008A68C3">
            <w:tc>
                <w:tcPr>
                    <w:tcW w:w="4530" w:type="dxa"/>
                </w:tcPr>
                <w:p w14:paraId="6DE8A678" w14:textId="28D4E672" w:rsidR="00D87065" w:rsidRDefault="00D87065" w:rsidP="00D87065">
                    <w:r>
                        <w:t>Normal cell</w:t>
                    </w:r>
                </w:p>
            </w:tc>
            <w:sdt>
                <w:sdtPr>
                    <w:id w:val="834274196"/>
                    <w:placeholder>
                        <w:docPart w:val="38439BE74EB3458EB38183CFE71463D5"/>
                    </w:placeholder>
                    <w:dropDownList>
                        <w:listItem w:displayText="A value in a dropdown list" w:value="A value in a dropdown list"/>
                        <w:listItem w:displayText="Another value in a dropdown list" w:value="Another value in a dropdown list"/>
                    </w:dropDownList>
                </w:sdtPr>
                <w:sdtContent>
                    <w:tc>
                        <w:tcPr>
                            <w:tcW w:w="4531" w:type="dxa"/>
                        </w:tcPr>
                        <w:p w14:paraId="11472D67" w14:textId="43D0742A" w:rsidR="00D87065" w:rsidRDefault="00D87065" w:rsidP="00D87065">
                            <w:r>
                                <w:t>A value in a dropdown list</w:t>
                            </w:r>
                        </w:p>
                    </w:tc>
                </w:sdtContent>
            </w:sdt>
        </w:tr>
    </w:tbl>
    <w:p w14:paraId="336AB02A" w14:textId="276C1AFE" w:rsidR="0047588A" w:rsidRPr="0047588A" w:rsidRDefault="0047588A" w:rsidP="00D87065">
        <w:pPr>
            <w:pStyle w:val="Heading3"/>
        </w:pPr>
    </w:p>
    <w:sectPr w:rsidR="0047588A" w:rsidRPr="0047588A" w:rsidSect="00341D09">
        <w:pgSz w:w="11907" w:h="16840" w:code="9"/>
        <w:pgMar w:top="1418" w:right="1418" w:bottom="1418" w:left="1418" w:header="720" w:footer="720" w:gutter="0"/>
        <w:cols w:space="720"/>
        <w:docGrid w:linePitch="360"/>
    </w:sectPr>
</w:body>

在这种情况下,我希望能够在第二个单元格中搜索下拉列表的值,即document.tables[0].cells(0,1),并获得'A value of a dropdown list'作为输出。此信息包装在xml元素<w:t>A value in a dropdown list</w:t>中。

python xml docx python-docx
1个回答
0
投票

我正在使用parsel库,因此我可以使用xpath-更容易获得值:

from parsel import Selector
sel = Selector(data,'xml')

#register namespace
sel.register_namespace("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main" )

#path to dropdownlost value : 
path = "//w:sdtContent//w:t/text()"
outcome = sel.xpath(path).getall()

print(outcome)
['A value in a dropdown list']

请注意,parsel建立在lxml之上,使用IMO更容易。另外,请看一下xpath,因为如果您要使用xml,这可能会很有利。

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