Spire.Office Knowledgebase Page 45 | E-iceblue

Excel has been a widely used tool for data organization and analysis for many years. Over time, Microsoft has introduced different file formats for storing Excel data, the most common being the older XLS format and the more modern XLSX format.

The XLS format, introduced in the late 1990s, had certain limitations, such as a file size limit of 65,536 rows and 256 columns, and a maximum of 65,000 unique styles. The XLSX format, introduced in 2007, addressed these limitations by allowing for larger file sizes, more rows and columns, and expanded style capabilities. While XLSX is now the standard format, there are still many existing XLS files that need to be accessed and used, which makes the ability to convert between these formats an essential skill. In this article, we will explain how to convert Excel XLS to XLSX and vice versa in Python using Spire.XLS for Python.

Install Spire.XLS for Python

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

pip install Spire.XLS

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

Convert XLSX to XLS in Python

To convert an XLSX file to XLS format, you can use the Workbook.SaveToFile(fileName, ExcelVersion.Version97to2003) method. The ExcelVersion.Version97to2003 parameter specifies that the workbook should be saved in the Excel 97-2003 (XLS) format. The detailed steps are as follows.

  • Create an object of the Workbook class.
  • Load an XLSX file using the Workbook.LoadFromFile() method.
  • Save the XLSX file to XLS format using the Workbook.SaveToFile(fileName, ExcelVersion.Version97to2003) method.
  • Python
from spire.xls import *
from spire.xls.common import *

# Specify the input and output file paths
inputFile = "Sample1.xlsx"
outputFile = "XlsxToXls.xls"

# Create a Workbook object
workbook = Workbook()
# Load the XLSX file
workbook.LoadFromFile(inputFile)

# Save the XLSX file to XLS format
workbook.SaveToFile(outputFile, ExcelVersion.Version97to2003)
workbook.Dispose()

Python: Convert Excel XLS to XLSX and Vice Versa

Convert XLS to XLSX in Python

To convert an XLS file to XLSX format, you need to specify the target Excel version to a version higher than 97-2003, such as 2007 (ExcelVersion.Version2007), 2010 (ExcelVersion.Version2010), 2013 (ExcelVersion.Version2013), or 2016 (ExcelVersion.Version2016). The detailed steps are as follows.

  • Create an object of the Workbook class.
  • Load an XLS file using the Workbook.LoadFromFile() method.
  • Save the XLS file to an Excel 2016 (XLSX) file using the Workbook.SaveToFile(fileName, ExcelVersion.Version2016) method.
  • Python
from spire.xls import *
from spire.xls.common import *

# Specify the input and output file paths
inputFile = "Sample2.xls"
outputFile = "XlsToXlsx.xlsx"

# Create a Workbook object
workbook = Workbook()
# Load the XLS file
workbook.LoadFromFile(inputFile)

# Save the XLS file to XLSX format
workbook.SaveToFile(outputFile, ExcelVersion.Version2016)
workbook.Dispose()

Python: Convert Excel XLS to XLSX and Vice Versa

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.

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.

Creating a table of contents in a Word document significantly enhances its navigability and readability. It serves as a road map for the document, enabling readers to quickly overview the structure and grasp the content framework. This feature facilitates easy navigation for users to jump to any section within the document, which is particularly valuable for lengthy reports, papers, or manuals. It not only saves readers time in locating information but also augments the professionalism of the document and enhances the user experience. Moreover, a table of contents is easy to maintain and update; following any restructuring of the document, it can be swiftly revised to reflect the latest content organization, ensuring coherence and accuracy throughout the document. This article will demonstrate how to use Spire.Doc for Python to create a table of contents in a newly created Word document 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 Windows through the following pip command.

pip install Spire.Doc

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

Python Create a Table Of Contents Using Heading Styles

Creating a table of contents using heading styles is a default method in Word documents to automatically generate a table of contents by utilizing different levels of heading styles to mark titles and sub-titles within the document, followed by leveraging Word's table of contents feature to automatically populate the contents. Here are the detailed steps:

  • Create a Document object.
  • Add a section using the Document.AddSection() method.
  • Add a paragraph using the Section.AddParagraph() method.
  • Create a table of contents object using the Paragraph.AppendTOC(int lowerLevel, int upperLevel) method.
  • Create a CharacterFormat object and set the font.
  • Apply a heading style to the paragraph using the Paragraph.ApplyStyle(BuiltinStyle.Heading1) method.
  • Add text content using the Paragraph.AppendText() method.
  • Apply character formatting to the text using the TextRange.ApplyCharacterFormat() method.
  • Update the table of contents using the Document.UpdateTableOfContents() method.
  • Save the document using the Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

# Create a new document object
doc = Document()

# Add a section to the document
section = doc.AddSection()

# Append a Table of Contents (TOC) paragraph
TOC_paragraph = section.AddParagraph()
TOC_paragraph.AppendTOC(1, 3)

# Create and set character format objects for font
character_format1 = CharacterFormat(doc)
character_format1.FontName = "Microsoft YaHei"

character_format2 = CharacterFormat(doc)
character_format2.FontName = "Microsoft YaHei"
character_format2.FontSize = 12

# Add a paragraph with Heading 1 style
paragraph = section.Body.AddParagraph()
paragraph.ApplyStyle(BuiltinStyle.Heading1)

# Add text and apply character formatting
text_range1 = paragraph.AppendText("Overview")
text_range1.ApplyCharacterFormat(character_format1)

# Insert normal content
paragraph = section.Body.AddParagraph()
text_range2 = paragraph.AppendText("Spire.Doc for Python is a professional Python Word development component that enables developers to easily integrate Word document creation, reading, editing, and conversion functionalities into their own Python applications. As a completely standalone component, Spire.Doc for Python does not require the installation of Microsoft Word on the runtime environment.")

# Add a paragraph with Heading 1 style
paragraph = section.Body.AddParagraph()
paragraph.ApplyStyle(BuiltinStyle.Heading1)
text_range1 = paragraph.AppendText("Main Functions")
text_range1.ApplyCharacterFormat(character_format1)

# Add a paragraph with Heading 2 style
paragraph = section.Body.AddParagraph()
paragraph.ApplyStyle(BuiltinStyle.Heading2)
textRange1 = paragraph.AppendText("Only Spire.Doc, No Microsoft Office Automation")
textRange1.ApplyCharacterFormat(character_format1)

# Add regular content
paragraph = section.Body.AddParagraph()
textRange2 = paragraph.AppendText("Spire.Doc for Python is a totally independent Python Word class library which doesn't require Microsoft Office installed on system. Microsoft Office Automation is proved to be unstable, slow and not scalable to produce MS Word documents. Spire.Doc for Python is many times faster than Microsoft Word Automation and with much better stability and scalability.")
textRange2.ApplyCharacterFormat(character_format2)

# Add a paragraph with Heading 3 style
paragraph = section.Body.AddParagraph()
paragraph.ApplyStyle(BuiltinStyle.Heading3)
textRange1 = paragraph.AppendText("Word Versions")
textRange1.ApplyCharacterFormat(character_format1)
paragraph = section.Body.AddParagraph()
textRange2 = paragraph.AppendText("Word97-03  Word2007  Word2010  Word2013  Word2016  Word2019")
textRange2.ApplyCharacterFormat(character_format2)

# Add a paragraph with Heading 2 style
paragraph = section.Body.AddParagraph()
paragraph.ApplyStyle(BuiltinStyle.Heading2)
textRange1 = paragraph.AppendText("Convert File Documents with High Quality")
textRange1.ApplyCharacterFormat(character_format1)

# Add regular content
paragraph = section.Body.AddParagraph()
textRange2 = paragraph.AppendText("By using Spire.Doc for Python, users can save Word Doc/Docx to stream, save as web response and convert Word Doc/Docx to XML, Markdown, RTF, EMF, TXT, XPS, EPUB, HTML, SVG, ODT and vice versa. Spire.Doc for Python also supports to convert Word Doc/Docx to PDF and HTML to image.")
textRange2.ApplyCharacterFormat(character_format2)

# Add a paragraph with Heading 2 style
paragraph = section.Body.AddParagraph()
paragraph.ApplyStyle(BuiltinStyle.Heading2)
extRange1 = paragraph.AppendText("Other Technical Features")
textRange1.ApplyCharacterFormat(character_format1)

# Add regular content
paragraph = section.Body.AddParagraph()
textRange2 = paragraph.AppendText("By using Spire.Doc for Python, developers can build any type of a 64-bit Python application to create and handle Word documents.")
textRange2.ApplyCharacterFormat(character_format2)

# Update the table of contents
doc.UpdateTableOfContents()

# Save the document
doc.SaveToFile("CreateTOCUsingHeadingStyles.docx", FileFormat.Docx2016)

# Release resources
doc.Dispose()

Python: Create a Table Of Contents for a Newly Created Word Document

Python Create a Table Of Contents Using Outline Level Styles

In a Word document, you can create a table of contents using outline level styles. You can assign an outline level to a paragraph using the ParagraphFormat.OutlineLevel property. Afterwards, you apply these outline levels to the rules for generating the table of contents using the TableOfContent.SetTOCLevelStyle() method. Here's a detailed steps:

  • Create a Document object.
  • Add a section using the Document.AddSection() method.
  • Create a ParagraphStyle object and set the outline level using ParagraphStyle.ParagraphFormat.OutlineLevel = OutlineLevel.Level1.
  • Add the created ParagraphStyle object to the document using the Document.Styles.Add() method.
  • Add a paragraph using the Section.AddParagraph() method.
  • Create a table of contents object using the Paragraph.AppendTOC(int lowerLevel, int upperLevel) method.
  • Set the default setting for creating the table of contents with heading styles to False, TableOfContent.UseHeadingStyles = false.
  • Apply the outline level style to the table of contents rules using the TableOfContent.SetTOCLevelStyle(int levelNumber, string styleName) method.
  • Create a CharacterFormat object and set the font.
  • Apply the style to the paragraph using the Paragraph.ApplyStyle(ParagraphStyle.Name) method.
  • Add text content using the Paragraph.AppendText() method.
  • Apply character formatting to the text using the TextRange.ApplyCharacterFormat() method.
  • Update the table of contents using the Document.UpdateTableOfContents() method.
  • Save the document using the Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

# Create a document object
doc = Document()

# Add a section to the document
section = doc.AddSection()

# Define Outline Level 1
titleStyle1 = ParagraphStyle(doc)
titleStyle1.Name = "T1S"
titleStyle1.ParagraphFormat.OutlineLevel = OutlineLevel.Level1
titleStyle1.CharacterFormat.Bold = True
titleStyle1.CharacterFormat.FontName = "Microsoft YaHei"
titleStyle1.CharacterFormat.FontSize = 18
titleStyle1.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Left
doc.Styles.Add(titleStyle1)

# Define Outline Level 2
titleStyle2 = ParagraphStyle(doc)
titleStyle2.Name = "T2S"
titleStyle2.ParagraphFormat.OutlineLevel = OutlineLevel.Level2
titleStyle2.CharacterFormat.Bold = True
titleStyle2.CharacterFormat.FontName = "Microsoft YaHei"
titleStyle2.CharacterFormat.FontSize = 16
titleStyle2.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Left
doc.Styles.Add(titleStyle2)

# Define Outline Level 3
titleStyle3 = ParagraphStyle(doc)
titleStyle3.Name = "T3S"
titleStyle3.ParagraphFormat.OutlineLevel = OutlineLevel.Level3
titleStyle3.CharacterFormat.Bold = True
titleStyle3.CharacterFormat.FontName = "Microsoft YaHei"
titleStyle3.CharacterFormat.FontSize = 14
titleStyle3.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Left
doc.Styles.Add(titleStyle3)

# Add a paragraph
TOCparagraph = section.AddParagraph()
toc = TOCparagraph.AppendTOC(1, 3)
toc.UseHeadingStyles = False
toc.UseHyperlinks = True
toc.UseTableEntryFields = False
toc.RightAlignPageNumbers = True
toc.SetTOCLevelStyle(1, titleStyle1.Name)
toc.SetTOCLevelStyle(2, titleStyle2.Name)
toc.SetTOCLevelStyle(3, titleStyle3.Name)

# Define character format
characterFormat = CharacterFormat(doc)
characterFormat.FontName = "Microsoft YaHei"
characterFormat.FontSize = 12

# Add a paragraph and apply outline level style 1
paragraph = section.Body.AddParagraph()
paragraph.ApplyStyle(titleStyle1.Name)
paragraph.AppendText("Overview")

# Add a paragraph and set the text content
paragraph = section.Body.AddParagraph()
textRange = paragraph.AppendText("Spire.Doc for Python is a professional Word Python API specifically designed for developers to create, read, write, convert, and compare Word documents with fast and high-quality performance.")
textRange.ApplyCharacterFormat(characterFormat)

# Add a paragraph and apply outline level style 1
paragraph = section.Body.AddParagraph()
paragraph.ApplyStyle(titleStyle1.Name)
paragraph.AppendText("Main Functions")

# Add a paragraph and apply outline level style 2
paragraph = section.Body.AddParagraph()
paragraph.ApplyStyle(titleStyle2.Name)
paragraph.AppendText("Only Spire.Doc, No Microsoft Office Automation")

# Add a paragraph and set the text content
paragraph = section.Body.AddParagraph()
textRange = paragraph.AppendText("Spire.Doc for Python is a totally independent Python Word class library which doesn't require Microsoft Office installed on system. Microsoft Office Automation is proved to be unstable, slow and not scalable to produce MS Word documents. Spire.Doc for Python is many times faster than Microsoft Word Automation and with much better stability and scalability.")
textRange.ApplyCharacterFormat(characterFormat)

# Add a paragraph and apply outline level style 3
paragraph = section.Body.AddParagraph()
paragraph.ApplyStyle(titleStyle3.Name)
paragraph.AppendText("Word Versions")

# Add a paragraph and set the text content
paragraph = section.Body.AddParagraph()
textRange = paragraph.AppendText("Word97-03  Word2007  Word2010  Word2013  Word2016  Word2019")
textRange.ApplyCharacterFormat(characterFormat)

# Add a paragraph and apply outline level style 2
paragraph = section.Body.AddParagraph()
paragraph.ApplyStyle(titleStyle2.Name)
paragraph.AppendText("Convert File Documents with High Quality")

# Add a paragraph and set the text content
paragraph = section.Body.AddParagraph()
textRange = paragraph.AppendText("By using Spire.Doc for Python, users can save Word Doc/Docx to stream, save as web response and convert Word Doc/Docx to XML, RTF, EMF, TXT, XPS, EPUB, HTML, SVG, ODT and vice versa. Spire.Doc for Python also supports to convert Word Doc/Docx to PDF and HTML to image.")
textRange.ApplyCharacterFormat(characterFormat)

# Add a paragraph and apply outline level style 2
paragraph = section.Body.AddParagraph()
paragraph.ApplyStyle(titleStyle2.Name)
paragraph.AppendText("Other Technical Features")

# Add a paragraph and set the text content
paragraph = section.Body.AddParagraph()
textRange = paragraph.AppendText("By using Spire.Doc for Python, developers can build any type of a 64-bit Python application to create and handle Word documents.")
textRange.ApplyCharacterFormat(characterFormat)

# Update the table of contents
doc.UpdateTableOfContents()

# Save the document
doc.SaveToFile("CreateTOCUsingOutlineStyles.docx", FileFormat.Docx2016)

# Release resources
doc.Dispose()

Python: Create a Table Of Contents for a Newly Created Word Document

Python Create a Table Of Contents Using Image Captions

Using the Spire.Doc library, you can create a table of contents based on image captions by employing the TableOfContent(Document, "\\h \\z \\c \"Picture\"") method. Below are the detailed steps:

  • Create a Document object.
  • Add a section using the Document.AddSection() method.
  • Create a table of content object with tocForImage = new TableOfContent(Document, " \\h \\z \\c \"Picture\"") and specify the style of the table of contents.
  • Add a paragraph using the Section.AddParagraph() method.
  • Add the table of content object to the paragraph using the Paragraph.Items.Add(tocForImage) method.
  • Add a field separator using the Paragraph.AppendFieldMark(FieldMarkType.FieldSeparator) method.
  • Add the text content "TOC" using the Paragraph.AppendText("TOC") method.
  • Add a field end mark using the Paragraph.AppendFieldMark(FieldMarkType.FieldEnd) method.
  • Add an image using the Paragraph.AppendPicture() method.
  • Add a caption paragraph for the image using the DocPicture.AddCaption() method, including product information and formatting.
  • Update the table of contents to reflect changes in the document using the Document.UpdateTableOfContents(tocForImage) method.
  • Save the document using the Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

# Create a new document object
doc = Document()

# Add a section to the document
section = doc.AddSection()

# Create a table of content object for images
tocForImage = TableOfContent(doc, " \\h \\z \\c \"Picture\"")

# Add a paragraph to the section
tocParagraph = section.Body.AddParagraph()

# Add the TOC object to the paragraph
tocParagraph.Items.Add(tocForImage)

# Add a field separator
tocParagraph.AppendFieldMark(FieldMarkType.FieldSeparator)

# Add text content
tocParagraph.AppendText("TOC")

# Add a field end mark
tocParagraph.AppendFieldMark(FieldMarkType.FieldEnd)

# Add a blank paragraph to the section
section.Body.AddParagraph()

# Add a paragraph to the section
paragraph = section.Body.AddParagraph()

# Add an image
docPicture = paragraph.AppendPicture("images/DOC-Python.png")
docPicture.Width = 100
docPicture.Height = 100

# Add a caption paragraph for the image
obj = docPicture.AddCaption("Picture",CaptionNumberingFormat.Number,CaptionPosition.BelowItem)

paragraph = (Paragraph)(obj)
paragraph.AppendText("  Spire.Doc for Python product")
paragraph.Format.AfterSpacing = 20

# Continue adding paragraphs to the section
paragraph = section.Body.AddParagraph()
docPicture = paragraph.AppendPicture("images/PDF-Python.png")
docPicture.Width = 100
docPicture.Height = 100
obj = docPicture.AddCaption("Picture",CaptionNumberingFormat.Number,CaptionPosition.BelowItem)
paragraph = (Paragraph)(obj)
paragraph.AppendText("  Spire.PDF for Python product")
paragraph.Format.AfterSpacing = 20

paragraph = section.Body.AddParagraph()
docPicture = paragraph.AppendPicture("images/XLS-Python.png")
docPicture.Width = 100
docPicture.Height = 100
obj = docPicture.AddCaption("Picture",CaptionNumberingFormat.Number,CaptionPosition.BelowItem)
paragraph = (Paragraph)(obj)
paragraph.AppendText("  Spire.XLS for Python product")
paragraph.Format.AfterSpacing = 20

paragraph = section.Body.AddParagraph()
docPicture = paragraph.AppendPicture("images/PPT-Python.png")
docPicture.Width = 100
docPicture.Height = 100
obj = docPicture.AddCaption("Picture",CaptionNumberingFormat.Number,CaptionPosition.BelowItem)
paragraph = (Paragraph)(obj)
paragraph.AppendText("  Spire.Presentation for Python product")
paragraph.Format.AfterSpacing = 20

# Update the table of contents
doc.UpdateTableOfContents(tocForImage)

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

# Dispose of the document object
doc.Dispose()

Python: Create a Table Of Contents for a Newly Created Word Document

Python Create a Table Of Contents Using Table Captions

Similarly, you can create a table of contents based on table captions by employing the TableOfContent(Document, " \\h \\z \\c \"Table\"") method. Here are the detailed steps:

  • Create a Document object.
  • Add a section using the Document.AddSection() method.
  • Create a table of content object tocForTable = new TableOfContent(Document, " \\h \\z \\c \"Table\"") and specify the style of the table of contents.
  • Add a paragraph using the Section.AddParagraph() method.
  • Add the table of content object to the paragraph using the Paragraph.Items.Add(tocForTable) method.
  • Add a field separator using the Paragraph.AppendFieldMark(FieldMarkType.FieldSeparator) method.
  • Add the text content "TOC" using the Paragraph.AppendText("TOC") method.
  • Add a field end mark using the Paragraph.AppendFieldMark(FieldMarkType.FieldEnd) method.
  • Add a table using the Section.AddTable() method and set the number of rows and columns using the Table.ResetCells(int rowsNum, int columnsNum) method.
  • Add a table caption paragraph using the Table.AddCaption() method, including product information and formatting.
  • Update the table of contents to reflect changes in the document using the Document.UpdateTableOfContents(tocForTable) method.
  • Save the document using the Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

# Create a new document
doc = Document()

# Add a section to the document
section = doc.AddSection()

# Create a TableOfContent object
tocForTable = TableOfContent(doc,  " \\h \\z \\c \"Table\"")

# Add a paragraph in the section to place the TableOfContent object
tocParagraph = section.Body.AddParagraph()
tocParagraph.Items.Add(tocForTable)
tocParagraph.AppendFieldMark(FieldMarkType.FieldSeparator)
tocParagraph.AppendText("TOC")
tocParagraph.AppendFieldMark(FieldMarkType.FieldEnd)

# Add two empty paragraphs in the section
section.Body.AddParagraph()
section.Body.AddParagraph()

# Add a table in the section
table = section.Body.AddTable(True)
table.ResetCells(1, 3)

# Add a caption paragraph for the table
obj = table.AddCaption("Table", CaptionNumberingFormat.Number, CaptionPosition.BelowItem)
paragraph = (Paragraph)(obj)
paragraph.AppendText(" One row three columns")
paragraph.Format.AfterSpacing = 20

# Add a new table in the section
table = section.Body.AddTable(True)
table.ResetCells(3, 3)

# Add a caption paragraph for the second table
obj = table.AddCaption("Table", CaptionNumberingFormat.Number, CaptionPosition.BelowItem)
paragraph = (Paragraph)(obj)
paragraph.AppendText(" Three rows three columns")
paragraph.Format.AfterSpacing = 20

# Add another new table in the section
table = section.Body.AddTable(True)
table.ResetCells(5, 3)

# Add a caption paragraph for the third table
obj = table.AddCaption("Table", CaptionNumberingFormat.Number, CaptionPosition.BelowItem)
paragraph = (Paragraph)(obj)
paragraph.AppendText(" Five rows three columns")
paragraph.Format.AfterSpacing = 20

# Update the table of contents
doc.UpdateTableOfContents(tocForTable)

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

# Dispose resources
doc.Dispose()

Python: Create a Table Of Contents for a Newly Created 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.

page 45