Skip to content

Commit 725df27

Browse files
author
Steve Canny
committed
tbl: add _Cell.text getter
1 parent 210d7f1 commit 725df27

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

docx/table.py

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

99
from .blkcntnr import BlockItemContainer
10-
from .shared import lazyproperty, Parented, write_only_property
10+
from .shared import lazyproperty, Parented
1111

1212

1313
class Table(Parented):
@@ -141,7 +141,16 @@ def tables(self):
141141
"""
142142
return super(_Cell, self).tables
143143

144-
@write_only_property
144+
@property
145+
def text(self):
146+
"""
147+
The entire contents of this cell as a string of text. Assigning
148+
a string to this property replaces all existing content with a single
149+
paragraph containing the assigned text in a single run.
150+
"""
151+
return '\n'.join(p.text for p in self.paragraphs)
152+
153+
@text.setter
145154
def text(self, text):
146155
"""
147156
Write-only. Set entire contents of cell to the string *text*. Any

tests/test_table.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,11 @@ def it_provides_access_to_the_tables_it_contains(self, tables_fixture):
187187
count += 1
188188
assert count == expected_count
189189

190+
def it_knows_what_text_it_contains(self, text_get_fixture):
191+
cell, expected_text = text_get_fixture
192+
text = cell.text
193+
assert text == expected_text
194+
190195
def it_can_replace_its_content_with_a_string_of_text(
191196
self, text_set_fixture):
192197
cell, text, expected_xml = text_set_fixture
@@ -247,6 +252,19 @@ def tables_fixture(self, request):
247252
cell = _Cell(element(cell_cxml), None)
248253
return cell, expected_count
249254

255+
@pytest.fixture(params=[
256+
('w:tc', ''),
257+
('w:tc/w:p/w:r/w:t"foobar"', 'foobar'),
258+
('w:tc/(w:p/w:r/w:t"foo",w:p/w:r/w:t"bar")', 'foo\nbar'),
259+
('w:tc/(w:tcPr,w:p/w:r/w:t"foobar")', 'foobar'),
260+
('w:tc/w:p/w:r/(w:t"fo",w:tab,w:t"ob",w:br,w:t"ar",w:br)',
261+
'fo\tob\nar\n'),
262+
])
263+
def text_get_fixture(self, request):
264+
tc_cxml, expected_text = request.param
265+
cell = _Cell(element(tc_cxml), None)
266+
return cell, expected_text
267+
250268
@pytest.fixture(params=[
251269
('w:tc/w:p', 'foobar',
252270
'w:tc/w:p/w:r/w:t"foobar"'),

0 commit comments

Comments
 (0)