You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Read information from selext box / combo box? #1197
Hello - i would like to read the information from the select box / combo box in the attached word-document -
(its selected "Lisa" under JOB DETAILS at the top in the attached file)
When i try to read this information with a loop trough the table for all paragraphs with eg. this code
t = doc.tables
for idxT,table in enumerate(doc.tables):
for idxR, row in enumerate(table.rows):
for idxC, cell in enumerate(row.cells):
for idxP, para in enumerate(cell.paragraphs):
I didn´t catch this information.
Is there any way to get this information?
import docx
import zipfile
from bs4 import BeautifulSoup as bs
try:
file = 'inp.docx'
doc = docx.Document(file)
ddl = doc.tables
for idxT,table in enumerate(doc.tables):
for idxR, row in enumerate(table.rows):
for idxC, cell in enumerate(row.cells):
for idxP, para in enumerate(cell.paragraphs):
print(f'para {para.text}')
# get data from the dropdown lists
document = zipfile.ZipFile(file)
xml_data = document.read('word/document.xml')
document.close()
soup = bs(xml_data, 'xml')
ddlists = soup.findAll('dropDownList')
for ddl in ddlists:
ddl_parent = ddl.parent
ddl_parent_entries = list(ddl_parent.children)
for ddl_parent_entry in ddl_parent_entries:
val = ddl_parent_entry.get('w:val')
if val and 'Entered by' in val:
ddl_entries = list(ddl.children)
names = []
for entry in ddl_entries:
names.append(entry.get('w:displayText'))
sibling = ddl_parent.next_sibling
select = sibling.findNext('w:t')
selected = select.get_text()
print(f'List of names: {names}')
print(f'Name selected: {selected}')
except:
print("Error")
Activity
Austin1990 commentedon May 19, 2023
You need to follow the method in this post:
python-docx get info from dropdownlist (in table) - Stack Overflow
The word\document.xml file in your document contains this structure from which you can extract the information you need :
Austin1990 commentedon May 19, 2023
Managed to produce some sample code: