Skip to content

Commit e22465d

Browse files
author
Steve Canny
committed
acpt: add scenarios for Table.alignment
1 parent 537f730 commit e22465d

File tree

6 files changed

+128
-0
lines changed

6 files changed

+128
-0
lines changed

docs/api/enum/WdRowAlignment.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
.. _WdRowAlignment:
2+
3+
``WD_TABLE_ALIGNMENT``
4+
======================
5+
6+
Specifies table justification type.
7+
8+
Example::
9+
10+
from docx.enum.table import WD_TABLE_ALIGNMENT
11+
12+
table = document.add_table(3, 3)
13+
table.alignment = WD_TABLE_ALIGNMENT.CENTER
14+
15+
----
16+
17+
LEFT
18+
Left-aligned
19+
20+
CENTER
21+
Center-aligned.
22+
23+
RIGHT
24+
Right-aligned.

docs/api/enum/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ can be found here:
1111
WdAlignParagraph
1212
WdOrientation
1313
WdSectionStart
14+
WdRowAlignment
1415
WdUnderline

docx/enum/table.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# encoding: utf-8
2+
3+
"""
4+
Enumerations related to tables in WordprocessingML files
5+
"""
6+
7+
from __future__ import absolute_import, print_function, unicode_literals
8+
9+
from .base import XmlEnumeration, XmlMappedEnumMember
10+
11+
12+
class WD_TABLE_ALIGNMENT(XmlEnumeration):
13+
"""
14+
Specifies table justification type.
15+
16+
Example::
17+
18+
from docx.enum.table import WD_TABLE_ALIGNMENT
19+
20+
table = document.add_table(3, 3)
21+
table.alignment = WD_TABLE_ALIGNMENT.CENTER
22+
"""
23+
24+
__ms_name__ = 'WdRowAlignment'
25+
26+
__url__ = ' http://office.microsoft.com/en-us/word-help/HV080607259.aspx'
27+
28+
__members__ = (
29+
XmlMappedEnumMember(
30+
'LEFT', 0, 'left', 'Left-aligned'
31+
),
32+
XmlMappedEnumMember(
33+
'CENTER', 1, 'center', 'Center-aligned.'
34+
),
35+
XmlMappedEnumMember(
36+
'RIGHT', 2, 'right', 'Right-aligned.'
37+
),
38+
)

features/steps/table.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from behave import given, then, when
1212

1313
from docx import Document
14+
from docx.enum.table import WD_TABLE_ALIGNMENT
1415
from docx.shared import Inches
1516
from docx.table import _Column, _Columns, _Row, _Rows
1617

@@ -75,6 +76,19 @@ def given_a_table_having_a_width_of_width_desc(context, width_desc):
7576
context.column = document.tables[0].columns[col_idx]
7677

7778

79+
@given('a table having {alignment} alignment')
80+
def given_a_table_having_alignment_alignment(context, alignment):
81+
table_idx = {
82+
'inherited': 3,
83+
'left': 4,
84+
'right': 5,
85+
'center': 6,
86+
}[alignment]
87+
docx_path = test_docx('tbl-props')
88+
document = Document(docx_path)
89+
context.table_ = document.tables[table_idx]
90+
91+
7892
@given('a table having an applied style')
7993
def given_a_table_having_an_applied_style(context):
8094
docx_path = test_docx('tbl-having-applied-style')
@@ -129,6 +143,18 @@ def when_apply_style_to_table(context):
129143
table.style = 'LightShading-Accent1'
130144

131145

146+
@when('I assign {value_str} to table.alignment')
147+
def when_I_assign_value_to_table_alignment(context, value_str):
148+
value = {
149+
'None': None,
150+
'WD_TABLE_ALIGNMENT.LEFT': WD_TABLE_ALIGNMENT.LEFT,
151+
'WD_TABLE_ALIGNMENT.RIGHT': WD_TABLE_ALIGNMENT.RIGHT,
152+
'WD_TABLE_ALIGNMENT.CENTER': WD_TABLE_ALIGNMENT.CENTER,
153+
}[value_str]
154+
table = context.table_
155+
table.alignment = value
156+
157+
132158
@when('I merge from cell {origin} to cell {other}')
133159
def when_I_merge_from_cell_origin_to_cell_other(context, origin, other):
134160
def cell(table, idx):
@@ -218,6 +244,18 @@ def then_can_iterate_over_row_collection(context):
218244
assert actual_count == 2
219245

220246

247+
@then('table.alignment is {value_str}')
248+
def then_table_alignment_is_value(context, value_str):
249+
value = {
250+
'None': None,
251+
'WD_TABLE_ALIGNMENT.LEFT': WD_TABLE_ALIGNMENT.LEFT,
252+
'WD_TABLE_ALIGNMENT.RIGHT': WD_TABLE_ALIGNMENT.RIGHT,
253+
'WD_TABLE_ALIGNMENT.CENTER': WD_TABLE_ALIGNMENT.CENTER,
254+
}[value_str]
255+
table = context.table_
256+
assert table.alignment == value, 'got %s' % table.alignment
257+
258+
221259
@then('table.cell({row}, {col}).text is {expected_text}')
222260
def then_table_cell_row_col_text_is_text(context, row, col, expected_text):
223261
table = context.table_
5.97 KB
Binary file not shown.

features/tbl-props.feature

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,33 @@ Feature: Get and set table properties
44
I need a way to get and set a table's properties
55

66

7+
@wip
8+
Scenario Outline: Determine table alignment
9+
Given a table having <alignment> alignment
10+
Then table.alignment is <value>
11+
12+
Examples: table alignment settings
13+
| alignment | value |
14+
| inherited | None |
15+
| left | WD_TABLE_ALIGNMENT.LEFT |
16+
| right | WD_TABLE_ALIGNMENT.RIGHT |
17+
| center | WD_TABLE_ALIGNMENT.CENTER |
18+
19+
20+
@wip
21+
Scenario Outline: Set table alignment
22+
Given a table having <alignment> alignment
23+
When I assign <value> to table.alignment
24+
Then table.alignment is <value>
25+
26+
Examples: results of assignment to table.alignment
27+
| alignment | value |
28+
| inherited | WD_TABLE_ALIGNMENT.LEFT |
29+
| left | WD_TABLE_ALIGNMENT.RIGHT |
30+
| right | WD_TABLE_ALIGNMENT.CENTER |
31+
| center | None |
32+
33+
734
Scenario Outline: Get autofit layout setting
835
Given a table having an autofit layout of <autofit-setting>
936
Then the reported autofit setting is <reported-autofit>

0 commit comments

Comments
 (0)