Knowledgebase (2330)
Children categories
Merging and splitting table cells in PowerPoint are two common functions, mainly used to adjust the layout and structure of the table. Merging cells involves combining adjacent cells into a larger one. It allows users to create title cells that span multiple columns or rows. On the other hand, splitting cells means dividing a cell into several smaller ones, which is useful for creating detailed layouts or accommodating diverse content. In this article, we will show you how to merge and split table cells in PowerPoint programmatically by using Spire.Presentation for .NET.
Install Spire.Presentation for .NET
To begin with, you need to add the DLL files included in the Spire.Presentation for.NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.Presentation
Merge Table Cells in PowerPoint
Spire.Presentation for .NET provides users with ITable[int columnIndex, int rowIndex] property and ITable.MergeCells(Cell startCell, Cell endCell, boolean allowSplitting) method to get and merge the specific cells. The detailed steps are as follows.
- Create an object of Presentation class.
- Load a sample file using Presentation.LoadFromFile() method.
- Get the table from the first slide by looping through all shapes.
- Get the specific cells by ITable[int columnIndex, int rowIndex] property and merge them by using ITable.MergeCells(Cell startCell, Cell endCell, boolean allowSplitting) method.
- Save the result file using Presentation.SaveToFile() method.
- C#
- VB.NET
using Spire.Presentation;
namespace MergeCells
{
class Program
{
static void Main(string[] args)
{
//Create an object of Presentation class
Presentation presentation = new Presentation();
//Load a PowerPoint presentation
presentation.LoadFromFile("sample.pptx");
//Get the table from the first slide by looping through all shapes
ITable table = null;
foreach (IShape shape in presentation.Slides[0].Shapes)
{
if (shape is ITable)
{
table = (ITable)shape;
//Merge the cells from [0,0] to [4,0]
table.MergeCells(table[0, 0], table[4, 0], false);
}
}
//Save the result document
presentation.SaveToFile("MergeCells.pptx", FileFormat.Pptx2010);
presentation.Dispose();
}
}
}

Split Table Cells in PowerPoint
Spire.Presentation for .NET also supports users to get the specific cell and split it into smaller ones by using ITable[int columnIndex, int rowIndex] property and Cell.Split(int RowCount, int ColunmCount) method. The detailed steps are as follows.
- Create an object of Presentation class.
- Load a sample file using Presentation.LoadFromFile() method.
- Get the table from the first slide by looping through all shapes.
- Get the specific cell by ITable[int columnIndex, int rowIndex] property and split it into 2 rows and 2 columns by using Cell.Split(int RowCount, int ColumnCount) method.
- Save the result file using Presentation.SaveToFile() method.
- C#
- VB.NET
using Spire.Presentation;
namespace SplitCells
{
class Program
{
static void Main(string[] args)
{
//Create an object of Presentation class
Presentation presentation = new Presentation();
//Load a PowerPoint presentation
presentation.LoadFromFile("sample.pptx");
//Get the table from the first slide by looping through all shapes
ITable table = null;
foreach (IShape shape in presentation.Slides[0].Shapes)
{
if (shape is ITable)
{
table = (ITable)shape;
//Split cell [2, 2] into 2 rows and 2 columns
table[2, 2].Split(2, 2);
}
}
//Save the result document
presentation.SaveToFile("SplitCells.pptx", FileFormat.Pptx2013);
presentation.Dispose();
}
}
}

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.
Text wrapping and unwrapping are powerful formatting options in Microsoft Excel that offer flexibility in displaying text within cells. When text wrapping is enabled, long text is automatically wrapped into multiple lines within a cell, which ensures that the entire content is visible without truncation. This feature is particularly useful for presenting lengthy descriptions, notes, or paragraphs within a confined cell space. On the other hand, text unwrapping allows you to remove line breaks and display the text in a single line within the cell. This can be beneficial in scenarios where you need to fit the text into a specific layout or when exporting data to other applications or file formats that may not handle wrapped text correctly. In this article, we will demonstrate how to wrap or unwrap text in Excel cells in Python using Spire.XLS for Python.
Install Spire.XLS for Python
This scenario requires Spire.XLS for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.
pip install Spire.XLS
If you are unsure how to install, please refer to this tutorial: How to Install Spire.XLS for Python on Windows
Wrap or Unwrap Text in Excel Cells in Python
Spire.XLS for Python provides the CellStyle.WrapText property to control whether the text should be wrapped or unwrapped within a cell. If you want to wrap text in a cell, you can set the property as True. Conversely, if you want to unwrap text in a cell, you can set the property as False.
The following steps explain how to wrap or unwrap text in an Excel cell using Spire.XLS for Python:
- Create a Workbook object.
- Load a sample Excel file using Workbook.LoadFromFile() method.
- Get a specified worksheet using Workbook.Worksheets[] property.
- Get a specified cell using Worksheet.Range[] property.
- Get the style of the specified cell using CellRange.Style property.
- Wrap the text in the cell by setting the CellStyle.WrapText property to True. Or unwrapping the text in the cell by setting the CellStyle.WrapText property to False.
- Save the resulting file using Workbook.SaveToFile() method.
- Python
from spire.xls import *
from spire.xls.common import *
# Create a Workbook object
workbook = Workbook()
# Load a sample Excel file
workbook.LoadFromFile("Sample.xlsx")
# Get the first worksheet of the file
sheet = workbook.Worksheets[0]
# Wrap the text in cell B3
sheet.Range["B3"].Style.WrapText = True
# Unwrap the text in cell B7
sheet.Range["B7"].Style.WrapText = False
#Save the resulting file
workbook.SaveToFile("WrapOrUnwrapTextInCells.xlsx", ExcelVersion.Version2013)
workbook.Dispose()

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.
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.
- Add or Insert a Row into a Word Table in Python
- Add or Insert a Column into a Word Table in Python
- Delete a Row from a Word Table in Python
- Delete a Column from a Word Table in 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.get_Item(0)
# Get the first table of the first section
table = section.Tables.get_Item(0) if isinstance(section.Tables.get_Item(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()

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.get_Item(0)
# Get the first table of the first section
table = section.Tables.get_Item(0) if isinstance(section.Tables.get_Item(0), Table) else None
# Loop through the rows of the table
for i in range(table.Rows.Count):
row = table.Rows.get_Item(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()

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.get_Item(0)
# Get the first table of the first section
table = section.Tables.get_Item(0) if isinstance(section.Tables.get_Item(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()

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.get_Item(0)
# Get the first table of the first section
table = section.Tables.get_Item(0) if isinstance(section.Tables.get_Item(0), Table) else None
# Loop through the rows of the table
for i in range(table.Rows.Count):
row = table.Rows.get_Item(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()

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.