Programmatic editing of Word documents involves using code to alter or modify the contents of these documents. This approach enables automation and customization, making it particularly advantageous for handling large document collections. Through the use of Spire.Doc library, developers can perform a wide range of operations, including text manipulation, formatting changes, and the addition of images or tables.
The following sections will demonstrate how to edit or modify a Word document in Python using Spire.Doc for Python.
- Modify Text in a Word Document
- Change Formatting of Text in a Word Document
- Add New Elements to a Word Document
- Remove Paragraphs from a Word Document
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
Modify Text in a Word Document in Python
In order to alter the content of a paragraph, the initial step is to obtain the desired paragraph from a specific section through the use of the Section.Paragraphs[index] property. Following this, you can replace the existing text with the new content by assigning it to the Paragraph.Text property of the chosen paragraph.
Here are the steps to edit text in a Word document with Python:
- Create a Document object.
- Load a Word file from the given file path.
- Get a specific section using Document.Sections[index] property.
- Get a specific paragraph using Section.Paragraphs[index] property.
- Reset the text of the paragraph using Paragraph.Text property.
- Save the updated document to a different Word file.
- Python
from spire.doc import * from spire.doc.common import * # Create a Document object document = Document() # Load an existing Word file document.LoadFromFile("C:\\Users\\Administrator\\Desktop\\input.docx"); # Get a specific section section = document.Sections[0] # Get a specific paragraph paragraph = section.Paragraphs[0] # Modify the text of the paragraph paragraph.Text = "The text has been modified" # Save the document to a different Word file document.SaveToFile("output/ModifyText.docx", FileFormat.Docx) # Dispose resource document.Dispose()
Change Formatting of Text in a Word Document in Python
To alter the text appearance of a particular paragraph, you first need to obtain the specified paragraph. Next, go through its child objects to find the individual text ranges. The formatting of each text range can then be updated using the TextRange.CharacterFormat property.
The steps to change text formatting in a Word document are as follows:
- Create a Document object.
- Load a Word file from the given file path.
- Get a specific section using Document.Sections[index] property.
- Get a specific paragraph using Section.Paragraphs[index] property.
- Iterate through the child objects in the paragraph.
- Determine if a child object is a text range.
- Get a specific text range.
- Reset the text formatting using TextRange.CharacterFormat property.
- Save the updated document to a different Word file.
- Python
from spire.doc import * from spire.doc.common import * # Create an object of Document doc = Document() # Load a Word document doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\input.docx") # Get a specific section section = doc.Sections[0] # Get a specific paragraph paragraph = section.Paragraphs[1]; # Iterate through the child objects in the paragraph for i in range(paragraph.ChildObjects.Count): # Determine if a child object is text range if isinstance(paragraph.ChildObjects[i], TextRange): # Get a specific text range textRange = paragraph.ChildObjects[i] # Reset font name textRange.CharacterFormat.FontName = "Corbel Light" # Reset font size textRange.CharacterFormat.FontSize = 11.0 # Reset text color textRange.CharacterFormat.TextColor = Color.get_Blue() # Apply italic to the text range textRange.CharacterFormat.Italic = True # Save the document to a different Word file doc.SaveToFile("output/ChangeFormatting.docx", FileFormat.Docx2019) # Dispose resource doc.Dispose()
Add New Elements to a Word Document in Python
In a Word document, most elements—such as text, images, lists, and charts—are fundamentally organized around the concept of a paragraph. To insert a new paragraph into a specific section, use the Section.AddParagraph() method.
After creating the new paragraph, you can add various elements to it by leveraging the methods and properties of the Paragraph object.
The steps to add new elements (text and images) to a Word document are as follows:
- Create a Document object.
- Load a Word file from the given file path.
- Get a specific section through Document.Sections[index] property.
- Add a paragraph to the section using Section.AddParagraph() method.
- Add text to the paragraph using Paragraph.AppendText() method.
- Add an image to the paragraph using Paragraph.AppendPicture() method.
- Save the updated document to a different Word file.
- Python
from spire.doc import * from spire.doc.common import * # Create an object of Document doc = Document() # Load a Word document doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Input.docx") # Get the last section lastSection = doc.LastSection # Add a paragraph to the section paragraph = lastSection.AddParagraph() # Add an image to the paragraph picture = paragraph.AppendPicture("C:\\Users\\Administrator\\Desktop\\logo.png"); # Set text wrap style picture.TextWrappingStyle = TextWrappingStyle.TopAndBottom # Add text to the paragraph paragraph.AppendText("This text and the image above are added by Spire.Doc for Python.") # Create a paragraph style style = ParagraphStyle(doc) style.Name = "FontStyle" style.CharacterFormat.FontName = "Times New Roman" style.CharacterFormat.FontSize = 12 doc.Styles.Add(style) # Apply the style to the paragraph paragraph.ApplyStyle(style.Name) # Save the document to a different Word file doc.SaveToFile("output/AddNewElements.docx", FileFormat.Docx2019) # Dispose resource doc.Dispose()
Remove Paragraphs from a Word Document in Python
To eliminate a specific paragraph from a document, simply invoke the ParagraphCollection.RemoveAt() method and supply the index of the paragraph you intend to delete.
The steps to remove paragraphs from a Word document are as follows:
- Create a Document object.
- Load a Word file from the given file path.
- Get a specific section through Document.Sections[index] property.
- Remove a specific paragraph from the section using Section.Paragraphs.RemoveAt() method.
- Save the updated document to a different Word file.
- Python
from spire.doc import * from spire.doc.common import * # Create an object of Document doc = Document() # Load a Word document doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\input.docx") # Get a specific section section = doc.Sections[0] # Remove a specific paragraph section.Paragraphs.RemoveAt(0) # Save the document to a different Word file doc.SaveToFile("output/RemoveParagraph.docx", FileFormat.Docx); # Dispose resource 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.