()
| 337 | |
| 338 | |
| 339 | def test_4004(): |
| 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') |
| 358 | doc = pymupdf.open(path) |
| 359 | widgets_by_name = get_widgets_by_name(doc) |
| 360 | |
| 361 | # Print widget information |
| 362 | for name, widgets in widgets_by_name.items(): |
| 363 | print(f"Widget Name: {name}") |
| 364 | for entry in widgets: |
| 365 | widget = entry["widget"] |
| 366 | page_num = entry["page_num"] |
| 367 | print(f" Page: {page_num + 1}, Type: {widget.field_type}, Value: {widget.field_value}, Rect: {widget.rect}") |
| 368 | |
| 369 | # Attempt to update field value |
| 370 | w = widgets_by_name["Text1"][0] |
| 371 | field = w['widget'] |
| 372 | field.value = "1234567890" |
| 373 | try: |
| 374 | field.update() |
| 375 | except Exception as e: |
| 376 | assert str(e) == 'Annot is not bound to a page' |
| 377 | |
| 378 | doc.close() |
| 379 | |
| 380 | |
| 381 | def test_4055(): |
nothing calls this directly
no test coverage detected
searching dependent graphs…