Python: Merge or Split Table Cells in PowerPoint

Merging and splitting table cells in PowerPoint are essential features that enable users to effectively organize and present data. By merging cells, users can create larger cells to accommodate more information or establish header rows for better categorization. On the other hand, splitting cells allows users to divide a cell into smaller units to showcase specific details, such as individual data points or subcategories. These operations enhance the visual appeal and clarity of slides, helping the audience better understand and analyze the presented data. In this article, we will demonstrate how to merge and split table cells in PowerPoint in Python using Spire.Presentation for Python.

Install Spire.Presentation for Python

This scenario requires Spire.Presentation for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.

pip install Spire.Presentation

If you are unsure how to install, please refer to this tutorial: How to Install Spire.Presentation for Python on Windows

Merge Table Cells in PowerPoint in Python

Spire.Presentation for Python offers the ITable[columnIndex, rowIndex] property to access specific table cells. Once accessed, you can use the ITable.MergeCells(startCell, endCell, allowSplitting) method to merge them into a larger cell. The detailed steps are as follows.

  • Create an object of the Presentation class.
  • Load a PowerPoint presentation using Presentation.LoadFromFile() method.
  • Get a specific slide using Presentation.Slides[index] property.
  • Find the table on the slide by looping through all shapes.
  • Get the cells you want to merge using ITable[columnIndex, rowIndex] property.
  • Merge the cells using ITable.MergeCells(startCell, endCell, allowSplitting) method.
  • Save the result presentation using Presentation.SaveToFile() method.
  • Python
from spire.presentation.common import *
from spire.presentation import *

# Create a Presentation object
ppt = Presentation()

# Load a PowerPoint presentation
ppt.LoadFromFile("Table1.pptx")

# Get the first slide
slide = ppt.Slides[0]

# Find the table on the first slide
table = None
for shape in slide.Shapes:
    if isinstance(shape, ITable):
        table = shape
        # Get the cell at column 2, row 2
        cell1 = table[1, 1]
        # Get the cell at column 2, row 3
        cell2 = table[1, 2]
        # Check if the content of the cells is the same
        if cell1.TextFrame.Text == cell2.TextFrame.Text:
            # Clear the text in the second cell
            cell2.TextFrame.Paragraphs.Clear()
        # Merge the cells
        table.MergeCells(cell1, cell2, True)

# Save the result presentation to a new file
ppt.SaveToFile("MergeCells.pptx", FileFormat.Pptx2016)
ppt.Dispose()

Python: Merge or Split Table Cells in PowerPoint

Split Table Cells in PowerPoint in Python

In addition to merging specific table cells, Spire.Presentation for Python also empowers you to split a specific table cell into smaller cells by using the Cell.Split(rowCount, colunmCount) method. The detailed steps are as follows.

  • Create an object of the Presentation class.
  • Load a PowerPoint presentation using Presentation.LoadFromFile() method.
  • Get a specific slide using Presentation.Slides[index] property.
  • Find the table on the slide by looping through all shapes.
  • Get the cell you want to split using ITable[columnIndex, rowIndex] property.
  • Split the cell into smaller cells using Cell.Split(rowCount, columnCount) method.
  • Save the result presentation using Presentation.SaveToFile() method.
  • Python
from spire.presentation.common import *
from spire.presentation import *

# Create a Presentation object
ppt = Presentation()

# Load a PowerPoint presentation
ppt.LoadFromFile("Table2.pptx")

# Get the first slide
slide = ppt.Slides[0]

# Find the table on the first slide
table = None
for shape in slide.Shapes:
    if isinstance(shape, ITable):
        table = shape
        # Get the cell at column 2, row 3
        cell = table[1, 2]
        # Split the cell into 3 rows and 2 columns
        cell.Split(3, 2)

# Save the result presentation to a new file
ppt.SaveToFile("SplitCells.pptx", FileFormat.Pptx2016)
ppt.Dispose()

Python: Merge or Split Table Cells in PowerPoint

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.