Skip to content

Commit ee184a8

Browse files
author
Steve Canny
committed
tbl: add _Cell.paragraphs
1 parent ce4ab90 commit ee184a8

5 files changed

Lines changed: 80 additions & 1 deletion

File tree

docs/dev/analysis/features/table.rst

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,48 @@ Schema Definitions
284284
</xsd:choice>
285285
</xsd:group>
286286

287+
::
288+
289+
w_CT_Tc =
290+
attribute w:id { s_ST_String }?,
291+
element tcPr { w_CT_TcPr }?,
292+
w_EG_BlockLevelElts+
293+
294+
w_EG_BlockLevelElts = # denormalized
295+
element customXml { w_CT_CustomXmlBlock }
296+
| element p { w_CT_P }
297+
| element sdt { w_CT_SdtBlock }
298+
| element tbl { w_CT_Tbl }
299+
| element altChunk { w_CT_AltChunk }
300+
301+
| element proofErr { w_CT_ProofErr }
302+
| element permStart { w_CT_PermStart }
303+
| element permEnd { w_CT_Perm }
304+
| element ins { w_CT_RunTrackChange }
305+
| element del { w_CT_RunTrackChange }
306+
| element moveFrom { w_CT_RunTrackChange }
307+
| element moveTo { w_CT_RunTrackChange }
308+
309+
| element bookmarkStart { w_CT_Bookmark }
310+
| element bookmarkEnd { w_CT_MarkupRange }
311+
| element moveFromRangeStart { w_CT_MoveBookmark }
312+
| element moveFromRangeEnd { w_CT_MarkupRange }
313+
| element moveToRangeStart { w_CT_MoveBookmark }
314+
| element moveToRangeEnd { w_CT_MarkupRange }
315+
| element commentRangeStart { w_CT_MarkupRange }
316+
| element commentRangeEnd { w_CT_MarkupRange }
317+
| element customXmlInsRangeStart { w_CT_TrackChange }
318+
| element customXmlInsRangeEnd { w_CT_Markup }
319+
| element customXmlDelRangeStart { w_CT_TrackChange }
320+
| element customXmlDelRangeEnd { w_CT_Markup }
321+
| element customXmlMoveFromRangeStart { w_CT_TrackChange }
322+
| element customXmlMoveFromRangeEnd { w_CT_Markup }
323+
| element customXmlMoveToRangeStart { w_CT_TrackChange }
324+
| element customXmlMoveToRangeEnd { w_CT_Markup }
325+
326+
| element oMathPara { m_CT_OMathPara }
327+
| element oMath { m_CT_OMath }
328+
287329

288330
Resources
289331
---------

docx/oxml/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ class ValidationError(Exception):
2323
register_custom_element_class('w:body', CT_Body)
2424
register_custom_element_class('w:document', CT_Document)
2525

26-
from docx.oxml.table import CT_Row, CT_Tbl, CT_TblGrid
26+
from docx.oxml.table import CT_Row, CT_Tbl, CT_TblGrid, CT_Tc
2727
register_custom_element_class('w:tbl', CT_Tbl)
2828
register_custom_element_class('w:tblGrid', CT_TblGrid)
29+
register_custom_element_class('w:tc', CT_Tc)
2930
register_custom_element_class('w:tr', CT_Row)
3031

3132
from docx.oxml.text import CT_P, CT_PPr, CT_R, CT_String, CT_Text

docx/oxml/table.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,10 @@ def new(cls):
185185
p = CT_P.new()
186186
tc.append(p)
187187
return tc
188+
189+
@property
190+
def p_lst(self):
191+
"""
192+
List of <w:p> child elements.
193+
"""
194+
return self.findall(qn('w:p'))

docx/table.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from __future__ import absolute_import, print_function, unicode_literals
88

99
from .shared import lazyproperty
10+
from .text import Paragraph
1011

1112

1213
class Table(object):
@@ -42,6 +43,10 @@ def __init__(self, tc):
4243
super(_Cell, self).__init__()
4344
self._tc = tc
4445

46+
@property
47+
def paragraphs(self):
48+
return [Paragraph(p) for p in self._tc.p_lst]
49+
4550

4651
class _Column(object):
4752
"""

tests/test_table.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
_Cell, _Column, _ColumnCellCollection, _ColumnCollection, _Row,
1313
_RowCellCollection, _RowCollection, Table
1414
)
15+
from docx.text import Paragraph
1516

1617
from .oxml.unitdata.table import a_gridCol, a_tbl, a_tblGrid, a_tc, a_tr
1718
from .oxml.unitdata.text import a_p
@@ -45,6 +46,29 @@ def table(self):
4546
return table
4647

4748

49+
class Describe_Cell(object):
50+
51+
def it_provides_access_to_the_paragraphs_it_contains(
52+
self, cell_with_paragraphs):
53+
cell = cell_with_paragraphs
54+
paragraphs = cell.paragraphs
55+
assert len(paragraphs) == 2
56+
for p in paragraphs:
57+
assert isinstance(p, Paragraph)
58+
59+
# fixtures -------------------------------------------------------
60+
61+
@pytest.fixture
62+
def cell_with_paragraphs(self):
63+
tc = (
64+
a_tc().with_nsdecls()
65+
.with_child(a_p())
66+
.with_child(a_p())
67+
.element
68+
)
69+
return _Cell(tc)
70+
71+
4872
class Describe_Column(object):
4973

5074
def it_provides_access_to_the_column_cells(self, column):

0 commit comments

Comments
 (0)