Skip to main content

Command Palette

Search for a command to run...

Delete Tables in Word Documents Using Python [Effortless]

Updated
3 min read

When working with automated document processing, modifying existing structures in Word documents is a frequent requirement. Whether you need to streamline information layouts, remove obsolete structured data, or dynamically refresh target content, deleting tables programmatically saves substantial manual effort. Manually removing tables from large volumes of Word files is not only incredibly time-consuming but also highly susceptible to human error. This article demonstrates how to easily remove tables from Word documents using Python.

In this tutorial, we will cover:

  • How to delete a specific table in Word using Python

  • How to delete all tables in Word using Python

The methods described below require the Spire.Doc for Python library. You can install it directly via pip:

pip install Spire.Doc

Python: Delete a Specific Table in a Word Document

To delete a particular table from a Word file, you can utilize the TableCollection.RemoveAt(index) method, which targets and deletes the table at a specified index. The detailed implementation steps are as follows:

  1. Load the target Word document.

  2. Access the desired section of the document through the Sections[] property.

  3. Retrieve the collection of tables within that section using the Section.Tables property.

  4. Invoke the TableCollection.RemoveAt(index) method to delete the table at the specified index position.

  5. Save the modified document.

Python Code:

from spire.doc import *
from spire.doc.common import *

# Load the Word document
doc = Document()
doc.LoadFromFile("Table1.docx")

# Access the first section of the document
sec = doc.Sections[0]

# Delete the second table in this section (index 1)
sec.Tables.RemoveAt(1)

# Save the updated document
doc.SaveToFile("DeleteSpecificTable.docx", FileFormat.Docx)
doc.Close()

Python: Delete All Tables in a Word Document

If your automation workflow requires removing all tables present throughout the entire document, you need to iterate through each section and clear the table collection. The operational workflow is detailed below:

  1. Load the source Word document.

  2. Iterate through all the sections within the document.

  3. Loop through the tables contained in each section, and apply the Section.Tables.Remove() method to delete every table individually.

  4. Save the cleared document.

Python Code:

from spire.doc import *
from spire.doc.common import *

# Load the Word document
doc = Document()
doc.LoadFromFile("Table1.docx")

# Iterate through all sections in the document
for i in range(doc.Sections.Count):
    sec = doc.Sections.get_Item(i)

    # Iterate through each table within the current section
    for j in range(sec.Tables.Count):
        table = sec.Tables.get_Item(j)
        # Remove the individual table
        sec.Tables.Remove(table)

# Save the processed document
doc.SaveToFile("DeleteAllTables.docx", FileFormat.Docx)
doc.Close()

Conclusion

In this article, we explored how to programmatically delete specific tables or completely strip all tables from a Word document using Spire.Doc for Python. Whether targeting a single table via its index or cleaning up an entire file through nested loops, Python automation simplifies tedious document formatting tasks, ensuring fast, accurate, and consistent results across bulk files.