Create TOC in Word

A Table of Contents (TOC) is an essential feature in Word documents, especially for lengthy documents such as reports, theses, and eBooks. It makes Word documents more readable and allows readers to quickly navigate through different sections of the document. In this post, you will learn how to automate TOC in Word documents programmatically using Python. We will discuss the steps to create a table of contents in a Word document, insert TOC in an existing document, or extract TOC from a document in Python.

This article covers the following topics:

Python Word Library to Work with TOC

To work with TOC in Word documents using Python, we will use Aspose.Words for Python library. It allows developers to create, modify, and automate Word documents in Python applications. Let’s start by setting up our environment.

Before we dive into the code, ensure you have Aspose.Words for Python installed. You can download the package or install the API from PyPI using the following pip command in your terminal:

PM> Install-Package Aspose.Words

This will download and install the Aspose.Words for Python library and its dependencies.

Create a Table of Contents in Word using Python

Please follow the steps below to add a table of contents to a Word document using Python.

  1. Create an instance of the Document class.
  2. Create an instance of the DocumentBuilder class with the Document object.
  3. Insert a table of contents using the insert_table_of_contents() method.
  4. Specify heading styles using the style_identifier property.
  5. Update the fields using the update_fields() method.
  6. Save the Word document using the save() method.

The following code sample shows how to create a table of contents in a Word document using Python.

Create a Table of Contents in Word using Python.

Add a Table of Contents in Word using Python

Create a Table of Contents in an Existing Word Document

You can also insert a table of contents in an existing Word document by following the steps below:

  1. Load an existing Word document using the Document class.
  2. Create an instance of the DocumentBuilder class and initialize it with the Document object created earlier.
  3. Insert a table of contents using the insert_table_of_contents() method.
  4. Update the fields using the update_fields() method.
  5. Save the Word document using the save() method.

The following code sample shows how to create a table of contents in an existing Word document using Python.

Create a Table of Contents in an Existing Word Document.

Create a Table of Contents in an Existing Word Document

Extract a Table of Contents from a Word Document in Python

You can extract fields from a table of contents of a Word document by following the steps below:

  1. Load the Word document using the Document class.
  2. Loop through the doc.range.fields collection.
  3. Check if the field type is FIELD_HYPERLINK.
  4. Check if the field comes under the table of contents section.
  5. Retrieve the information from the field and print it.

The following code sample shows how to extract a table of contents from a Word document using Python.

Heading 1       1
------------------
Heading 1

Heading2        1
------------------
Heading2

Another Heading 1
------------------
Another Heading

Get a Free License

You can get a free temporary license and create a table of contents in Word documents without evaluation limitations.

TOC in Word – Free Resources

Besides working with TOC in Word documents, explore more features of Aspose.Words for Python using the resources below:

Conclusion

In this article, you have learned how to work with a table of contents (TOC) in a Word document programmatically. By following the steps outlined in this article, you can automate the process of creating and managing lengthy documents. Aspose.Words for Python equips you to efficiently manage your Word documents and enhance their usability with a well-structured Table of Contents. If you have any questions or need further assistance, please feel free to reach out at our free support forum.

See Also