Python: Add or Remove Captions in Word Documents

Captions play a significant role in Word documents by serving as markers, explanations, navigation aids, and accessibility features. They are crucial elements for creating professional, accurate, and user-friendly documents. Captions help improve the readability, usability, and accessibility of the document and are essential for understanding and effectively processing the document's content. This article will explain how to use Spire.Doc for Python to add or remove captions in Word documents using Python programs.

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

Add Image Captions to a Word document in Python

Spire.Doc for Python provides a convenient method to add captions to images. Simply call the DocPicture.AddCaption(self, name: str, numberingFormat: 'CaptionNumberingFormat', captionPosition: 'CaptionPosition') method to add a caption for the image. The detailed steps are as follows:

  • Create an object of the Document class.
  • Use the Document.AddSection() method to add a section.
  • Add a paragraph using Section.AddParagraph() method.
  • Use the Paragraph.AppendPicture(self ,imgFile:str) method to add a DocPicture image object to the paragraph.
  • Use the DocPicture.AddCaption(self ,name:str,numberingFormat:'CaptionNumberingFormat',captionPosition:'CaptionPosition') method to add a caption with numbering format as CaptionNumberingFormat.Number.
  • Set the Document.IsUpdateFields property to true to update all fields.
  • Use the Document.SaveToFile() method to save the resulting document.
  • Python
from spire.doc import *
from spire.doc.common import *

# Create a Word document object
document = Document()

# Add a section
section = document.AddSection()

# Add a new paragraph and add an image to it
pictureParagraphCaption = section.AddParagraph()
pictureParagraphCaption.Format.AfterSpacing = 10
pic1 = pictureParagraphCaption.AppendPicture("Data\\1.png")
pic1.Height = 100
pic1.Width = 100

# Add a caption to the image
format = CaptionNumberingFormat.Number
pic1.AddCaption("Image", format, CaptionPosition.BelowItem)

# Add another new paragraph and add an image to it
pictureParagraphCaption = section.AddParagraph()
pic2 = pictureParagraphCaption.AppendPicture("Data\\2.png")
pic2.Height = 100
pic2.Width = 100

# Add a caption to the image
pic2.AddCaption("Image", format, CaptionPosition.BelowItem)

# Update all fields in the document
document.IsUpdateFields = True

# Save the document as a docx file
result = "AddImageCaption.docx"
document.SaveToFile(result, FileFormat.Docx2016)

# Close the document object and release resources
document.Close()
document.Dispose()

Python: Add or Remove Captions in Word documents

Add Table Captions to a Word document in Python

To facilitate the addition of captions to tables, Spire.Doc for Python also provides a convenient method similar to adding captions to images. You can use the Table.AddCaption(self, name:str, format:'CaptionNumberingFormat', captionPosition:'CaptionPosition') method to create a caption for the table. The following are the detailed steps:

  • Create an object of the Document class.
  • Use the Document.AddSection() method to add a section.
  • Create a Table object and add it to the specified section in the document.
  • Use the Table.ResetCells(self ,rowsNum:int,columnsNum:int) method to set the number of rows and columns in the table.
  • Add a caption to the table using the Table.AddCaption(self ,name:str,format:'CaptionNumberingFormat',captionPosition:'CaptionPosition') method, specifying the caption numbering format as CaptionNumberingFormat.Number.
  • Set the Document.IsUpdateFields property to true to update all fields.
  • Use the Document.SaveToFile() method to save the resulting document.
  • Python
from spire.doc import *
from spire.doc.common import *

# Create a Word document object
document = Document()

# Add a section
section = document.AddSection()

# Add a table
tableCaption = section.AddTable(True)
tableCaption.ResetCells(3, 2)

# Add a caption to the table
tableCaption.AddCaption("Table", CaptionNumberingFormat.Number, CaptionPosition.BelowItem)

# Add another table and caption to it
tableCaption = section.AddTable(True)
tableCaption.ResetCells(2, 3)
tableCaption.AddCaption("Table", CaptionNumberingFormat.Number, CaptionPosition.BelowItem)

# Update all fields in the document
document.IsUpdateFields = True

# Save the document as a docx file
result = "AddTableCaption.docx"
document.SaveToFile(result, FileFormat.Docx2016)

# Close the document object and release resources
document.Close()
document.Dispose()

Python: Add or Remove Captions in Word documents

Remove Captions from a Word document in Python

Spire.Doc for Python also supports removing captions from Word documents. Here are the detailed steps:

  • Create an object of the Document class.
  • Use the Document.LoadFromFile() method to load a Word document.
  • Create a custom method, named detect_caption_paragraph(paragraph), to determine if a paragraph contains a caption.
  • Iterate through all the Paragraph objects in the document using a loop and utilize the custom method, detect_caption_paragraph(paragraph), to identify and delete paragraphs that contain captions.
  • Use the Document.SaveToFile() method to save the resulting document.
  • Python
from spire.doc import *
from spire.doc.common import *

# Method to detect if a paragraph is a caption paragraph
def detect_caption_paragraph(paragraph):
    tag = False
    field = None
    
# Iterate through the child objects in the paragraph
    for i in range(len(paragraph.ChildObjects)):
        if paragraph.ChildObjects[i].DocumentObjectType == DocumentObjectType.Field:
            # Check if the child object is of Field type
            field = paragraph.ChildObjects[i]
            if field.Type == FieldType.FieldSequence:
                # Check if the Field type is FieldSequence, indicating a caption field type
                return True

    return tag

# Create a Word document object
document = Document()
# Load the sample.docx file
document.LoadFromFile("Data/sample.docx")

# Iterate through all sections
for i in range(len(document.Sections)):
    section = document.Sections.get_Item(i)
    
    # Iterate through paragraphs in reverse order within the section
    for j in range(len(section.Body.Paragraphs) - 1, -1, -1):
        # Check if the paragraph is a caption paragraph
        if detect_caption_paragraph(section.Body.Paragraphs[j]):
            # If it's a caption paragraph, remove it
            section.Body.Paragraphs.RemoveAt(j)
            
# Save the document after removing captions           
result = "DeleteCaptions.docx"
document.SaveToFile(result, FileFormat.Docx2016)

# Close the document object and release resources
document.Close()
document.Dispose()

Python: Add or Remove Captions in Word documents

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.