Edit and Update Form Fields

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

  1. 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.

    from groupdocs.editor import Editor
    from groupdocs.editor.options import WordProcessingLoadOptions
    
    with open("form-fields.docx", "rb") as stream:
        with Editor(stream, WordProcessingLoadOptions()) as editor:
            # Further code will be placed here
            pass
    
  2. Retrieve the FormFieldManager

    Obtain the FormFieldManager instance from the form_field_manager property of the Editor class and read the form_field_collection.

    with open("form-fields.docx", "rb") as stream:
        with Editor(stream, WordProcessingLoadOptions()) as editor:
            field_manager = editor.form_field_manager
            collection = field_manager.form_field_collection
            print("Form fields count:", len(collection))
    
  3. Update a form field

    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 field
    text_field = collection.get_form_field("Text1")
    text_field.locale_id = 1029
    text_field.value = "new Value"
    field_manager.update_form_filed(collection)
    
  4. Save the updated document

    After updating the form fields, save the document with editor.save(...) using the desired save options.

    from groupdocs.editor.options import WordProcessingSaveOptions
    from groupdocs.editor.formats import WordProcessingFormats
    
    save_options = WordProcessingSaveOptions(WordProcessingFormats.DOCX)
    editable = editor.edit()
    editor.save(editable, "output.docx", save_options)
    

Complete code example

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.

import os
from groupdocs.editor import Editor, License
from groupdocs.editor.options import WordProcessingLoadOptions

def edit_and_update_form_fields():
    # Optionally set a license
    license_path = os.path.abspath("./GroupDocs.Editor.lic")
    if os.path.exists(license_path):
        License().set_license(license_path)

    # Open the document as a stream and load it with WordProcessingLoadOptions
    with open("./form-fields.docx", "rb") as stream:
        with Editor(stream, WordProcessingLoadOptions()) as editor:
            # Read the FormFieldManager instance
            field_manager = editor.form_field_manager

            # Read the form field collection
            collection = field_manager.form_field_collection

            print("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

Download full output