Skip to content

Commit cc8339b

Browse files
author
Steve Canny
committed
acpt: add cel-text.feature
1 parent c0281d6 commit cc8339b

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

docx/shared.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,13 @@ def get_prop_value(obj):
2525
return value
2626

2727
return property(get_prop_value, doc=docstring)
28+
29+
30+
def write_only_property(f):
31+
"""
32+
@write_only_property decorator. Creates a property (descriptor attribute)
33+
that accepts assignment, but not getattr (use in an expression).
34+
"""
35+
docstring = f.__doc__
36+
37+
return property(fset=f, doc=docstring)

docx/table.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from __future__ import absolute_import, print_function, unicode_literals
88

9-
from .shared import lazyproperty
9+
from .shared import lazyproperty, write_only_property
1010
from .text import Paragraph
1111

1212

@@ -66,6 +66,14 @@ def __init__(self, tc):
6666
def paragraphs(self):
6767
return [Paragraph(p) for p in self._tc.p_lst]
6868

69+
@write_only_property
70+
def text(self, text):
71+
"""
72+
Write-only. Set entire contents of cell to the string *text*. Any
73+
existing content or revisions are replaced.
74+
"""
75+
raise NotImplementedError
76+
6977

7078
class _Column(object):
7179
"""

features/cel-text.feature

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Feature: Set table cell text
2+
In order to quickly populate a table cell with regular text
3+
As an python-docx developer working with a table
4+
I need the ability to set the text of a table cell
5+
6+
@wip
7+
Scenario: Set table cell text
8+
Given a table cell
9+
When I assign a string to the cell text attribute
10+
Then the cell contains the string I assigned

features/steps/cell.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# encoding: utf-8
2+
3+
"""
4+
Step implementations for table cell-related features
5+
"""
6+
7+
from __future__ import absolute_import, print_function, unicode_literals
8+
9+
from behave import given, then, when
10+
11+
from docx import Document
12+
13+
14+
# given ===================================================
15+
16+
@given('a table cell')
17+
def given_a_table_cell(context):
18+
table = Document().body.add_table(rows=2, cols=2)
19+
context.cell = table.cell(0, 0)
20+
21+
22+
# when =====================================================
23+
24+
@when('I assign a string to the cell text attribute')
25+
def when_assign_string_to_cell_text_attribute(context):
26+
cell = context.cell
27+
text = 'foobar'
28+
cell.text = text
29+
context.expected_text = text
30+
31+
32+
# then =====================================================
33+
34+
@then('the cell contains the string I assigned')
35+
def then_cell_contains_string_assigned(context):
36+
cell, expected_text = context.cell, context.expected_text
37+
text = cell.paragraphs[0].runs[0].text
38+
msg = "expected '%s', got '%s'" % (expected_text, text)
39+
assert text == expected_text, msg

0 commit comments

Comments
 (0)