Python: Merge or Split Tables in Word

Merge tables in Word can be useful when you want to combine data from multiple tables into a single, larger table to create a more comprehensive view of the information. On the contrary, split tables can help you divide a large table into smaller, more manageable sections so you can focus on specific data sets. This article will demonstrate how to merge or split tables in Word in Python using Spire.Doc for Python.

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 commands.

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

Merge Tables in Word in Python

With Spire.Doc for Python, you can combine two or more tables into one by copying all rows from other tables to the target table and then deleting the other tables. The following are the detailed steps.

  • Create a Document instance.
  • Load a Word document using Document.LoadFromFile() method.
  • Get a specified section using Document.Sections[] property.
  • Get two tables in the section using section.Tables[] property.
  • Iterate through all rows in the second table and copy them using Table.Rows[].Clone() method.
  • Add the rows of the second table to the first table using Table.Rows.Add() method.
  • Save the result document using Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

inputFile = "Cost.docx"
outputFile = "CombineTables.docx"

# Create a Document instance
doc = Document()

# Load a Word document
doc.LoadFromFile(inputFile)

# Get the first section
section = doc.Sections[0]

# Get the first and second table in the section
table1 = section.Tables[0] if isinstance(section.Tables[0], Table) else None
table2 = section.Tables[1] if isinstance(section.Tables[1], Table) else None

# Add rows of the second table to the first table
for i in range(table2.Rows.Count):
    table1.Rows.Add(table2.Rows[i].Clone())

# Remove the second table
section.Tables.Remove(table2)

# Save the result document
section.Document.SaveToFile(outputFile, FileFormat.Docx2013)
doc.Close()

Python: Merge or Split Tables in Word

Spilt a Table in Word in Python

To split a table into two or more tables, you need to create a new table, then copy the specified rows from the original table to the new table, and then delete those rows from the original table. The following are the detailed steps.

  • Create a Document instance.
  • Load a Word document using Document.LoadFromFile() method.
  • Get a specified section using Document.Sections[] property.
  • Get a specified table in the section using section.Tables[] property.
  • Specify the row index where the table will be split.
  • Create a new instance of the Table class.
  • Iterate through the specified rows in the original table and copy them using Table.Rows[].Clone() method.
  • Add the specified rows to the new table using Table.Rows.Add() method.
  • Iterate through the copied rows and remove each row from the original table using Table.Rows.RemoveAt() method.
  • Add the new table to the section using Section.Tables.Add() method.
  • Save the result document using Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

inputFile = "CombineTables.docx"
outputFile = "SplitTable.docx"

# Create a Document instance
doc = Document()

# Load a Word document
doc.LoadFromFile(inputFile)

# Get the first section
section = doc.Sections[0]

# Get the first table in the section
table = section.Tables[0] if isinstance(section.Tables[0], Table) else None

# Specify to split the table from the fifth row
splitIndex = 4

# Create a new table
newTable = Table(section.Document)

# Adds rows (from the 5th to the last row) to the new table
for i in range(splitIndex, table.Rows.Count):
    newTable.Rows.Add(table.Rows[i].Clone())

# Delete rows from the original table
for i in range(table.Rows.Count - 1, splitIndex - 1, -1):
    table.Rows.RemoveAt(i)

# Add the new table to the section
section.Tables.Add(newTable)

# Save the result document
section.Document.SaveToFile(outputFile, FileFormat.Docx2013)
doc.Close()

Python: Merge or Split Tables in Word

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.