Table objects¶
Table objects are constructed using the add_table()
method on Document
.
Table
objects¶
-
class
docx.table.
Table
(tbl, parent)[source]¶ Proxy class for a WordprocessingML
<w:tbl>
element.-
alignment
¶ Read/write. A member of WD_TABLE_ALIGNMENT or None, specifying the positioning of this table between the page margins.
None
if no setting is specified, causing the effective value to be inherited from the style hierarchy.
-
autofit
¶ True
if column widths can be automatically adjusted to improve the fit of cell contents.False
if table layout is fixed. Column widths are adjusted in either case if total column width exceeds page width. Read/write boolean.
-
cell
(row_idx, col_idx)[source]¶ Return
_Cell
instance correponding to table cell at row_idx, col_idx intersection, where (0, 0) is the top, left-most cell.
-
style
¶ Read/write. A
_TableStyle
object representing the style applied to this table. The default table style for the document (often Normal Table) is returned if the table has no directly-applied style. AssigningNone
to this property removes any directly-applied table style causing it to inherit the default table style of the document. Note that the style name of a table style differs slightly from that displayed in the user interface; a hyphen, if it appears, must be removed. For example, Light Shading - Accent 1 becomes Light Shading Accent 1.
-
table_direction
¶ A member of WD_TABLE_DIRECTION indicating the direction in which the table cells are ordered, e.g. WD_TABLE_DIRECTION.LTR.
None
indicates the value is inherited from the style hierarchy.
-
_Cell
objects¶
-
class
docx.table.
_Cell
(tc, parent)[source]¶ Table cell
-
add_paragraph
(text=u'', style=None)[source]¶ Return a paragraph newly added to the end of the content in this cell. If present, text is added to the paragraph in a single run. If specified, the paragraph style style is applied. If style is not specified or is
None
, the result is as though the ‘Normal’ style was applied. Note that the formatting of text in a cell can be influenced by the table style. text can contain tab (\t
) characters, which are converted to the appropriate XML form for a tab. text can also include newline (\n
) or carriage return (\r
) characters, each of which is converted to a line break.
-
add_table
(rows, cols)[source]¶ Return a table newly added to this cell after any existing cell content, having rows rows and cols columns. An empty paragraph is added after the table because Word requires a paragraph element as the last element in every cell.
-
merge
(other_cell)[source]¶ Return a merged cell created by spanning the rectangular region having this cell and other_cell as diagonal corners. Raises
InvalidSpanError
if the cells do not define a rectangular region.
-
paragraphs
¶ List of paragraphs in the cell. A table cell is required to contain at least one block-level element and end with a paragraph. By default, a new cell contains a single paragraph. Read-only
-
tables
¶ List of tables in the cell, in the order they appear. Read-only.
-
text
¶ The entire contents of this cell as a string of text. Assigning a string to this property replaces all existing content with a single paragraph containing the assigned text in a single run.
-
vertical_alignment
¶ Member of WD_CELL_VERTICAL_ALIGNMENT or None.
A value of
None
indicates vertical alignment for this cell is inherited. AssigningNone
causes any explicitly defined vertical alignment to be removed, restoring inheritance.
-
width
¶ The width of this cell in EMU, or
None
if no explicit width is set.
-
_Row
objects¶
-
class
docx.table.
_Row
(tr, parent)[source]¶ Table row
-
height
¶ Return a
Length
object representing the height of this cell, orNone
if no explicit height is set.
-
height_rule
¶ Return the height rule of this cell as a member of the WD_ROW_HEIGHT_RULE enumeration, or
None
if no explicit height_rule is set.
-