Skip to main content

Table

A DrawingML table object. Not intended to be constructed directly, use Slide.shapes.add_table() to add a table to a slide.

Properties

rows
_RowCollection
Collection providing access to rows in this table. Rows are accessed using list notation.
row = table.rows[0]
print(len(table.rows))
columns
_ColumnCollection
Collection providing access to columns in this table. Columns are accessed using list notation.
col = table.columns[0]
col.width = Inches(2.0)
first_row
bool
When True, indicates first row should have distinct formatting. Used when the first row contains column headings.
table.first_row = True
first_col
bool
When True, indicates first column should have distinct formatting. Used when the first column contains row headings.
table.first_col = True
last_row
bool
When True, indicates the bottom row should have distinct formatting. Used when a totals row appears as the bottom row.
table.last_row = True
last_col
bool
When True, indicates the rightmost column should have distinct formatting. Used when a row totals column appears at the far right.
table.last_col = True
horz_banding
bool
When True, indicates rows should have alternating shading. Used to allow rows to be traversed more easily.
table.horz_banding = True
vert_banding
bool
When True, indicates columns should have alternating shading.
table.vert_banding = True

Methods

cell(row_idx, col_idx)
_Cell
Return cell at specified row and column indices (zero-based).
row_idx
int
required
Zero-based row index
col_idx
int
required
Zero-based column index
cell = table.cell(0, 0)  # Top-left cell
cell.text = "Header"
iter_cells()
Iterator[_Cell]
Generate _Cell object for each cell in this table. Each grid cell is generated in left-to-right, top-to-bottom order.
for cell in table.iter_cells():
    cell.text = ""

Cell

Table cell object representing a single cell at a particular row/column location.

Properties

text
str
Textual content of cell as a single string. Newline characters ("\n") separate paragraphs, vertical-tab ("\v") characters represent line breaks.
cell.text = "Line 1\nParagraph 2"
text_frame
TextFrame
TextFrame containing the text that appears in the cell.
cell.text_frame.text = "Cell content"
cell.text_frame.word_wrap = True
fill
FillFormat
FillFormat instance for this cell. Provides access to fill properties such as foreground color.
from pptx.util import RGBColor
cell.fill.solid()
cell.fill.fore_color.rgb = RGBColor(200, 200, 200)
vertical_anchor
MSO_VERTICAL_ANCHOR | None
Vertical alignment of this cell. None indicates the cell has no explicitly applied setting and its value is inherited.
from pptx.enum.text import MSO_VERTICAL_ANCHOR
cell.vertical_anchor = MSO_VERTICAL_ANCHOR.MIDDLE
margin_left
Length
Left margin of cell. Assigning None uses the default value (0.1 inches for left and right margins).
from pptx.util import Inches
cell.margin_left = Inches(0.1)
margin_right
Length
Right margin of cell.
margin_top
Length
Top margin of cell. Default is 0.05 inches for top and bottom margins.
margin_bottom
Length
Bottom margin of cell.
is_merge_origin
bool
True if this cell is the top-left grid cell in a merged cell.
if cell.is_merge_origin:
    print(f"Spans {cell.span_width} columns, {cell.span_height} rows")
is_spanned
bool
True if this cell is spanned by a merge-origin cell. Note this is False for a merge-origin cell itself.
span_width
int
Count of columns spanned by this cell. Only meaningful on merge-origin cells.
span_height
int
Count of rows spanned by this cell. Only meaningful on merge-origin cells.

Methods

merge(other_cell)
None
Create merged cell from this cell to other_cell. Either diagonal may be specified in either order.
other_cell
_Cell
required
The opposite corner cell of the merge range
# Merge cells from (0,0) to (1,2)
top_left = table.cell(0, 0)
bottom_right = table.cell(1, 2)
top_left.merge(bottom_right)
Raises ValueError if the range contains merged cells or if other_cell is not in the same table.
split()
None
Remove merge from this merge-origin cell. The merged cell will be “unmerged”, yielding separate cells.
if cell.is_merge_origin:
    cell.split()
Raises ValueError when this cell is not a merge-origin cell.

Row

Table row object.

Properties

height
Length
Height of row in EMU.
from pptx.util import Inches
row.height = Inches(0.5)
cells
_CellCollection
Read-only reference to collection of cells in row. Individual cells are accessed using list notation.
cell = row.cells[0]
for cell in row.cells:
    cell.text = ""

Column

Table column object.

Properties

width
Length
Width of column in EMU.
from pptx.util import Inches
column.width = Inches(2.0)

Build docs developers (and LLMs) love