![](/img/trans.png)
[英]Ho to read the number of columns (colspan) used by a single table cell with python-docx
[英]python-docx: read dropdown lists in a specific cell of a table
我有幾個表包含.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'
為 output . 此信息包含在 xml 元素<w:t>A value in a dropdown list</w:t>
中。
我正在使用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,它可能會很有優勢。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.