Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4732b99

Browse files
committed
tbl: add _Cell.vertical_alignment getter
1 parent fbfaaa6 commit 4732b99

File tree

5 files changed

+46
-12
lines changed

5 files changed

+46
-12
lines changed

docx/oxml/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def OxmlElement(nsptag_str, attrs=None, nsdecls=None):
129129

130130
from .table import (
131131
CT_Height, CT_Row, CT_Tbl, CT_TblGrid, CT_TblGridCol, CT_TblLayoutType,
132-
CT_TblPr, CT_TblWidth, CT_Tc, CT_TcPr, CT_TrPr, CT_VMerge
132+
CT_TblPr, CT_TblWidth, CT_Tc, CT_TcPr, CT_TrPr, CT_VerticalJc, CT_VMerge
133133
)
134134
register_element_cls('w:bidiVisual', CT_OnOff)
135135
register_element_cls('w:gridCol', CT_TblGridCol)
@@ -145,6 +145,7 @@ def OxmlElement(nsptag_str, attrs=None, nsdecls=None):
145145
register_element_cls('w:tr', CT_Row)
146146
register_element_cls('w:trHeight', CT_Height)
147147
register_element_cls('w:trPr', CT_TrPr)
148+
register_element_cls('w:vAlign', CT_VerticalJc)
148149
register_element_cls('w:vMerge', CT_VMerge)
149150

150151
from .text.font import (

docx/oxml/table.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
# encoding: utf-8
22

3-
"""
4-
Custom element classes for tables
5-
"""
3+
"""Custom element classes for tables"""
64

75
from __future__ import (
86
absolute_import, division, print_function, unicode_literals
97
)
108

119
from . import parse_xml
12-
from ..enum.table import WD_ROW_HEIGHT_RULE
10+
from ..enum.table import WD_CELL_VERTICAL_ALIGNMENT, WD_ROW_HEIGHT_RULE
1311
from ..exceptions import InvalidSpanError
1412
from .ns import nsdecls, qn
1513
from ..shared import Emu, Twips
@@ -764,6 +762,7 @@ class CT_TcPr(BaseOxmlElement):
764762
tcW = ZeroOrOne('w:tcW', successors=_tag_seq[2:])
765763
gridSpan = ZeroOrOne('w:gridSpan', successors=_tag_seq[3:])
766764
vMerge = ZeroOrOne('w:vMerge', successors=_tag_seq[5:])
765+
vAlign = ZeroOrOne('w:vAlign', successors=_tag_seq[12:])
767766
del _tag_seq
768767

769768
@property
@@ -783,6 +782,18 @@ def grid_span(self, value):
783782
if value > 1:
784783
self.get_or_add_gridSpan().val = value
785784

785+
@property
786+
def vAlign_val(self):
787+
"""Value of `w:val` attribute on `w:vAlign` child.
788+
789+
Value is |None| if `w:vAlign` child is not present. The `w:val`
790+
attribute on `w:vAlign` is required.
791+
"""
792+
vAlign = self.vAlign
793+
if vAlign is None:
794+
return None
795+
return vAlign.val
796+
786797
@property
787798
def vMerge_val(self):
788799
"""
@@ -865,6 +876,11 @@ def trHeight_val(self, value):
865876
trHeight.val = value
866877

867878

879+
class CT_VerticalJc(BaseOxmlElement):
880+
"""`w:vAlign` element, specifying vertical alignment of cell."""
881+
val = RequiredAttribute('w:val', WD_CELL_VERTICAL_ALIGNMENT)
882+
883+
868884
class CT_VMerge(BaseOxmlElement):
869885
"""
870886
``<w:vMerge>`` element, specifying vertical merging behavior of a cell.

docx/table.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,11 @@ def _tblPr(self):
190190

191191

192192
class _Cell(BlockItemContainer):
193-
"""
194-
Table cell
195-
"""
193+
"""Table cell"""
194+
196195
def __init__(self, tc, parent):
197196
super(_Cell, self).__init__(tc, parent)
198-
self._tc = tc
197+
self._tc = self._element = tc
199198

200199
def add_paragraph(self, text='', style=None):
201200
"""
@@ -278,7 +277,10 @@ def vertical_alignment(self):
278277
inherited. Assigning |None| causes any explicitly defined vertical
279278
alignment to be removed, restoring inheritance.
280279
"""
281-
raise NotImplementedError
280+
tcPr = self._element.tcPr
281+
if tcPr is None:
282+
return None
283+
return tcPr.vAlign_val
282284

283285
@property
284286
def width(self):

features/tbl-cell-props.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ Feature: Get and set table cell properties
44
I need a way to get and set the properties of a table cell
55

66

7-
@wip
87
Scenario Outline: Get _Cell.vertical_alignment
98
Given a _Cell object with <state> vertical alignment as cell
109
Then cell.vertical_alignment is <value>

tests/test_table.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from docx.enum.style import WD_STYLE_TYPE
1212
from docx.enum.table import (
13-
WD_ROW_HEIGHT, WD_TABLE_ALIGNMENT, WD_TABLE_DIRECTION
13+
WD_ALIGN_VERTICAL, WD_ROW_HEIGHT, WD_TABLE_ALIGNMENT, WD_TABLE_DIRECTION
1414
)
1515
from docx.oxml import parse_xml
1616
from docx.oxml.table import CT_Tc
@@ -339,6 +339,11 @@ def it_can_replace_its_content_with_a_string_of_text(
339339
cell.text = text
340340
assert cell._tc.xml == expected_xml
341341

342+
def it_knows_its_vertical_alignment(self, alignment_get_fixture):
343+
cell, expected_value = alignment_get_fixture
344+
vertical_alignment = cell.vertical_alignment
345+
assert vertical_alignment == expected_value
346+
342347
def it_knows_its_width_in_EMU(self, width_get_fixture):
343348
cell, expected_width = width_get_fixture
344349
assert cell.width == expected_width
@@ -412,6 +417,17 @@ def add_table_fixture(self, request):
412417
expected_xml = snippet_seq('new-tbl')[1]
413418
return cell, expected_xml
414419

420+
@pytest.fixture(params=[
421+
('w:tc', None),
422+
('w:tc/w:tcPr', None),
423+
('w:tc/w:tcPr/w:vAlign{w:val=bottom}', WD_ALIGN_VERTICAL.BOTTOM),
424+
('w:tc/w:tcPr/w:vAlign{w:val=top}', WD_ALIGN_VERTICAL.TOP),
425+
])
426+
def alignment_get_fixture(self, request):
427+
tc_cxml, expected_value = request.param
428+
cell = _Cell(element(tc_cxml), None)
429+
return cell, expected_value
430+
415431
@pytest.fixture
416432
def merge_fixture(self, tc_, tc_2_, parent_, merged_tc_):
417433
cell, other_cell = _Cell(tc_, parent_), _Cell(tc_2_, parent_)

0 commit comments

Comments
 (0)