MCPcopy Index your code
hub / github.com/python-openxml/python-docx / _Cell

Class _Cell

src/docx/table.py:192–311  ·  view source on GitHub ↗

Table cell.

Source from the content-addressed store, hash-verified

190
191
192class _Cell(BlockItemContainer):
193 """Table cell."""
194
195 def __init__(self, tc: CT_Tc, parent: TableParent):
196 super(_Cell, self).__init__(tc, cast("t.ProvidesStoryPart", parent))
197 self._parent = parent
198 self._tc = self._element = tc
199
200 def add_paragraph(self, text: str = "", style: str | ParagraphStyle | None = None):
201 """Return a paragraph newly added to the end of the content in this cell.
202
203 If present, `text` is added to the paragraph in a single run. If specified, the
204 paragraph style `style` is applied. If `style` is not specified or is |None|,
205 the result is as though the 'Normal' style was applied. Note that the formatting
206 of text in a cell can be influenced by the table style. `text` can contain tab
207 (``\\t``) characters, which are converted to the appropriate XML form for a tab.
208 `text` can also include newline (``\\n``) or carriage return (``\\r``)
209 characters, each of which is converted to a line break.
210 """
211 return super(_Cell, self).add_paragraph(text, style)
212
213 def add_table( # pyright: ignore[reportIncompatibleMethodOverride]
214 self, rows: int, cols: int
215 ) -> Table:
216 """Return a table newly added to this cell after any existing cell content.
217
218 The new table will have `rows` rows and `cols` columns.
219
220 An empty paragraph is added after the table because Word requires a paragraph
221 element as the last element in every cell.
222 """
223 width = self.width if self.width is not None else Inches(1)
224 table = super(_Cell, self).add_table(rows, cols, width)
225 self.add_paragraph()
226 return table
227
228 @property
229 def grid_span(self) -> int:
230 """Number of layout-grid cells this cell spans horizontally.
231
232 A "normal" cell has a grid-span of 1. A horizontally merged cell has a grid-span of 2 or
233 more.
234 """
235 return self._tc.grid_span
236
237 def merge(self, other_cell: _Cell):
238 """Return a merged cell created by spanning the rectangular region having this
239 cell and `other_cell` as diagonal corners.
240
241 Raises |InvalidSpanError| if the cells do not define a rectangular region.
242 """
243 tc, tc_2 = self._tc, other_cell._tc
244 merged_tc = tc.merge(tc_2)
245 return _Cell(merged_tc, self._parent)
246
247 @property
248 def paragraphs(self):
249 """List of paragraphs in the cell.

Calls

no outgoing calls

Used in the wild real call sites across dependent graphs

searching dependent graphs…