Skip to content

Commit

Permalink
tbl: add CT_Row.trPr
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve Canny committed Feb 22, 2015
1 parent 9605cc0 commit 0c887e8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ NEXT
- Fix #149: KeyError on Document.add_table()
- Fix #78: feature: add_table() sets cell widths
- Add #106: feature: Table.direction (i.e. right-to-left)
- Add #102: feature: add CT_Row.trPr


0.8.4 (2015-02-20)
Expand Down
12 changes: 12 additions & 0 deletions docx/oxml/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class CT_Row(BaseOxmlElement):
"""
``<w:tr>`` element
"""
tblPrEx = ZeroOrOne('w:tblPrEx') # custom inserter below
trPr = ZeroOrOne('w:trPr') # custom inserter below
tc = ZeroOrMore('w:tc')

def tc_at_grid_col(self, idx):
Expand All @@ -49,6 +51,16 @@ def tr_idx(self):
"""
return self.getparent().tr_lst.index(self)

def _insert_tblPrEx(self, tblPrEx):
self.insert(0, tblPrEx)

def _insert_trPr(self, trPr):
tblPrEx = self.tblPrEx
if tblPrEx is not None:
tblPrEx.addnext(trPr)
else:
self.insert(0, trPr)

def _new_tc(self):
return CT_Tc.new()

Expand Down
17 changes: 17 additions & 0 deletions tests/oxml/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,30 @@

class DescribeCT_Row(object):

def it_can_add_a_trPr(self, add_trPr_fixture):
tr, expected_xml = add_trPr_fixture
tr._add_trPr()
assert tr.xml == expected_xml

def it_raises_on_tc_at_grid_col(self, tc_raise_fixture):
tr, idx = tc_raise_fixture
with pytest.raises(ValueError):
tr.tc_at_grid_col(idx)

# fixtures -------------------------------------------------------

@pytest.fixture(params=[
('w:tr', 'w:tr/w:trPr'),
('w:tr/w:tblPrEx', 'w:tr/(w:tblPrEx,w:trPr)'),
('w:tr/w:tc', 'w:tr/(w:trPr,w:tc)'),
('w:tr/(w:sdt,w:del,w:tc)', 'w:tr/(w:trPr,w:sdt,w:del,w:tc)'),
])
def add_trPr_fixture(self, request):
tr_cxml, expected_cxml = request.param
tr = element(tr_cxml)
expected_xml = xml(expected_cxml)
return tr, expected_xml

@pytest.fixture(params=[(0, 0, 3), (1, 0, 1)])
def tc_raise_fixture(self, request):
snippet_idx, row_idx, col_idx = request.param
Expand Down

0 comments on commit 0c887e8

Please sign in to comment.