Skip to content

Commit

Permalink
acpt: add scenarios for Cell.vertical_alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
revossen-asml authored and scanny committed Aug 18, 2018
1 parent c9e5ecb commit fbfaaa6
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 0 deletions.
32 changes: 32 additions & 0 deletions docs/api/enum/WdCellVerticalAlignment.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
.. _WdCellVerticalAlignment:

``WD_CELL_VERTICAL_ALIGNMENT``
==============================

alias: **WD_ALIGN_VERTICAL**

Specifies the vertical alignment of text in one or more cells of a table.

Example::

from docx.enum.table import WD_ALIGN_VERTICAL

table = document.add_table(3, 3)
table.cell(0, 0).vertical_alignment = WD_ALIGN_VERTICAL.BOTTOM

----

TOP
Text is aligned to the top border of the cell.

CENTER
Text is aligned to the center of the cell.

BOTTOM
Text is aligned to the bottom border of the cell.

BOTH
This is an option in the OpenXml spec, but not in Word itself. It's not
clear what Word behavior this setting produces. If you find out please let
us know and we'll update this documentation. Otherwise, probably best to
avoid this option.
1 change: 1 addition & 0 deletions docs/api/enum/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ can be found here:
MsoThemeColorIndex
WdAlignParagraph
WdBuiltinStyle
WdCellVerticalAlignment
WdColorIndex
WdLineSpacing
WdOrientation
Expand Down
41 changes: 41 additions & 0 deletions docx/enum/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,47 @@
)


@alias('WD_ALIGN_VERTICAL')
class WD_CELL_VERTICAL_ALIGNMENT(XmlEnumeration):
"""
alias: **WD_ALIGN_VERTICAL**
Specifies the vertical alignment of text in one or more cells of a table.
Example::
from docx.enum.table import WD_ALIGN_VERTICAL
table = document.add_table(3, 3)
table.cell(0, 0).vertical_alignment = WD_ALIGN_VERTICAL.BOTTOM
"""

__ms_name__ = 'WdCellVerticalAlignment'

__url__ = 'https://msdn.microsoft.com/en-us/library/office/ff193345.aspx'

__members__ = (
XmlMappedEnumMember(
'TOP', 0, 'top', 'Text is aligned to the top border of the cell.'
),
XmlMappedEnumMember(
'CENTER', 1, 'center', 'Text is aligned to the center of the cel'
'l.'
),
XmlMappedEnumMember(
'BOTTOM', 3, 'bottom', 'Text is aligned to the bottom border of '
'the cell.'
),
XmlMappedEnumMember(
'BOTH', 101, 'both', 'This is an option in the OpenXml spec, but'
' not in Word itself. It\'s not clear what Word behavior this se'
'tting produces. If you find out please let us know and we\'ll u'
'pdate this documentation. Otherwise, probably best to avoid thi'
's option.'
),
)


@alias('WD_ROW_HEIGHT')
class WD_ROW_HEIGHT_RULE(XmlEnumeration):
"""
Expand Down
10 changes: 10 additions & 0 deletions docx/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,16 @@ def text(self, text):
r = p.add_r()
r.text = text

@property
def vertical_alignment(self):
"""Member of :ref:`WdCellVerticalAlignment` or None.
A value of |None| indicates vertical alignment for this cell is
inherited. Assigning |None| causes any explicitly defined vertical
alignment to be removed, restoring inheritance.
"""
raise NotImplementedError

@property
def width(self):
"""
Expand Down
28 changes: 28 additions & 0 deletions features/steps/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from behave import given, then, when

from docx import Document
from docx.enum.table import WD_ALIGN_VERTICAL # noqa
from docx.enum.table import (
WD_ROW_HEIGHT_RULE, WD_TABLE_ALIGNMENT, WD_TABLE_DIRECTION
)
Expand Down Expand Up @@ -39,6 +40,19 @@ def given_a_3x3_table_having_span_state(context, span_state):
context.table_ = document.tables[table_idx]


@given('a _Cell object with {state} vertical alignment as cell')
def given_a_Cell_object_with_vertical_alignment_as_cell(context, state):
table_idx = {
'inherited': 0,
'bottom': 1,
'center': 2,
'top': 3,
}[state]
document = Document(test_docx('tbl-props'))
table = document.tables[table_idx]
context.cell = table.cell(0, 0)


@given('a column collection having two columns')
def given_a_column_collection_having_two_columns(context):
docx_path = test_docx('blk-containing-table')
Expand Down Expand Up @@ -179,6 +193,11 @@ def when_add_row_to_table(context):
context.row = table.add_row()


@when('I assign {value} to cell.vertical_alignment')
def when_I_assign_value_to_cell_vertical_alignment(context, value):
context.cell.vertical_alignment = eval(value)


@when('I assign {value} to row.height')
def when_I_assign_value_to_row_height(context, value):
new_value = None if value == 'None' else int(value)
Expand Down Expand Up @@ -257,6 +276,15 @@ def when_I_set_the_table_autofit_to_setting(context, setting):

# then =====================================================

@then('cell.vertical_alignment is {value}')
def then_cell_vertical_alignment_is_value(context, value):
expected_value = eval(value)
actual_value = context.cell.vertical_alignment
assert actual_value is expected_value, (
'cell.vertical_alignment is %s' % actual_value
)


@then('I can access a collection column by index')
def then_can_access_collection_column_by_index(context):
columns = context.columns
Expand Down
Binary file modified features/steps/test_files/tbl-props.docx
Binary file not shown.
26 changes: 26 additions & 0 deletions features/tbl-cell-props.feature
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,32 @@ Feature: Get and set table cell properties
I need a way to get and set the properties of a table cell


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

Examples: Cell.vertical_alignment value cases
| state | value |
| inherited | None |
| bottom | WD_ALIGN_VERTICAL.BOTTOM |
| center | WD_ALIGN_VERTICAL.CENTER |


@wip
Scenario Outline: Set Cell.vertical_alignment
Given a _Cell object with <state> vertical alignment as cell
When I assign <value> to cell.vertical_alignment
Then cell.vertical_alignment is <value>

Examples: Cell.vertical_alignment assignment cases
| state | value |
| inherited | WD_ALIGN_VERTICAL.BOTTOM |
| bottom | WD_ALIGN_VERTICAL.CENTER |
| center | None |
| inherited | None |


Scenario Outline: Get cell width
Given a table cell having a width of <width-setting>
Then the reported width of the cell is <reported-width>
Expand Down

0 comments on commit fbfaaa6

Please sign in to comment.