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

Skip to content

Commit 4d93057

Browse files
revossen-asmlSteve Canny
authored andcommitted
acpt: add scenarios for _Row.height, .height_rule
1 parent f02b1d6 commit 4d93057

File tree

4 files changed

+151
-2
lines changed

4 files changed

+151
-2
lines changed

docx/enum/table.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,42 @@
99
)
1010

1111
from .base import (
12-
Enumeration, EnumMember, XmlEnumeration, XmlMappedEnumMember
12+
alias, Enumeration, EnumMember, XmlEnumeration, XmlMappedEnumMember
1313
)
1414

1515

16+
@alias('WD_ROW_HEIGHT')
17+
class WD_ROW_HEIGHT_RULE(XmlEnumeration):
18+
"""
19+
Specifies the rule for determining the height or a table row
20+
21+
Example::
22+
23+
from docx.enum.table import WD_ROW_HEIGHT_RULE
24+
25+
table = document.add_table(3, 3)
26+
table.rows[0].height_rule = WD_ROW_HEIGHT_RULE.EXACTLY
27+
"""
28+
29+
__ms_name__ = "WdRowHeightRule"
30+
31+
__url__ = 'https://msdn.microsoft.com/en-us/library/office/ff193620.aspx'
32+
33+
__members__ = (
34+
XmlMappedEnumMember(
35+
'AUTO', 0, 'auto', 'The row height is adjusted to accommodate th'
36+
'e tallest value in the row.'
37+
),
38+
XmlMappedEnumMember(
39+
'AT_LEAST', 1, 'atLeast', 'The row height is at least a minimum '
40+
'specified value.'
41+
),
42+
XmlMappedEnumMember(
43+
'EXACTLY', 2, 'exact', 'The row height is an exact value.'
44+
),
45+
)
46+
47+
1648
class WD_TABLE_ALIGNMENT(XmlEnumeration):
1749
"""
1850
Specifies table justification type.

features/steps/table.py

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
from behave import given, then, when
1212

1313
from docx import Document
14-
from docx.enum.table import WD_TABLE_ALIGNMENT, WD_TABLE_DIRECTION
14+
from docx.enum.table import (
15+
WD_ROW_HEIGHT_RULE, WD_TABLE_ALIGNMENT, WD_TABLE_DIRECTION
16+
)
1517
from docx.shared import Inches
1618
from docx.table import _Column, _Columns, _Row, _Rows
1719

@@ -139,6 +141,31 @@ def given_a_table_having_two_rows(context):
139141
context.table_ = document.tables[0]
140142

141143

144+
@given('a table row having height of {state}')
145+
def given_a_table_row_having_height_of_state(context, state):
146+
table_idx = {
147+
'no explicit setting': 0,
148+
'2 inches': 2,
149+
'3 inches': 3
150+
}[state]
151+
document = Document(test_docx('tbl-props'))
152+
table = document.tables[table_idx]
153+
context.row = table.rows[0]
154+
155+
156+
@given('a table row having height rule {state}')
157+
def given_a_table_row_having_height_rule_state(context, state):
158+
table_idx = {
159+
'no explicit setting': 0,
160+
'automatic': 1,
161+
'at least': 2,
162+
'exactly': 3
163+
}[state]
164+
document = Document(test_docx('tbl-props'))
165+
table = document.tables[table_idx]
166+
context.row = table.rows[0]
167+
168+
142169
# when =====================================================
143170

144171
@when('I add a 1.0 inch column to the table')
@@ -152,6 +179,20 @@ def when_add_row_to_table(context):
152179
context.row = table.add_row()
153180

154181

182+
@when('I assign {value} to row.height')
183+
def when_I_assign_value_to_row_height(context, value):
184+
new_value = None if value == 'None' else int(value)
185+
context.row.height = new_value
186+
187+
188+
@when('I assign {value} to row.height_rule')
189+
def when_I_assign_value_to_row_height_rule(context, value):
190+
new_value = (
191+
None if value == 'None' else getattr(WD_ROW_HEIGHT_RULE, value)
192+
)
193+
context.row.height_rule = new_value
194+
195+
155196
@when('I assign {value_str} to table.alignment')
156197
def when_I_assign_value_to_table_alignment(context, value_str):
157198
value = {
@@ -266,6 +307,26 @@ def then_can_iterate_over_row_collection(context):
266307
assert actual_count == 2
267308

268309

310+
@then('row.height is {value}')
311+
def then_row_height_is_value(context, value):
312+
expected_height = None if value == 'None' else int(value)
313+
actual_height = context.row.height
314+
assert actual_height == expected_height, (
315+
'expected %s, got %s' % (expected_height, actual_height)
316+
)
317+
318+
319+
@then('row.height_rule is {value}')
320+
def then_row_height_rule_is_value(context, value):
321+
expected_rule = (
322+
None if value == 'None' else getattr(WD_ROW_HEIGHT_RULE, value)
323+
)
324+
actual_rule = context.row.height_rule
325+
assert actual_rule == expected_rule, (
326+
'expected %s, got %s' % (expected_rule, actual_rule)
327+
)
328+
329+
269330
@then('table.alignment is {value_str}')
270331
def then_table_alignment_is_value(context, value_str):
271332
value = {
1.42 KB
Binary file not shown.

features/tbl-row-props.feature

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
Feature: Get and set table row properties
2+
In order to format a table row to my requirements
3+
As a developer using python-docx
4+
I need a way to get and set the properties of a table row
5+
6+
7+
@wip
8+
Scenario Outline: Get Row.height_rule
9+
Given a table row having height rule <state>
10+
Then row.height_rule is <value>
11+
12+
Examples: Row.height_rule value cases
13+
| state | value |
14+
| no explicit setting | None |
15+
| automatic | AUTO |
16+
| at least | AT_LEAST |
17+
18+
19+
@wip
20+
Scenario Outline: Set Row.height_rule
21+
Given a table row having height rule <state>
22+
When I assign <value> to row.height_rule
23+
Then row.height_rule is <value>
24+
25+
Examples: Row.height_rule assignment cases
26+
| state | value |
27+
| no explicit setting | AUTO |
28+
| automatic | AT_LEAST |
29+
| at least | None |
30+
| no explicit setting | None |
31+
32+
33+
@wip
34+
Scenario Outline: Get Row.height
35+
Given a table row having height of <state>
36+
Then row.height is <value>
37+
38+
Examples: Row.height value cases
39+
| state | value |
40+
| no explicit setting | None |
41+
| 2 inches | 1828800 |
42+
| 3 inches | 2743200 |
43+
44+
45+
@wip
46+
Scenario Outline: Set row height
47+
Given a table row having height of <state>
48+
When I assign <value> to row.height
49+
Then row.height is <value>
50+
51+
Examples: Row.height assignment cases
52+
| state | value |
53+
| no explicit setting | 1828800 |
54+
| 2 inches | 2743200 |
55+
| 3 inches | None |
56+
| no explicit setting | None |

0 commit comments

Comments
 (0)