X Tutup
Skip to content

Commit 320496b

Browse files
author
Steve Canny
committed
tbl: add _Cell.text setter
1 parent cc8339b commit 320496b

File tree

6 files changed

+94
-4
lines changed

6 files changed

+94
-4
lines changed

docs/dev/analysis/features/table.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,27 @@ Schema Definitions
274274
</xsd:choice>
275275
</xsd:group>
276276

277+
<xsd:complexType name="CT_TcPr"> <!-- denormalized -->
278+
<xsd:sequence>
279+
<xsd:element name="cnfStyle" type="CT_Cnf" minOccurs="0" maxOccurs="1"/>
280+
<xsd:element name="tcW" type="CT_TblWidth" minOccurs="0" maxOccurs="1"/>
281+
<xsd:element name="gridSpan" type="CT_DecimalNumber" minOccurs="0"/>
282+
<xsd:element name="hMerge" type="CT_HMerge" minOccurs="0"/>
283+
<xsd:element name="vMerge" type="CT_VMerge" minOccurs="0"/>
284+
<xsd:element name="tcBorders" type="CT_TcBorders" minOccurs="0" maxOccurs="1"/>
285+
<xsd:element name="shd" type="CT_Shd" minOccurs="0"/>
286+
<xsd:element name="noWrap" type="CT_OnOff" minOccurs="0"/>
287+
<xsd:element name="tcMar" type="CT_TcMar" minOccurs="0" maxOccurs="1"/>
288+
<xsd:element name="textDirection" type="CT_TextDirection" minOccurs="0" maxOccurs="1"/>
289+
<xsd:element name="tcFitText" type="CT_OnOff" minOccurs="0" maxOccurs="1"/>
290+
<xsd:element name="vAlign" type="CT_VerticalJc" minOccurs="0"/>
291+
<xsd:element name="hideMark" type="CT_OnOff" minOccurs="0"/>
292+
<xsd:element name="headers" type="CT_Headers" minOccurs="0"/>
293+
<xsd:group ref="EG_CellMarkupElements" minOccurs="0" maxOccurs="1"/>
294+
<xsd:element name="tcPrChange" type="CT_TcPrChange" minOccurs="0"/>
295+
</xsd:sequence>
296+
</xsd:complexType>
297+
277298
::
278299

279300
w_CT_Tc =

docx/oxml/table.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,26 @@ class CT_Tc(OxmlBaseElement):
207207
"""
208208
``<w:tc>`` table cell element
209209
"""
210+
def add_p(self):
211+
"""
212+
Return a new <w:p> element that has been added at the end of any
213+
existing cell content.
214+
"""
215+
p = CT_P.new()
216+
self.append(p)
217+
return p
218+
219+
def clear_content(self):
220+
"""
221+
Remove all content child elements, preserving the ``<w:tcPr>``
222+
element if present.
223+
"""
224+
new_children = []
225+
tcPr = self.tcPr
226+
if tcPr is not None:
227+
new_children.append(tcPr)
228+
self[:] = new_children
229+
210230
@classmethod
211231
def new(cls):
212232
"""
@@ -224,3 +244,10 @@ def p_lst(self):
224244
List of <w:p> child elements.
225245
"""
226246
return self.findall(qn('w:p'))
247+
248+
@property
249+
def tcPr(self):
250+
"""
251+
<w:tcPr> child element or |None| if not present.
252+
"""
253+
return self.find(qn('w:tcPr'))

docx/table.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ def text(self, text):
7272
Write-only. Set entire contents of cell to the string *text*. Any
7373
existing content or revisions are replaced.
7474
"""
75-
raise NotImplementedError
75+
tc = self._tc
76+
tc.clear_content()
77+
p = tc.add_p()
78+
r = p.add_r()
79+
r.add_t(text)
7680

7781

7882
class _Column(object):

features/cel-text.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ Feature: Set table cell text
33
As an python-docx developer working with a table
44
I need the ability to set the text of a table cell
55

6-
@wip
76
Scenario: Set table cell text
87
Given a table cell
98
When I assign a string to the cell text attribute

tests/oxml/unitdata/table.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ class CT_TcBuilder(BaseBuilder):
4444
__attrs__ = ('w:id',)
4545

4646

47+
class CT_TcPrBuilder(BaseBuilder):
48+
__tag__ = 'w:tcPr'
49+
__nspfxs__ = ('w',)
50+
__attrs__ = ()
51+
52+
4753
def a_gridCol():
4854
return CT_TblGridColBuilder()
4955

@@ -68,5 +74,9 @@ def a_tc():
6874
return CT_TcBuilder()
6975

7076

77+
def a_tcPr():
78+
return CT_TcPrBuilder()
79+
80+
7181
def a_tr():
7282
return CT_RowBuilder()

tests/test_table.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
from docx.text import Paragraph
1616

1717
from .oxml.unitdata.table import (
18-
a_gridCol, a_tbl, a_tblGrid, a_tblPr, a_tblStyle, a_tc, a_tr
18+
a_gridCol, a_tbl, a_tblGrid, a_tblPr, a_tblStyle, a_tc, a_tcPr, a_tr
1919
)
20-
from .oxml.unitdata.text import a_p
20+
from .oxml.unitdata.text import a_p, a_t, an_r
2121

2222

2323
class DescribeTable(object):
@@ -93,8 +93,37 @@ def it_provides_access_to_the_paragraphs_it_contains(
9393
for p in paragraphs:
9494
assert isinstance(p, Paragraph)
9595

96+
def it_can_replace_its_content_with_a_string_of_text(
97+
self, cell_text_fixture):
98+
cell, text, expected_xml = cell_text_fixture
99+
cell.text = text
100+
assert cell._tc.xml == expected_xml
101+
96102
# fixtures -------------------------------------------------------
97103

104+
@pytest.fixture
105+
def cell_text_fixture(self):
106+
# cell -------------------------
107+
tc = (
108+
a_tc().with_nsdecls().with_child(
109+
a_tcPr()).with_child(
110+
a_p()).with_child(
111+
a_tbl()).with_child(
112+
a_p())
113+
).element
114+
cell = _Cell(tc)
115+
# text -------------------------
116+
text = 'foobar'
117+
# expected_xml -----------------
118+
expected_xml = (
119+
a_tc().with_nsdecls().with_child(
120+
a_tcPr()).with_child(
121+
a_p().with_child(
122+
an_r().with_child(
123+
a_t().with_text(text))))
124+
).xml()
125+
return cell, text, expected_xml
126+
98127
@pytest.fixture
99128
def cell_with_paragraphs(self):
100129
tc = (

0 commit comments

Comments
 (0)
X Tutup