Skip to content

Commit 56a5af9

Browse files
author
Steve Canny
committed
tbl: add _Cell.add_paragraph()
* use super() rather than direct inheritance to allow custom docstring
1 parent dc94243 commit 56a5af9

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

docx/table.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,20 @@ def __init__(self, tc, parent):
100100
super(_Cell, self).__init__(tc, parent)
101101
self._tc = tc
102102

103+
def add_paragraph(self, text='', style=None):
104+
"""
105+
Return a paragraph newly added to the end of the content in this
106+
cell. If present, *text* is added to the paragraph in a single run.
107+
If specified, the paragraph style *style* is applied. If *style* is
108+
not specified or is |None|, the result is as though the 'Normal'
109+
style was applied. Note that the formatting of text in a cell can be
110+
influenced by the table style. *text* can contain tab (``\\t``)
111+
characters, which are converted to the appropriate XML form for
112+
a tab. *text* can also include newline (``\\n``) or carriage return
113+
(``\\r``) characters, each of which is converted to a line break.
114+
"""
115+
return super(_Cell, self).add_paragraph(text, style)
116+
103117
@property
104118
def paragraphs(self):
105119
"""

tests/test_table.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,12 @@ def table(self):
151151

152152
class Describe_Cell(object):
153153

154+
def it_can_add_a_paragraph(self, add_paragraph_fixture):
155+
cell, expected_xml = add_paragraph_fixture
156+
p = cell.add_paragraph()
157+
assert cell._tc.xml == expected_xml
158+
assert isinstance(p, Paragraph)
159+
154160
def it_provides_access_to_the_paragraphs_it_contains(
155161
self, paragraphs_fixture):
156162
cell = paragraphs_fixture
@@ -181,6 +187,17 @@ def it_can_change_its_width(self, width_set_fixture):
181187

182188
# fixtures -------------------------------------------------------
183189

190+
@pytest.fixture(params=[
191+
('w:tc', 'w:tc/w:p'),
192+
('w:tc/w:p', 'w:tc/(w:p, w:p)'),
193+
('w:tc/w:tbl', 'w:tc/(w:tbl, w:p)'),
194+
])
195+
def add_paragraph_fixture(self, request):
196+
tc_cxml, after_tc_cxml = request.param
197+
cell = _Cell(element(tc_cxml), None)
198+
expected_xml = xml(after_tc_cxml)
199+
return cell, expected_xml
200+
184201
@pytest.fixture
185202
def paragraphs_fixture(self):
186203
return _Cell(element('w:tc/(w:p, w:p)'), None)

0 commit comments

Comments
 (0)