GeezerVison is showing github.com — acpt: add scenarios for Cell.vertical_alignment · python-openxml/python-docx@fbfaaa6
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +32Lines changed: 32 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +.. _WdCellVerticalAlignment: | | 2 | + | | 3 | +``WD_CELL_VERTICAL_ALIGNMENT`` | | 4 | +============================== | | 5 | + | | 6 | +alias: **WD_ALIGN_VERTICAL** | | 7 | + | | 8 | +Specifies the vertical alignment of text in one or more cells of a table. | | 9 | + | | 10 | +Example:: | | 11 | + | | 12 | + from docx.enum.table import WD_ALIGN_VERTICAL | | 13 | + | | 14 | + table = document.add_table(3, 3) | | 15 | + table.cell(0, 0).vertical_alignment = WD_ALIGN_VERTICAL.BOTTOM | | 16 | + | | 17 | +---- | | 18 | + | | 19 | +TOP | | 20 | + Text is aligned to the top border of the cell. | | 21 | + | | 22 | +CENTER | | 23 | + Text is aligned to the center of the cell. | | 24 | + | | 25 | +BOTTOM | | 26 | + Text is aligned to the bottom border of the cell. | | 27 | + | | 28 | +BOTH | | 29 | + This is an option in the OpenXml spec, but not in Word itself. It's not | | 30 | + clear what Word behavior this setting produces. If you find out please let | | 31 | + us know and we'll update this documentation. Otherwise, probably best to | | 32 | + avoid this option. |
Collapse file Copy file name to clipboardExpand all lines: docs/api/enum/index.rst +1Lines changed: 1 addition & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -12,6 +12,7 @@ can be found here: | 12 | 12 | MsoThemeColorIndex | 13 | 13 | WdAlignParagraph | 14 | 14 | WdBuiltinStyle | | 15 | + WdCellVerticalAlignment | 15 | 16 | WdColorIndex | 16 | 17 | WdLineSpacing | 17 | 18 | WdOrientation | |
Collapse file Copy file name to clipboardExpand all lines: docx/enum/table.py +41Lines changed: 41 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 13 | 13 | ) | 14 | 14 |
| 15 | 15 |
| | 16 | +@alias('WD_ALIGN_VERTICAL') | | 17 | +class WD_CELL_VERTICAL_ALIGNMENT(XmlEnumeration): | | 18 | + """ | | 19 | + alias: **WD_ALIGN_VERTICAL** | | 20 | +
| | 21 | + Specifies the vertical alignment of text in one or more cells of a table. | | 22 | +
| | 23 | + Example:: | | 24 | +
| | 25 | + from docx.enum.table import WD_ALIGN_VERTICAL | | 26 | +
| | 27 | + table = document.add_table(3, 3) | | 28 | + table.cell(0, 0).vertical_alignment = WD_ALIGN_VERTICAL.BOTTOM | | 29 | + """ | | 30 | + | | 31 | + __ms_name__ = 'WdCellVerticalAlignment' | | 32 | + | | 33 | + __url__ = 'https://msdn.microsoft.com/en-us/library/office/ff193345.aspx' | | 34 | + | | 35 | + __members__ = ( | | 36 | + XmlMappedEnumMember( | | 37 | + 'TOP', 0, 'top', 'Text is aligned to the top border of the cell.' | | 38 | + ), | | 39 | + XmlMappedEnumMember( | | 40 | + 'CENTER', 1, 'center', 'Text is aligned to the center of the cel' | | 41 | + 'l.' | | 42 | + ), | | 43 | + XmlMappedEnumMember( | | 44 | + 'BOTTOM', 3, 'bottom', 'Text is aligned to the bottom border of ' | | 45 | + 'the cell.' | | 46 | + ), | | 47 | + XmlMappedEnumMember( | | 48 | + 'BOTH', 101, 'both', 'This is an option in the OpenXml spec, but' | | 49 | + ' not in Word itself. It\'s not clear what Word behavior this se' | | 50 | + 'tting produces. If you find out please let us know and we\'ll u' | | 51 | + 'pdate this documentation. Otherwise, probably best to avoid thi' | | 52 | + 's option.' | | 53 | + ), | | 54 | + ) | | 55 | + | | 56 | + | 16 | 57 | @alias('WD_ROW_HEIGHT') | 17 | 58 | class WD_ROW_HEIGHT_RULE(XmlEnumeration): | 18 | 59 | """ | |
Collapse file Copy file name to clipboardExpand all lines: docx/table.py +10Lines changed: 10 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -270,6 +270,16 @@ def text(self, text): | 270 | 270 | r = p.add_r() | 271 | 271 | r.text = text | 272 | 272 |
| | 273 | + @property | | 274 | + def vertical_alignment(self): | | 275 | + """Member of :ref:`WdCellVerticalAlignment` or None. | | 276 | +
| | 277 | + A value of |None| indicates vertical alignment for this cell is | | 278 | + inherited. Assigning |None| causes any explicitly defined vertical | | 279 | + alignment to be removed, restoring inheritance. | | 280 | + """ | | 281 | + raise NotImplementedError | | 282 | + | 273 | 283 | @property | 274 | 284 | def width(self): | 275 | 285 | """ | |
Collapse file Copy file name to clipboardExpand all lines: features/steps/table.py +28Lines changed: 28 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 11 | 11 | from behave import given, then, when | 12 | 12 |
| 13 | 13 | from docx import Document | | 14 | +from docx.enum.table import WD_ALIGN_VERTICAL # noqa | 14 | 15 | from docx.enum.table import ( | 15 | 16 | WD_ROW_HEIGHT_RULE, WD_TABLE_ALIGNMENT, WD_TABLE_DIRECTION | 16 | 17 | ) | @@ -39,6 +40,19 @@ def given_a_3x3_table_having_span_state(context, span_state): | 39 | 40 | context.table_ = document.tables[table_idx] | 40 | 41 |
| 41 | 42 |
| | 43 | +@given('a _Cell object with {state} vertical alignment as cell') | | 44 | +def given_a_Cell_object_with_vertical_alignment_as_cell(context, state): | | 45 | + table_idx = { | | 46 | + 'inherited': 0, | | 47 | + 'bottom': 1, | | 48 | + 'center': 2, | | 49 | + 'top': 3, | | 50 | + }[state] | | 51 | + document = Document(test_docx('tbl-props')) | | 52 | + table = document.tables[table_idx] | | 53 | + context.cell = table.cell(0, 0) | | 54 | + | | 55 | + | 42 | 56 | @given('a column collection having two columns') | 43 | 57 | def given_a_column_collection_having_two_columns(context): | 44 | 58 | docx_path = test_docx('blk-containing-table') | @@ -179,6 +193,11 @@ def when_add_row_to_table(context): | 179 | 193 | context.row = table.add_row() | 180 | 194 |
| 181 | 195 |
| | 196 | +@when('I assign {value} to cell.vertical_alignment') | | 197 | +def when_I_assign_value_to_cell_vertical_alignment(context, value): | | 198 | + context.cell.vertical_alignment = eval(value) | | 199 | + | | 200 | + | 182 | 201 | @when('I assign {value} to row.height') | 183 | 202 | def when_I_assign_value_to_row_height(context, value): | 184 | 203 | new_value = None if value == 'None' else int(value) | @@ -257,6 +276,15 @@ def when_I_set_the_table_autofit_to_setting(context, setting): | 257 | 276 |
| 258 | 277 | # then ===================================================== | 259 | 278 |
| | 279 | +@then('cell.vertical_alignment is {value}') | | 280 | +def then_cell_vertical_alignment_is_value(context, value): | | 281 | + expected_value = eval(value) | | 282 | + actual_value = context.cell.vertical_alignment | | 283 | + assert actual_value is expected_value, ( | | 284 | + 'cell.vertical_alignment is %s' % actual_value | | 285 | + ) | | 286 | + | | 287 | + | 260 | 288 | @then('I can access a collection column by index') | 261 | 289 | def then_can_access_collection_column_by_index(context): | 262 | 290 | columns = context.columns | |
Collapse file Copy file name to clipboard Collapse file Copy file name to clipboardExpand all lines: features/tbl-cell-props.feature +26Lines changed: 26 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -4,6 +4,32 @@ Feature: Get and set table cell properties | 4 | 4 | I need a way to get and set the properties of a table cell | 5 | 5 |
| 6 | 6 |
| | 7 | + @wip | | 8 | + Scenario Outline: Get _Cell.vertical_alignment | | 9 | + Given a _Cell object with <state> vertical alignment as cell | | 10 | + Then cell.vertical_alignment is <value> | | 11 | + | | 12 | + Examples: Cell.vertical_alignment value cases | | 13 | + | state | value | | | 14 | + | inherited | None | | | 15 | + | bottom | WD_ALIGN_VERTICAL.BOTTOM | | | 16 | + | center | WD_ALIGN_VERTICAL.CENTER | | | 17 | + | | 18 | + | | 19 | + @wip | | 20 | + Scenario Outline: Set Cell.vertical_alignment | | 21 | + Given a _Cell object with <state> vertical alignment as cell | | 22 | + When I assign <value> to cell.vertical_alignment | | 23 | + Then cell.vertical_alignment is <value> | | 24 | + | | 25 | + Examples: Cell.vertical_alignment assignment cases | | 26 | + | state | value | | | 27 | + | inherited | WD_ALIGN_VERTICAL.BOTTOM | | | 28 | + | bottom | WD_ALIGN_VERTICAL.CENTER | | | 29 | + | center | None | | | 30 | + | inherited | None | | | 31 | + | | 32 | + | 7 | 33 | Scenario Outline: Get cell width | 8 | 34 | Given a table cell having a width of <width-setting> | 9 | 35 | Then the reported width of the cell is <reported-width> | |
|