Python: Add or Delete Table Rows and Columns in Word

Adding or removing rows and columns in a Word table allows you to adjust the table's structure to accommodate your data effectively. By adding rows and columns, you can effortlessly expand the table as your data grows, ensuring that all relevant information is captured and displayed in a comprehensive manner. On the other hand, removing unnecessary rows and columns allows you to streamline the table, eliminating any redundant or extraneous data that may clutter the document. In this article, we will demonstrate how to add or delete table rows and columns 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 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 or Insert a Row into a Word Table in Python

You can add a row to the end of a Word table or insert a row at a specific location of a Word table using the Table.AddRow() or Table.InsertRow() method. The following are the detailed steps:

  • Create a Document object.
  • Load a Word document using Document.LoadFromFile() method.
  • Get the first section of the document using Document.Sections[] property.
  • Get the first table of the section using Section.Tables[] property.
  • Insert a row at a specific location of the table using Table.Rows.Insert() method.
  • Add data to the newly inserted row.
  • Add a row to the end of the table using Table.AddRow() method.
  • Add data to the newly added row.
  • Save the resulting document using Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

# Create a Document object
document = Document()
# Load a Word document
document.LoadFromFile("Table1.docx")

# Get the first section of the document
section = document.Sections[0]

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

# Insert a row into the table as the third row
table.Rows.Insert(2, table.AddRow())
# Get the inserted row
insertedRow = table.Rows[2]
# Add data to the row
for i in range(insertedRow.Cells.Count):
    cell = insertedRow.Cells[i]
    paragraph = cell.AddParagraph()
    paragraph.AppendText("Inserted Row")
    paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center
    cell.CellFormat.VerticalAlignment = VerticalAlignment.Middle

# Add a row at the end of the table
addedRow = table.AddRow()
# Add data to the row 
for i in range(addedRow.Cells.Count):
    cell = addedRow.Cells[i]
    paragraph = cell.AddParagraph()
    paragraph.AppendText("End Row")
    paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center    
    cell.CellFormat.VerticalAlignment = VerticalAlignment.Middle

# Save the resulting document
document.SaveToFile("AddRows.docx", FileFormat.Docx2016)
document.Close()

Python: Add or Delete Table Rows and Columns in Word

Add or Insert a Column into a Word Table in Python

Spire.Doc for Python doesn't offer a direct method to add or insert a column into a Word table. But you can achieve this by adding or inserting cells at a specific location of each table row using TableRow.Cells.Add() or TableRow.Cells.Insert() method. The detailed steps are as follows:

  • Create a Document object.
  • Load a Word document using Document.LoadFromFile() method.
  • Get the first section of the document using Document.Sections[] property.
  • Get the first table of the section using Section.Tables[] property.
  • Loop through each row of the table.
  • Create a TableCell object, then insert it at a specific location of each row using TableRow.Cells.Insert() method and set cell width.
  • Add data to the cell and set text alignment.
  • Add a cell to the end of each row using TableRow.AddCell() method and set cell width.
  • Add data to the cell and set text alignment.
  • Save the resulting document using Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

# Create a Document object
document = Document()
# Load a Word document
document.LoadFromFile("Table1.docx")

# Get the first section of the document
section = document.Sections[0]

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

# Loop through the rows of the table
for i in range(table.Rows.Count):
    row = table.Rows[i]
    # Create a TableCell object
    cell = TableCell(document)   
    # Insert the cell as the third cell of the row and set cell width
    row.Cells.Insert(2, cell)
    cell.Width = row.Cells[0].Width
    # Add data to the cell
    paragraph = cell.AddParagraph()
    paragraph.AppendText("Inserted Column")
    # Set text alignment
    paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center  
    cell.CellFormat.VerticalAlignment = VerticalAlignment.Middle

    # Add a cell to the end of the row and set cell width
    cell = row.AddCell()
    cell.Width = row.Cells[1].Width
    # Add data to the cell
    paragraph = cell.AddParagraph()
    paragraph.AppendText("End Column")
    # Set text alignment
    paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center    
    cell.CellFormat.VerticalAlignment = VerticalAlignment.Middle

# Save the resulting document
document.SaveToFile("AddColumns.docx", FileFormat.Docx2016)
document.Close()

Python: Add or Delete Table Rows and Columns in Word

Delete a Row from a Word Table in Python

To delete a specific row from a Word table, you can use the Table.Rows.RemoveAt() method. The detailed steps are as follows:

  • Create a Document object.
  • Load a Word document using Document.LoadFromFile() method.
  • Get the first section of the document using Document.Sections[] property.
  • Get the first table of the section using Section.Tables[] property.
  • Remove a specific row from the table using Table.Rows.RemoveAt() method.
  • Save the resulting document using Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

# Create a Document object
document = Document()
# Load a Word document
document.LoadFromFile("AddRows.docx")

# Get the first section of the document
section = document.Sections[0]

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

# Remove the third row
table.Rows.RemoveAt(2)
# Remove the last row
table.Rows.RemoveAt(table.Rows.Count - 1)

# Save the resulting document
document.SaveToFile("RemoveRows.docx", FileFormat.Docx2016)
document.Close()

Python: Add or Delete Table Rows and Columns in Word

Delete a Column from a Word Table in Python

To delete a specific column from a Word table, you need to remove the corresponding cell from each table row using the TableRow.Cells.RemoveAt() method. The detailed steps are as follows:

  • Create a Document object.
  • Load a Word document using Document.LoadFromFile() method.
  • Get the first section of the document using Document.Sections[] property.
  • Get the first table of the section using Section.Tables[] property.
  • Loop through each row of the table.
  • Remove a specific cell from each row using TableRow.Cells.RemoveAt() method.
  • Save the resulting document using Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

# Create a Document object
document = Document()
# Load a Word document
document.LoadFromFile("AddColumns.docx")

# Get the first section of the document
section = document.Sections[0]

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

# Loop through the rows of the table
for i in range(table.Rows.Count):
    row = table.Rows[i]
    # Remove the third cell from the row
    row.Cells.RemoveAt(2)
    # Remove the last cell from the row
    row.Cells.RemoveAt(row.Cells.Count - 1)

# Save the resulting document
document.SaveToFile("RemoveColumns.docx", FileFormat.Docx2016)
document.Close()

Python: Add or Delete Table Rows and Columns 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.