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.
- Modify Content Controls in the Body using Python
- Modify Content Controls within Paragraphs using Python
- Modify Content Controls Wrapping Table Rows using Python
- Modify Content Controls Wrapping Table Cells using Python
- Modify Content Controls within Table Cells using Python
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()
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()
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()
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()
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()
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.