This article demonstrates how to edit form fields in a Word document using GroupDocs.Editor for Python via .NET. It guides you through loading a document, inspecting its form fields, and updating them.
Step-by-Step Guide
Load the document into the Editor instance
Open the document as a binary stream and pass it to the Editor class together with WordProcessingLoadOptions. If the document is password-protected, specify the password through the load options.
fromgroupdocs.editorimportEditorfromgroupdocs.editor.optionsimportWordProcessingLoadOptionswithopen("form-fields.docx","rb")asstream:withEditor(stream,WordProcessingLoadOptions())aseditor:# Further code will be placed herepass
A specific form field can be modified and the change applied back to the document with the update_form_filed(...) method. The snippet below is illustrative — retrieve the form field of interest from the collection, modify its properties, and pass the collection to update_form_filed(...). Adjust the per-type access details to the exact members exposed by your build.
# Illustrative: retrieve, modify, and update a text form fieldtext_field=collection.get_form_field("Text1")text_field.locale_id=1029text_field.value="new Value"field_manager.update_form_filed(collection)
Save the updated document
After updating the form fields, save the document with editor.save(...) using the desired save options.
Below is the complete runnable example. It opens the document, retrieves the form field manager, and reports the number of form fields it contains. The mutation steps shown above are illustrative; the runnable example performs only safe, documented calls.
importosfromgroupdocs.editorimportEditor,Licensefromgroupdocs.editor.optionsimportWordProcessingLoadOptionsdefedit_and_update_form_fields():# Optionally set a licenselicense_path=os.path.abspath("./GroupDocs.Editor.lic")ifos.path.exists(license_path):License().set_license(license_path)# Open the document as a stream and load it with WordProcessingLoadOptionswithopen("./form-fields.docx","rb")asstream:withEditor(stream,WordProcessingLoadOptions())aseditor:# Read the FormFieldManager instancefield_manager=editor.form_field_manager# Read the form field collectioncollection=field_manager.form_field_collectionprint("Has invalid form fields:",field_manager.has_invalid_form_fields())print("Form fields count:",len(collection))if__name__=="__main__":edit_and_update_form_fields()
form-fields.docx is the sample file used in this example. Click here to download it.
Has invalid form fields: True
Form fields count: 31