I'm using pymupdf and just trying to write some text into an already existing pdf form field (widget). I was able to identify the widget by its xref, and read its contents, but I don't know how to modify its field_value and save it back. I've tried with different combinations of page.add_widget as you can see below, but without success. Thanks in advance for your help.
import fitz
import pprint as pp
doc = fitz.open('Loan01.pdf')
page = doc.load_page(0)
field=page.load_widget(724) #pg_firstname (which is empty)
field.field_value="Mary"
print(field.field_value) #ok, prints Mary
#page.delete_widget(field)
annot= page.add_widget(field)
#page=doc.reload_page(page)
#print('Page',page.load_widget(724).field_value)
for i,field in enumerate(page.widgets()):
print(i,field.field_name, field.xref, field.field_value,'#')
#but field_value continue being empty, Mary is not there
doc.save('output01.pdf') #and in the output file the field is now empty and dead
and below is the widget's contents
{'_annot': 'Widget' annotation on page 0 of Loan01.pdf,
'_text_da': '',
'border_color': [0.0],
'border_dashes': None,
'border_style': 'Solid',
'border_width': 1.0,
'button_caption': None,
'choice_values': None,
'field_display': 0,
'field_flags': 0,
'field_label': '',
'field_name': 'pg_firstname',
'field_type': 7,
'field_type_string': 'Text',
'field_value': '',
'fill_color': None,
'is_signed': None,
'parent': <weakproxy at 0x0000026DC5403330 to Page at 0x0000026DC53F4520>,
'rect': Rect(136.5, 196.5, 304.875, 212.25),
'script': None,
'script_calc': None,
'script_change': None,
'script_format': None,
'script_stroke': None,
'text_color': [0.0],
'text_font': 'Helv',
'text_fontsize': 0.0,
'text_format': 0,
'text_maxlen': 0,
'xref': 724}