Python: Set or Remove Table Borders in PowerPoint

Table borders in PowerPoint refer to the visible lines or outlines that surround the cells within a table. These borders provide a visual separation between cells and help define the boundaries of the table. By setting or modifying table borders, you can customize the appearance of tables in your PowerPoint presentations. In this article, we will guide you on how to set and remove table borders 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

Set Table Borders in PowerPoint in Python

Spire.Presentation for Python provides the ITable.SetTableBorder() method, which allows you to set borders for a table in PowerPoint. The detailed steps are as follows.

  • Create an object of the Presentation class.
  • Get the first slide of the presentation using Presentation.Slides[] property.
  • Add a table to the slide using ISlide.Shapes.AppendTable() method.
  • Add borders to the table and set the border type, width, and color using ITable.SetTableBorder() method.
  • Save the result presentation using Presentation.SaveToFile() method.
  • Python
from spire.presentation.common import *
from spire.presentation import *

# Create a Presentation object
presentation = Presentation()
# Get  the first slide of the presentation
slide = presentation.Slides[0]

# Specify the number and size of rows and columns in table
widths = [100, 100, 100, 100, 100]
heights = [20, 20]

# Add a table to the first slide
table = slide.Shapes.AppendTable(100, 100, widths, heights)

# Add borders to the table and set the border type, width, and color
table.SetTableBorder(TableBorderType.All, 1, Color.get_Blue())

# Save the result presentation
presentation.SaveToFile("SetBorders.pptx", FileFormat.Pptx2013)
presentation.Dispose()

Python: Set or Remove Table Borders in PowerPoint

Remove Table Borders in PowerPoint in Python

To remove borders from a table, you need to iterate through the cells in the table and then remove the borders from each 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 of the presentation using Presentation.Slides[] property.
  • Get the table on the slide.
  • Iterate through the rows in the table and the cells in each row.
  • Remove the borders from each cell by setting the fill type of the top, bottom, left and right borders of the cell as none.
  • Save the result presentation using Presentation.SaveToFile() method.
  • Python
from spire.presentation.common import *
from spire.presentation import *

# Create a PowerPoint presentation
presentation = Presentation()
# Load a PowerPoint presentation
presentation.LoadFromFile("SetBorders.pptx")

# Get the first slide
slide = presentation.Slides[0]
# Get the table on the slide
table = slide.Shapes[0] if isinstance(slide.Shapes[0], ITable) else None

# Iterate through the rows and cells in the table
for row in table.TableRows:
    for cell in row:
        # Remove borders from each cell by setting the fill type of the top, bottom, left and right borders of the cell as none
        cell.BorderTop.FillType = FillFormatType.none
        cell.BorderBottom.FillType = FillFormatType.none
        cell.BorderLeft.FillType = FillFormatType.none
        cell.BorderRight.FillType = FillFormatType.none                   

# Save the result presentation
presentation.SaveToFile("RemoveBorders.pptx", FileFormat.Pptx2013)
presentation.Dispose()

Python: Set or Remove Table Borders 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.