python-docx 从下拉列表中获取信息(在 table 中)
python-docx get info from dropdownlist (in table)
我有一个包含多个 table 的 docx 文件,我想从列表中的 table 中获取所有信息(该列表称为 'alletabellen') .
通过下面的脚本,我收到了 table 中的几乎所有信息,除了下拉列表中的一些变量的值(在某些 table 单元格中)。
这些单元格的值在我的列表中保持为空(例如变量 'Number:' 中的值“1.2”,请参阅:https://s30.postimg.org/477j8z6ch/table.png 我在列表中没有得到该值)。
是否也可以从这些变量中获取信息?
import docx
bestand = docx.Document('somefile.docx')
tabellen = bestand.tables
alletabellen = []
for i, tabel in enumerate(tabellen):
for row in tabellen[i].rows:
for cell in row.cells:
for paragraph in cell.paragraphs:
alletabellen.append(paragraph.text)
更新
我找到了解决方案(感谢 scanny 为我指明了正确的方向)。
我没有意识到 docx 文件实际上是一个带有 xml 文件的压缩文件,其中包含所有文本。我使用模块 zipfile 提取 docx,使用模块 bs4 查找所有下拉列表标签 ('ddList') 并将数据放入列表中。在我的文档中有 12 个下拉列表,我只需要其中的 3 个(其中一个是屏幕截图中的 'Number:',这是文档中的第一个下拉列表)。
import docx
import zipfile
from bs4 import BeautifulSoup
doc = 'somefile.docx'
bestand = docx.Document(doc)
tabellen = bestand.tables
#get data from all the "normal" fields
alletabellen = []
for i, tabel in enumerate(tabellen):
for row in tabellen[i].rows:
for cell in row.cells:
for paragraph in cell.paragraphs:
alletabellen.append(paragraph.text)
#get data from all the dropdown lists
document = zipfile.ZipFile(doc)
xml_data = document.read('word/document.xml')
document.close()
soup = BeautifulSoup(xml_data, 'xml')
gegevens = soup.findAll('ddList') #search dropdownlists (n = 12)
dropdownlist = []
dropdownlistdata = []
for i in gegevens:
dropdownlist.append(i.find('result'))
#convert to string for if statements
number = str(dropdownlist[0])
job = str(dropdownlist[1])
vehicle = str(dropdownlist[7])
if number == '<w:result w:val="1"/>' :
dropdownlistdata.append('0,3')
elif number == '<w:result w:val="2"/>' :
dropdownlistdata.append('1,2')
elif number == '<w:result w:val="3"/>' :
dropdownlistdata.append('onbekend')
else:
dropdownlistdata.append('geen')
if job == '<w:result w:val="1"/>' :
dropdownlistdata.append('nee')
else:
dropdownlistdata.append('ja')
if vehicle == '<w:result w:val="1"/>' :
dropdownlistdata.append('nee')
else:
dropdownlistdata.append('ja')
#show data
print alletabellen
print dropdownlistdata
'1.2' 没有从 .text
调用返回的原因很可能是它被包裹在某种 "container" XML 中以使其表现得像表单域。
第一步是检查 XML,以便您了解自己面临的挑战。然后你会写一些代码来找到隐藏的内容。
opc-diag
可以帮助您检查您的 XML:
http://opc-diag.readthedocs.io/en/latest/index.html
您需要查看 document.xml
部分。
如果您 trim 将文档缩小到表现出这种行为的最低限度,这样可以更轻松地找到您需要处理的部分。
如果你可以 post XML 那部分 table 我可以指导你进一步。
我有一个包含多个 table 的 docx 文件,我想从列表中的 table 中获取所有信息(该列表称为 'alletabellen') . 通过下面的脚本,我收到了 table 中的几乎所有信息,除了下拉列表中的一些变量的值(在某些 table 单元格中)。 这些单元格的值在我的列表中保持为空(例如变量 'Number:' 中的值“1.2”,请参阅:https://s30.postimg.org/477j8z6ch/table.png 我在列表中没有得到该值)。
是否也可以从这些变量中获取信息?
import docx
bestand = docx.Document('somefile.docx')
tabellen = bestand.tables
alletabellen = []
for i, tabel in enumerate(tabellen):
for row in tabellen[i].rows:
for cell in row.cells:
for paragraph in cell.paragraphs:
alletabellen.append(paragraph.text)
更新
我找到了解决方案(感谢 scanny 为我指明了正确的方向)。 我没有意识到 docx 文件实际上是一个带有 xml 文件的压缩文件,其中包含所有文本。我使用模块 zipfile 提取 docx,使用模块 bs4 查找所有下拉列表标签 ('ddList') 并将数据放入列表中。在我的文档中有 12 个下拉列表,我只需要其中的 3 个(其中一个是屏幕截图中的 'Number:',这是文档中的第一个下拉列表)。
import docx
import zipfile
from bs4 import BeautifulSoup
doc = 'somefile.docx'
bestand = docx.Document(doc)
tabellen = bestand.tables
#get data from all the "normal" fields
alletabellen = []
for i, tabel in enumerate(tabellen):
for row in tabellen[i].rows:
for cell in row.cells:
for paragraph in cell.paragraphs:
alletabellen.append(paragraph.text)
#get data from all the dropdown lists
document = zipfile.ZipFile(doc)
xml_data = document.read('word/document.xml')
document.close()
soup = BeautifulSoup(xml_data, 'xml')
gegevens = soup.findAll('ddList') #search dropdownlists (n = 12)
dropdownlist = []
dropdownlistdata = []
for i in gegevens:
dropdownlist.append(i.find('result'))
#convert to string for if statements
number = str(dropdownlist[0])
job = str(dropdownlist[1])
vehicle = str(dropdownlist[7])
if number == '<w:result w:val="1"/>' :
dropdownlistdata.append('0,3')
elif number == '<w:result w:val="2"/>' :
dropdownlistdata.append('1,2')
elif number == '<w:result w:val="3"/>' :
dropdownlistdata.append('onbekend')
else:
dropdownlistdata.append('geen')
if job == '<w:result w:val="1"/>' :
dropdownlistdata.append('nee')
else:
dropdownlistdata.append('ja')
if vehicle == '<w:result w:val="1"/>' :
dropdownlistdata.append('nee')
else:
dropdownlistdata.append('ja')
#show data
print alletabellen
print dropdownlistdata
'1.2' 没有从 .text
调用返回的原因很可能是它被包裹在某种 "container" XML 中以使其表现得像表单域。
第一步是检查 XML,以便您了解自己面临的挑战。然后你会写一些代码来找到隐藏的内容。
opc-diag
可以帮助您检查您的 XML:
http://opc-diag.readthedocs.io/en/latest/index.html
您需要查看 document.xml
部分。
如果您 trim 将文档缩小到表现出这种行为的最低限度,这样可以更轻松地找到您需要处理的部分。
如果你可以 post XML 那部分 table 我可以指导你进一步。