Extracts and returns a dictionary of widgets indexed by their names.
(doc)
| 340 | import collections |
| 341 | |
| 342 | def get_widgets_by_name(doc): |
| 343 | """ |
| 344 | Extracts and returns a dictionary of widgets indexed by their names. |
| 345 | """ |
| 346 | widgets_by_name = collections.defaultdict(list) |
| 347 | for page_num in range(len(doc)): |
| 348 | page = doc.load_page(page_num) |
| 349 | for field in page.widgets(): |
| 350 | widgets_by_name[field.field_name].append({ |
| 351 | "page_num": page_num, |
| 352 | "widget": field |
| 353 | }) |
| 354 | return widgets_by_name |
| 355 | |
| 356 | # Open document and get widgets |
| 357 | path = os.path.normpath(f'{__file__}/../../tests/resources/test_4004.pdf') |