Skip to content

Commit 572526b

Browse files
author
Steve Canny
committed
tbl: add Table.alignment getter
1 parent a18b013 commit 572526b

File tree

4 files changed

+47
-9
lines changed

4 files changed

+47
-9
lines changed

docx/oxml/table.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,29 @@ class CT_TblPr(BaseOxmlElement):
134134
``<w:tblPr>`` element, child of ``<w:tbl>``, holds child elements that
135135
define table properties such as style and borders.
136136
"""
137-
tblStyle = ZeroOrOne('w:tblStyle', successors=(
138-
'w:tblpPr', 'w:tblOverlap', 'w:bidiVisual', 'w:tblStyleRowBandSize',
139-
'w:tblStyleColBandSize', 'w:tblW', 'w:jc', 'w:tblCellSpacing',
140-
'w:tblInd', 'w:tblBorders', 'w:shd', 'w:tblLayout', 'w:tblCellMar',
141-
'w:tblLook', 'w:tblCaption', 'w:tblDescription', 'w:tblPrChange'
142-
))
143-
tblLayout = ZeroOrOne('w:tblLayout', successors=(
137+
_tag_seq = (
138+
'w:tblStyle', 'w:tblpPr', 'w:tblOverlap', 'w:bidiVisual',
139+
'w:tblStyleRowBandSize', 'w:tblStyleColBandSize', 'w:tblW', 'w:jc',
140+
'w:tblCellSpacing', 'w:tblInd', 'w:tblBorders', 'w:shd',
144141
'w:tblLayout', 'w:tblCellMar', 'w:tblLook', 'w:tblCaption',
145142
'w:tblDescription', 'w:tblPrChange'
146-
))
143+
)
144+
tblStyle = ZeroOrOne('w:tblStyle', successors=_tag_seq[1:])
145+
jc = ZeroOrOne('w:jc', successors=_tag_seq[8:])
146+
tblLayout = ZeroOrOne('w:tblLayout', successors=_tag_seq[13:])
147+
del _tag_seq
148+
149+
@property
150+
def alignment(self):
151+
"""
152+
Member of :ref:`WdRowAlignment` enumeration or |None|, based on the
153+
contents of the `w:val` attribute of `./w:jc`. |None| if no `w:jc`
154+
element is present.
155+
"""
156+
jc = self.jc
157+
if jc is None:
158+
return None
159+
return jc.val
147160

148161
@property
149162
def autofit(self):

docx/table.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ def add_row(self):
3939
tr.add_tc()
4040
return _Row(tr, self)
4141

42+
@property
43+
def alignment(self):
44+
"""
45+
Read/write. A member of :ref:`WdRowAlignment` or None, specifying the
46+
positioning of this table between the page margins. |None| if no
47+
setting is specified, causing the effective value to be inherited
48+
from the style hierarchy.
49+
"""
50+
return self._tblPr.alignment
51+
4252
@property
4353
def autofit(self):
4454
"""

features/tbl-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 properties
44
I need a way to get and set a table's properties
55

66

7-
@wip
87
Scenario Outline: Determine table alignment
98
Given a table having <alignment> alignment
109
Then table.alignment is <value>

tests/test_table.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import pytest
1010

11+
from docx.enum.table import WD_TABLE_ALIGNMENT
1112
from docx.oxml import parse_xml
1213
from docx.oxml.table import CT_Tc
1314
from docx.shared import Inches
@@ -23,6 +24,10 @@
2324

2425
class DescribeTable(object):
2526

27+
def it_knows_its_alignment_setting(self, alignment_get_fixture):
28+
table, expected_value = alignment_get_fixture
29+
assert table.alignment == expected_value
30+
2631
def it_knows_whether_it_should_autofit(self, autofit_get_fixture):
2732
table, expected_value = autofit_get_fixture
2833
assert table.autofit is expected_value
@@ -117,6 +122,17 @@ def add_row_fixture(self):
117122
expected_xml = _tbl_bldr(rows=2, cols=2).xml()
118123
return table, expected_xml
119124

125+
@pytest.fixture(params=[
126+
('w:tbl/w:tblPr', None),
127+
('w:tbl/w:tblPr/w:jc{w:val=center}', WD_TABLE_ALIGNMENT.CENTER),
128+
('w:tbl/w:tblPr/w:jc{w:val=right}', WD_TABLE_ALIGNMENT.RIGHT),
129+
('w:tbl/w:tblPr/w:jc{w:val=left}', WD_TABLE_ALIGNMENT.LEFT),
130+
])
131+
def alignment_get_fixture(self, request):
132+
tbl_cxml, expected_value = request.param
133+
table = Table(element(tbl_cxml), None)
134+
return table, expected_value
135+
120136
@pytest.fixture(params=[
121137
('w:tbl/w:tblPr', True),
122138
('w:tbl/w:tblPr/w:tblLayout', True),

0 commit comments

Comments
 (0)