Skip to content

Commit e51ae26

Browse files
committed
tbl: add _Cell.vertical_alignment setter
1 parent 4732b99 commit e51ae26

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

docx/oxml/table.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,13 @@ def vAlign_val(self):
794794
return None
795795
return vAlign.val
796796

797+
@vAlign_val.setter
798+
def vAlign_val(self, value):
799+
if value is None:
800+
self._remove_vAlign()
801+
return
802+
self.get_or_add_vAlign().val = value
803+
797804
@property
798805
def vMerge_val(self):
799806
"""

docx/table.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,11 @@ def vertical_alignment(self):
282282
return None
283283
return tcPr.vAlign_val
284284

285+
@vertical_alignment.setter
286+
def vertical_alignment(self, value):
287+
tcPr = self._element.get_or_add_tcPr()
288+
tcPr.vAlign_val = value
289+
285290
@property
286291
def width(self):
287292
"""

features/tbl-cell-props.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ Feature: Get and set table cell properties
1515
| center | WD_ALIGN_VERTICAL.CENTER |
1616

1717

18-
@wip
1918
Scenario Outline: Set Cell.vertical_alignment
2019
Given a _Cell object with <state> vertical alignment as cell
2120
When I assign <value> to cell.vertical_alignment

tests/test_table.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# encoding: utf-8
22

3-
"""
4-
Test suite for the docx.table module
5-
"""
3+
"""Test suite for the docx.table module"""
64

75
from __future__ import absolute_import, print_function, unicode_literals
86

@@ -344,6 +342,11 @@ def it_knows_its_vertical_alignment(self, alignment_get_fixture):
344342
vertical_alignment = cell.vertical_alignment
345343
assert vertical_alignment == expected_value
346344

345+
def it_can_change_its_vertical_alignment(self, alignment_set_fixture):
346+
cell, new_value, expected_xml = alignment_set_fixture
347+
cell.vertical_alignment = new_value
348+
assert cell._element.xml == expected_xml
349+
347350
def it_knows_its_width_in_EMU(self, width_get_fixture):
348351
cell, expected_width = width_get_fixture
349352
assert cell.width == expected_width
@@ -428,6 +431,26 @@ def alignment_get_fixture(self, request):
428431
cell = _Cell(element(tc_cxml), None)
429432
return cell, expected_value
430433

434+
@pytest.fixture(params=[
435+
('w:tc', WD_ALIGN_VERTICAL.TOP,
436+
'w:tc/w:tcPr/w:vAlign{w:val=top}'),
437+
('w:tc/w:tcPr', WD_ALIGN_VERTICAL.CENTER,
438+
'w:tc/w:tcPr/w:vAlign{w:val=center}'),
439+
('w:tc/w:tcPr/w:vAlign{w:val=center}', WD_ALIGN_VERTICAL.BOTTOM,
440+
'w:tc/w:tcPr/w:vAlign{w:val=bottom}'),
441+
('w:tc/w:tcPr/w:vAlign{w:val=center}', None,
442+
'w:tc/w:tcPr'),
443+
('w:tc', None,
444+
'w:tc/w:tcPr'),
445+
('w:tc/w:tcPr', None,
446+
'w:tc/w:tcPr'),
447+
])
448+
def alignment_set_fixture(self, request):
449+
cxml, new_value, expected_cxml = request.param
450+
cell = _Cell(element(cxml), None)
451+
expected_xml = xml(expected_cxml)
452+
return cell, new_value, expected_xml
453+
431454
@pytest.fixture
432455
def merge_fixture(self, tc_, tc_2_, parent_, merged_tc_):
433456
cell, other_cell = _Cell(tc_, parent_), _Cell(tc_2_, parent_)

0 commit comments

Comments
 (0)