Word documents leverage Content Control technology to infuse dynamic vitality into document content, offering users enhanced flexibility and convenience when editing and managing documents. These controls, serving as interactive elements, empower users to freely add, remove, or adjust specified content sections while preserving the integrity of the document structure, thereby facilitating agile iterations and personalized customization of document content. This article will guide you how to use Spire.Doc for Python to modify content controls in Word documents within a Python project.

Install Spire.Doc for Python

This scenario requires Spire.Doc for Python and plum-dispatch v1.7.4. They can be easily installed in your VS Code through the following pip command.

pip install Spire.Doc

If you are unsure how to install, please refer to this tutorial: How to Install Spire.Doc for Python on Windows

Modify Content Controls in the Body using Python

In Spire.Doc, the object type for the body content control is StructureDocumentTag. To modify these controls, one needs to traverse the Section.Body.ChildObjects collection to locate objects of type StructureDocumentTag. Below are the detailed steps:

  • Create a Document object.
  • Use the Document.LoadFromFile() method to load a Word document into memory.
  • Retrieve the body of a section in the document using Section.Body.
  • Traverse the collection of child objects within Body.ChildObjects, identifying those that are of type StructureDocumentTag.
  • Within the StructureDocumentTag.ChildObjects sub-collection, perform modifications based on the type of each child object.
  • Finally, utilize the Document.SaveToFile() method to save the changes back to the Word document.
  • Python
from spire.doc import *
from spire.doc.common import *

# Create a new document object
doc = Document()

# Load the document content from a file
doc.LoadFromFile("Sample1.docx")

# Get the body of the document
body = doc.Sections.get_Item(0).Body

# Create lists for paragraphs and tables
paragraphs = []
tables = []

for i in range(body.ChildObjects.Count):
        obj = body.ChildObjects.get_Item(i)
        # If it is a StructureDocumentTag object
        if obj.DocumentObjectType == DocumentObjectType.StructureDocumentTag:
            sdt = (StructureDocumentTag)(obj)

            # If the tag is "c1" or the alias is "c1"
            if sdt.SDTProperties.Tag == "c1" or sdt.SDTProperties.Alias == "c1":
                for j in range(sdt.ChildObjects.Count):
                    child_obj = sdt.ChildObjects.get_Item(j)

                    # If it is a paragraph object
                    if child_obj.DocumentObjectType == DocumentObjectType.Paragraph:
                        paragraphs.append(child_obj)
                    
                    # If it is a table object
                    elif child_obj.DocumentObjectType == DocumentObjectType.Table:
                        tables.append(child_obj)

# Modify the text content of the first paragraph
if paragraphs:
    (Paragraph)(paragraphs[0]).Text = "Spire.Doc for Python is a totally independent Python Word class library which doesn't require Microsoft Office installed on system."

if tables:
    # Reset the cells of the first table
    (Table)(tables[0]).ResetCells(5, 4)

# Save the modified document to a file
doc.SaveToFile("ModifyBodyContentControls.docx", FileFormat.Docx2016)

# Release document resources
doc.Close()
doc.Dispose()

Python: Modify Content Controls in a Word Document

Modify Content Controls within Paragraphs using Python

In Spire.Doc, the object type for content controls within paragraphs is StructureDocumentTagInline. To modify these, you would traverse the Paragraph.ChildObjects collection to locate objects of type StructureDocumentTagInline. Here are the detailed steps:

  • Instantiate a Document object.
  • Load a Word document using the Document.LoadFromFile() method.
  • Get the body of a section in the document via Section.Body.
  • Retrieve the first paragraph of the text body using Body.Paragraphs.get_Item(0).
  • Traverse the collection of child objects within Paragraph.ChildObjects, identifying those that are of type StructureDocumentTagInline.
  • Within the StructureDocumentTagInline.ChildObjects sub-collection, execute modification operations according to the type of each child object.
  • Save the changes back to the Word document using the Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

 # Create a new Document object
doc = Document()

# Load document content from a file
doc.LoadFromFile("Sample2.docx")

# Get the body of the document
body = doc.Sections.get_Item(0).Body

# Get the first paragraph in the body
paragraph = body.Paragraphs.get_Item(0)

# Iterate through child objects in the paragraph
for i in range(paragraph.ChildObjects.Count):
    obj = paragraph.ChildObjects.get_Item(i)

    # Check if the child object is StructureDocumentTagInline
    if obj.DocumentObjectType == DocumentObjectType.StructureDocumentTagInline:
       
       # Convert the child object to StructureDocumentTagInline type
       structure_document_tag_inline = (StructureDocumentTagInline)(obj)

       # Check if the Tag or Alias property is "text1"
       if structure_document_tag_inline.SDTProperties.Tag == "text1":
            
            # Iterate through child objects in the StructureDocumentTagInline object
            for j in range(structure_document_tag_inline.ChildObjects.Count):
                obj2 = structure_document_tag_inline.ChildObjects.get_Item(j)

                # Check if the child object is a TextRange object
                if obj2.DocumentObjectType == DocumentObjectType.TextRange:

                    # Convert the child object to TextRange type
                    range = (TextRange)(obj2)

                    # Set the text content to a specified content
                    range.Text = "97-2003/2007/2010/2013/2016/2019"

       # Check if the Tag or Alias property is "logo1"
       if structure_document_tag_inline.SDTProperties.Tag == "logo1":
            
            # Iterate through child objects in the StructureDocumentTagInline object
            for j in range(structure_document_tag_inline.ChildObjects.Count):
                obj2 = structure_document_tag_inline.ChildObjects.get_Item(j)
               
                # Check if the child object is an image
                if obj2.DocumentObjectType == DocumentObjectType.Picture:

                    # Convert the child object to DocPicture type
                    doc_picture = (DocPicture)(obj2)

                    # Load a specified image
                    doc_picture.LoadImage("DOC-Python.png")

                    # Set the width and height of the image
                    doc_picture.Width = 100
                    doc_picture.Height = 100

# Save the modified document to a new file
doc.SaveToFile("ModifiedContentControlsInParagraph.docx", FileFormat.Docx2016)

# Release resources of the Document object
doc.Close()
doc.Dispose()

Python: Modify Content Controls in a Word Document

Modify Content Controls Wrapping Table Rows using Python

In Spire.Doc, the object type for content controls within table rows is StructureDocumentTagRow. To modify these controls, you need to traverse the Table.ChildObjects collection to find objects of type StructureDocumentTagRow. Here are the detailed steps:

  • Create a Document object.
  • Load a Word document using the Document.LoadFromFile() method.
  • Retrieve the body of a section within the document using Section.Body.
  • Obtain the first table in the text body via Body.Tables.get_Item(0).
  • Traverse the collection of child objects within Table.ChildObjects, identifying those that are of type StructureDocumentTagRow.
  • Access StructureDocumentTagRow.Cells collection to iterate through the cells within this controlled row, and then execute the appropriate modification actions on the cell contents.
  • Lastly, use the Document.SaveToFile() method to persist the changes made to the document.
  • Python
from spire.doc import *
from spire.doc.common import *

# Create a new document object
doc = Document()

# Load the document from a file
doc.LoadFromFile("Sample3.docx")

# Get the body of the document
body = doc.Sections.get_Item(0).Body

# Get the first table
table = body.Tables.get_Item(0)

# Iterate through the child objects in the table
for i in range(table.ChildObjects.Count):
    obj = table.ChildObjects.get_Item(i)

    # Check if the child object is of type StructureDocumentTagRow
    if obj.DocumentObjectType == DocumentObjectType.StructureDocumentTagRow:

        # Convert the child object to a StructureDocumentTagRow object
        structureDocumentTagRow = (StructureDocumentTagRow)(obj)

        # Check if the Tag or Alias property of the StructureDocumentTagRow is "row1"
        if structureDocumentTagRow.SDTProperties.Tag == "row1":

            # Clear the paragraphs in the cell
            structureDocumentTagRow.Cells.get_Item(0).Paragraphs.Clear()

            # Add a paragraph in the cell and set the text
            textRange = structureDocumentTagRow.Cells.get_Item(0).AddParagraph().AppendText("Arts")
            textRange.CharacterFormat.TextColor = Color.get_Blue()
      
# Save the modified document to a file
doc.SaveToFile("ModifiedTableRowContentControl.docx",  FileFormat.Docx2016)

# Release document resources
doc.Close()
doc.Dispose()

Python: Modify Content Controls in a Word Document

Modify Content Controls Wrapping Table Cells using Python

In Spire.Doc, the object type for content controls within table cells is StructureDocumentTagCell. To manipulate these controls, you need to traverse the TableRow.ChildObjects collection to locate objects of type StructureDocumentTagCell. Here are the detailed steps:

  • Create a Document object.
  • Load a Word document using the Document.LoadFromFile() method.
  • Retrieve the body of a section in the document using Section.Body.
  • Obtain the first table in the body using Body.Tables.get_Item(0).
  • Traverse the collection of rows in the table.
  • Within each TableRow, traverse its child objects TableRow.ChildObjects to identify those of type StructureDocumentTagCell.
  • Access StructureDocumentTagCell.Paragraphs collection. This allows you to iterate through the paragraphs within the cell and apply the necessary modification operations to the content.
  • Finally, use the Document.SaveToFile() method to save the modified document.
  • Python
from spire.doc import *
from spire.doc.common import *

# Create a new document object
doc = Document()

# Load the document from a file
doc.LoadFromFile("Sample4.docx")

# Get the body of the document
body = doc.Sections.get_Item(0).Body

# Get the first table in the document
table = body.Tables.get_Item(0)

# Iterate through the rows of the table
for i in range(table.Rows.Count):
    row = table.Rows.get_Item(i)

    # Iterate through the child objects in each row
    for j in range(row.ChildObjects.Count):
        obj = row.ChildObjects.get_Item(j)

        # Check if the child object is a StructureDocumentTagCell
        if obj.DocumentObjectType == DocumentObjectType.StructureDocumentTagCell:

            # Convert the child object to StructureDocumentTagCell type
            structureDocumentTagCell = (StructureDocumentTagCell)(obj)

            # Check if the Tag or Alias property of structureDocumentTagCell is "cell1"
            if structureDocumentTagCell.SDTProperties.Tag == "cell1":
                
                # Clear the paragraphs in the cell
                structureDocumentTagCell.Paragraphs.Clear()

                # Add a new paragraph and add text to it
                textRange = structureDocumentTagCell.AddParagraph().AppendText("92")
                textRange.CharacterFormat.TextColor = Color.get_Blue()

# Save the modified document to a new file
doc.SaveToFile("ModifiedTableCellContentControl.docx", FileFormat.Docx2016)

# Dispose of the document object
doc.Close()
doc.Dispose()

Python: Modify Content Controls in a Word Document

Modify Content Controls within Table Cells using Python

This case demonstrates modifying content controls within paragraphs inside table cells. The process involves navigating to the paragraph collection TableCell.Paragraphs within each cell, then iterating through each paragraph's child objects (Paragraph.ChildObjects) to locate StructureDocumentTagInline objects for modification. Here are the detailed steps:

  • Initiate a Document instance.
  • Use the Document.LoadFromFile() method to load a Word document.
  • Retrieve the body of a section in the document with Section.Body.
  • Obtain the first table in the body via Body.Tables.get_Item(0).
  • Traverse the table rows collection (Table.Rows), engaging with each TableRow object.
  • For each TableRow, navigate its cells collection (TableRow.Cells), entering each TableCell object.
  • Within each TableCell, traverse its paragraph collection (TableCell.Paragraphs), examining each Paragraph object.
  • In each paragraph, traverse its child objects (Paragraph.ChildObjects), identifying StructureDocumentTagInline instances for modification.
  • Within the StructureDocumentTagInline.ChildObjects collection, apply the appropriate edits based on the type of each child object.
  • Finally, utilize Document.SaveToFile() to commit the changes to the document.
  • Python
from spire.doc import *
from spire.doc.common import *

 # Create a new Document object
doc = Document()

# Load document content from file
doc.LoadFromFile("Sample5.docx")

# Get the body of the document
body = doc.Sections.get_Item(0).Body

# Get the first table
table = body.Tables.get_Item(0)

# Iterate through the rows of the table
for r in range(table.Rows.Count):
    row = table.Rows.get_Item(r)
    for c in range(row.Cells.Count):
        cell = row.Cells.get_Item(c)
        for p in range(cell.Paragraphs.Count):
            paragraph = cell.Paragraphs.get_Item(p)
            for i in range(paragraph.ChildObjects.Count):
                obj = paragraph.ChildObjects.get_Item(i)

                # Check if the child object is of type StructureDocumentTagInline
                if obj.DocumentObjectType == DocumentObjectType.StructureDocumentTagInline:

                    # Convert to StructureDocumentTagInline object
                    structure_document_tag_inline = (StructureDocumentTagInline)(obj)

                    # Check if the Tag or Alias property of StructureDocumentTagInline is "test1"
                    if structure_document_tag_inline.SDTProperties.Tag == "test1":

                        # Iterate through the child objects of StructureDocumentTagInline
                        for j in range(structure_document_tag_inline.ChildObjects.Count):
                            obj2 = structure_document_tag_inline.ChildObjects.get_Item(j)

                            # Check if the child object is of type TextRange
                            if obj2.DocumentObjectType == DocumentObjectType.TextRange:

                                # Convert to TextRange object
                                textRange = (TextRange)(obj2)

                                # Set the text content
                                textRange.Text = "89"

                                # Set text color
                                textRange.CharacterFormat.TextColor = Color.get_Blue()

# Save the modified document to a new file
doc.SaveToFile("ModifiedContentControlInParagraphOfTableCell.docx", FileFormat.Docx2016)

# Dispose of the Document object resources
doc.Close()
doc.Dispose()

Python: Modify Content Controls in a Word Document

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Published in Form Field
Tuesday, 09 January 2024 01:13

Python: Create a Fillable Form in Word

Creating a fillable form in Word allows you to design a document that can be easily completed and customized by others. Whether you need to collect information, gather feedback, or create an interactive document, fillable forms provide a convenient way to capture data electronically. By adding various elements such as text fields, checkboxes, dropdown menus, and more, you can tailor the form to your specific requirements.

To create a fillable form in Word, you probably need to use the following tools.

  • Content Controls: The areas where users input information in a form.
  • Tables: Tables are used in forms to align text and form fields, and to create borders and boxes.
  • Protection: Allows users to populate fields but not to make changes to the rest of the document.

In Word, content controls serve as containers for structured documents, allowing users to organize content within a document. Word 2013 provides ten types of content controls. This article introduces how to create a fillable form in Word that includes the following seven commonly-used content controls using Spire.Doc for Python.

Content Control Description
Plain Text A text field limited to plain text, so no formatting can be included.
Rich Text A text field that can contain formatted text or other items, such as tables, pictures, or other content controls.
Picture Accepts a single picture.
Drop-Down List A drop-down list displays a predefined list of items for the user to choose from.
Combo Box A combo box enables users to select a predefined value in a list or type their own value in the text box of the control.
Check Box A check box provides a graphical widget that allows the user to make a binary choice: yes (checked) or no (not checked).
Date Picker Contains a calendar control from which the user can select a date.

Install Spire.Doc for Python

This scenario requires Spire.Doc for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.

pip install Spire.Doc

If you are unsure how to install, please refer to this tutorial: How to Install Spire.Doc for Python on Windows

Create a Fillable Form in Word in Python

Spire.Doc for Python offers the StructureDocumentTagInline class, which is utilized to generate structured document tags within a paragraph. By utilizing the SDTProperties property and SDTContent property of this class, one can define the properties and content of the current structured document tag. Below are the step-by-step instructions for creating a fill form in a Word document in Python.

  • Create a Document object.
  • Add a section using Document.AddSection() method.
  • Add a table using Section.AddTable() method.
  • Add a paragraph to a specific table cell using TableCell.AddParagraph() method.
  • Create an instance of StructureDocumentTagInline class, and add it to the paragraph as a child object using Paragraph.ChildObjects.Add() method.
  • Specify the type, content and other attributes of the structured document tag through the SDTProperties property and the SDTContent property of the StructureDocumentTagInline object.
  • Prevent users from editing content outside form fields using Document.Protect() method.
  • Save the document using Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

# Create a Document object
doc = Document()

# Add a section
section = doc.AddSection()

# Add a table
table = section.AddTable(True)
table.ResetCells(7, 2)
table.SetColumnWidth(0, 120, CellWidthType.Point)
table.SetColumnWidth(1, 350, CellWidthType.Point)

# Add text to the cells of the first column
paragraph = table.Rows[0].Cells[0].AddParagraph()
paragraph.AppendText("Name")
paragraph = table.Rows[1].Cells[0].AddParagraph()
paragraph.AppendText("Profile")
paragraph = table.Rows[2].Cells[0].AddParagraph()
paragraph.AppendText("Photo")
paragraph = table.Rows[3].Cells[0].AddParagraph()
paragraph.AppendText("Country")
paragraph = table.Rows[4].Cells[0].AddParagraph()
paragraph.AppendText("Hobbies")
paragraph = table.Rows[5].Cells[0].AddParagraph()
paragraph.AppendText("Birthday")
paragraph = table.Rows[6].Cells[0].AddParagraph()
paragraph.AppendText("Sex")

# Add a plain text content control to the cell (0,1)
paragraph = table.Rows[0].Cells[1].AddParagraph()
sdt = StructureDocumentTagInline(doc)
paragraph.ChildObjects.Add(sdt)
sdt.SDTProperties.SDTType = SdtType.Text
sdt.SDTProperties.Alias = "Plain Text"
sdt.SDTProperties.Tag = "Plain Text"
sdt.SDTProperties.IsShowingPlaceHolder = True
text = SdtText(True)
text.IsMultiline = False
sdt.SDTProperties.ControlProperties = text
textRange = TextRange(doc)
textRange.Text = "your name here"
sdt.SDTContent.ChildObjects.Add(textRange)

# Add a rich text content control to the cell (1,1)
paragraph = table.Rows[1].Cells[1].AddParagraph()
sdt = StructureDocumentTagInline(doc)
paragraph.ChildObjects.Add(sdt)
sdt.SDTProperties.SDTType = SdtType.RichText
sdt.SDTProperties.Alias = "Rich Text"
sdt.SDTProperties.Tag = "Rich Text"
sdt.SDTProperties.IsShowingPlaceHolder = True
text = SdtText(True)
text.IsMultiline = False
sdt.SDTProperties.ControlProperties = text
textRange = TextRange(doc)
textRange.Text = "brief introduction of yourself"
sdt.SDTContent.ChildObjects.Add(textRange )

# Add a picture content control to the cell (2,1)
paragraph = table.Rows[2].Cells[1].AddParagraph()
sdt = StructureDocumentTagInline(doc)
paragraph.ChildObjects.Add(sdt)
sdt.SDTProperties.SDTType = SdtType.Picture
sdt.SDTProperties.Alias = "Picture"
sdt.SDTProperties.Tag = "Picture"
sdtPicture = SdtPicture(True) 
sdt.SDTProperties.ControlProperties = sdtPicture
pic = DocPicture(doc)  
pic.LoadImage("C:\\Users\\Administrator\\Desktop\\placeHolder.png")   
sdt.SDTContent.ChildObjects.Add(pic)

# Add a dropdown list content control to the cell (3,1)
paragraph = table.Rows[3].Cells[1].AddParagraph();
sdt = StructureDocumentTagInline(doc)
sdt.SDTProperties.SDTType = SdtType.DropDownList
sdt.SDTProperties.Alias = "Dropdown List"
sdt.SDTProperties.Tag = "Dropdown List"
paragraph.ChildObjects.Add(sdt)
stdList = SdtDropDownList()
stdList.ListItems.Add(SdtListItem("USA", "1"))
stdList.ListItems.Add(SdtListItem("China", "2"))
stdList.ListItems.Add(SdtListItem("Briza", "3"))
stdList.ListItems.Add(SdtListItem("Austrilia", "4"))
sdt.SDTProperties.ControlProperties = stdList;
textRange = TextRange(doc)
textRange .Text = stdList.ListItems[0].DisplayText
sdt.SDTContent.ChildObjects.Add(textRange )

# Add two check box content controls to the cell (4,1)
paragraph = table.Rows[4].Cells[1].AddParagraph()
sdt = StructureDocumentTagInline(doc)
paragraph.ChildObjects.Add(sdt)
sdt.SDTProperties.SDTType = SdtType.CheckBox
sdtCheckBox = SdtCheckBox()
sdt.SDTProperties.ControlProperties = sdtCheckBox
textRange = TextRange(doc)
sdt.ChildObjects.Add(textRange)
sdtCheckBox.Checked = False
paragraph.AppendText(" Movie")

paragraph = table.Rows[4].Cells[1].AddParagraph();
sdt = StructureDocumentTagInline(doc)
paragraph.ChildObjects.Add(sdt)
sdt.SDTProperties.SDTType = SdtType.CheckBox
sdtCheckBox = SdtCheckBox()
sdt.SDTProperties.ControlProperties = sdtCheckBox
textRange = TextRange(doc)
sdt.ChildObjects.Add(textRange)
sdtCheckBox.Checked = False
paragraph.AppendText(" Game")

# Add a date picker content control to the cell (5,1)
paragraph = table.Rows[5].Cells[1].AddParagraph()
sdt = StructureDocumentTagInline(doc)
paragraph.ChildObjects.Add(sdt)
sdt.SDTProperties.SDTType = SdtType.DatePicker
sdt.SDTProperties.Alias = "Date Picker"
sdt.SDTProperties.Tag = "Date Picker"
stdDate = SdtDate()
stdDate.CalendarType = CalendarType.Default
stdDate.DateFormat = "yyyy.MM.dd"
stdDate.FullDate = DateTime.get_Now()
sdt.SDTProperties.ControlProperties = stdDate
textRange = TextRange(doc)
textRange.Text = "your birth date"
sdt.SDTContent.ChildObjects.Add(textRange)

# Add a combo box content control to the cell (6,1)
paragraph = table.Rows[6].Cells[1].AddParagraph()
sdt = StructureDocumentTagInline(doc)
paragraph.ChildObjects.Add(sdt)
sdt.SDTProperties.SDTType = SdtType.ComboBox
sdt.SDTProperties.Alias = "Combo Box"
sdt.SDTProperties.Tag = "Combo Box"
stdComboBox = SdtComboBox()
stdComboBox.ListItems.Add(SdtListItem("Male"))
stdComboBox.ListItems.Add(SdtListItem("Female"))
sdt.SDTProperties.ControlProperties = stdComboBox
textRange = TextRange(doc)
textRange.Text = stdComboBox.ListItems[0].DisplayText
sdt.SDTContent.ChildObjects.Add(textRange)

# Allow users to edit the form fields only
doc.Protect(ProtectionType.AllowOnlyFormFields, "permission-psd")

# Save to file
doc.SaveToFile("output/Form.docx", FileFormat.Docx2013)

Python: Create a Fillable Form in Word

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Published in Form Field