Python: Set Alignment for Table and Table Text in Word

2024-05-24 01:02:59 Written by  support iceblue
Rate this item
(0 votes)

Proper alignment of tables and text in Microsoft Word is crucial for creating visually appealing and easy-to-read documents. By aligning table headers, numeric data, and text appropriately, you can enhance the organization and clarity of your information, making it more accessible to your readers. In this article, we will demonstrate how to align tables and the text in table cells in Microsoft 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

Align Tables in Word in Python

A table in a Word document can be aligned to the left, center, or right side by using the Table.TableFormat.HorizontalAlignment property. The detailed steps are as follows.

  • Create an instance of the Document class.
  • Load a Word document using Document.LoadFromFile() method.
  • Get a specific section in the document using Document.Sections[index] property.
  • Get a specific table in the section using Section.Tables[index] property.
  • Set the alignment for the table using Table.TableFormat.HorizontalAlignment property.
  • Save the result document using Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

# Create an instance of the Document class
document = Document()
# Load a Word document
document.LoadFromFile("Tables.docx")

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

# Get the first, second, and third tables in the section
table1 = section.Tables[0]
table2 = section.Tables[1]
table3 = section.Tables[2]

# Align the first table to the left
table1.TableFormat.HorizontalAlignment = RowAlignment.Left
# Align the second table to the center
table2.TableFormat.HorizontalAlignment = RowAlignment.Center
# Align the third table to the right
table3.TableFormat.HorizontalAlignment = RowAlignment.Right

# Save the result document
document.SaveToFile("AlignTable.docx", FileFormat.Docx2013)
document.Close()

Python: Set Alignment for Table and Table Text in Word

Align the Text in Table Cells in Word in Python

The text within a table cell can be horizontally aligned to the left, center, or right side using the TableCell.Paragraphs[index].Format.HorizontalAlignment property. Additionally, they can also be vertically aligned to the top, center, or bottom of the cell using the TableCell.CellFormat.VerticalAlignment property. The detailed steps are as follows.

  • Create an instance of the Document class.
  • Load a Word document using Document.LoadFromFile() method.
  • Get a specific section in the document using Document.Sections[index] property.
  • Get a specific table in the section using Section.Tables[index] property.
  • Loop through the rows in the table.
  • Loop through the cells in each row.
  • Set the vertical alignment for the text in each cell using TableCell.CellFormat.VerticalAlignment property.
  • Loop through the paragraphs in each cell.
  • Set the horizontal alignment for each paragraph using TableCell.Paragraphs[index].Format.HorizontalAlignment property.
  • Save the result document using Document.SaveToFile() method.
  • Python
from spire.doc import *
from spire.doc.common import *

# Create an instance of the Document class
document = Document()
# Load a Word document
document.LoadFromFile("Table.docx")

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

# Get the first tables in the section
table = section.Tables[0]

# Loop through the rows in the table
for row_index in range(table.Rows.Count):
    row = table.Rows[row_index]
    # Loop through the cells in the row
    for cell_Index in range(row.Cells.Count):
        cell = row.Cells[cell_Index]
        # Vertically align the text in the cell to the center
        cell.CellFormat.VerticalAlignment = VerticalAlignment.Middle
        # Horizontally align the text in the cell to the center
        for para_index in range(cell.Paragraphs.Count):
            paragraph = cell.Paragraphs[para_index]
            paragraph.Format.HorizontalAlignment = HorizontalAlignment.Center

# Save the result document
document.SaveToFile("AlignTableText.docx", FileFormat.Docx2013)
document.Close()

Python: Set Alignment for Table and Table Text 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.