pythonpdfpymupdf

PyMuPDF: It is possible to get a face value from a combobox widget?


I am trying to extract a chosen value from a combobox widget using the following Python code:

#Extract data from combobox
import fitz
fileIN_Master = "Mypdf.pdf"     
with fitz.open(fileIN_Master) as doc:
for page in doc: 
widgets = page.widgets()
for widget in widgets:
if widget.field_type_string in ('ComboBox'):
print('field_name:', widget.field_name, 'field_value:', widget.field_value)

All I can get is field name and export value (field_name). I was wondering if it is possible to get the face value as well.


Solution

  • A combo box item may have 1 or 2 sub items. If you have the Widget object w you can do this to access both values where present:

    In [5]: w.field_value
    Out[5]: 'England'
    In [6]: [item for item in w.choice_values if w.field_value in item]
    Out[6]: [('England', 'GB')]
    In [8]: w.choice_values  # all available values: there are items with 1 or 2 subitems
    Out[8]:
    [('Spanien', 'ES'),
     ('Frankreich', 'F'),
     ('Holland', 'NL'),
     ('Dänemark', 'DK'),
     ('Schweden', 'S'),
     ('Norwegen', 'N'),
     ('England', 'GB'),
     ('Polen', 'P'),
     'Italien',
     'Portugal',
     'Griechenland']