【问题标题】:python-docx: read dropdown lists in a specific cell of a tablepython-docx:读取表格特定单元格中的下拉列表
【发布时间】:2023-11-03 21:52:01
【问题描述】:

我有几个表在.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 元素 &lt;w:t&gt;A value in a dropdown list&lt;/w:t&gt; 中。

【问题讨论】:

  • xml 有命名空间吗?请分享你的输出应该是什么。你是否针对这一行的文本:下拉列表中的值
  • 我认为这是相关的命名空间:xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
  • 是的,我正在寻找这条线:&lt;w:t&gt;A value in a dropdown list&lt;/w:t&gt;。但我希望能够在特定单元格中搜索这一行。鉴于我在表 [0] 的单元格 (0, 1) 中查看的输入,输出应该是“下拉列表中的值”

标签: python xml docx python-docx


【解决方案1】:

我正在使用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,它可能会很有优势

【讨论】: