diff --git a/HISTORY.rst b/HISTORY.rst index 66fdbedc0..e9dc78033 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -8,6 +8,7 @@ NEXT - Fix #149: KeyError on Document.add_table() - Fix #78: feature: add_table() sets cell widths +- Add #106: feature: Table.direction (i.e. right-to-left) 0.8.4 (2015-02-20) diff --git a/docx/oxml/table.py b/docx/oxml/table.py index f68604b29..7b45b0ff9 100644 --- a/docx/oxml/table.py +++ b/docx/oxml/table.py @@ -73,6 +73,14 @@ def bidiVisual_val(self): return None return bidiVisual.val + @bidiVisual_val.setter + def bidiVisual_val(self, value): + tblPr = self.tblPr + if value is None: + tblPr._remove_bidiVisual() + else: + tblPr.get_or_add_bidiVisual().val = value + @property def col_count(self): """ diff --git a/docx/table.py b/docx/table.py index 2b4bf85eb..d0b472fc8 100644 --- a/docx/table.py +++ b/docx/table.py @@ -154,6 +154,10 @@ def table_direction(self): """ return self._element.bidiVisual_val + @table_direction.setter + def table_direction(self, value): + self._element.bidiVisual_val = value + @property def _cells(self): """ diff --git a/features/tbl-props.feature b/features/tbl-props.feature index 8c5ae530a..e588c6e86 100644 --- a/features/tbl-props.feature +++ b/features/tbl-props.feature @@ -66,7 +66,6 @@ Feature: Get and set table properties | left-to-right | LTR | - @wip Scenario Outline: Set table direction Given a table having table direction set When I assign to table.table_direction diff --git a/tests/test_table.py b/tests/test_table.py index 26e05174f..44aec41dd 100644 --- a/tests/test_table.py +++ b/tests/test_table.py @@ -95,6 +95,11 @@ def it_knows_its_direction(self, direction_get_fixture): table, expected_value = direction_get_fixture assert table.table_direction == expected_value + def it_can_change_its_direction(self, direction_set_fixture): + table, new_value, expected_xml = direction_set_fixture + table.table_direction = new_value + assert table._element.xml == expected_xml + def it_knows_its_table_style(self, style_get_fixture): table, style_id_, style_ = style_get_fixture style = table.style @@ -239,6 +244,22 @@ def direction_get_fixture(self, request): table = Table(element(tbl_cxml), None) return table, expected_value + @pytest.fixture(params=[ + ('w:tbl/w:tblPr', WD_TABLE_DIRECTION.RTL, + 'w:tbl/w:tblPr/w:bidiVisual'), + ('w:tbl/w:tblPr/w:bidiVisual', WD_TABLE_DIRECTION.LTR, + 'w:tbl/w:tblPr/w:bidiVisual{w:val=0}'), + ('w:tbl/w:tblPr/w:bidiVisual{w:val=0}', WD_TABLE_DIRECTION.RTL, + 'w:tbl/w:tblPr/w:bidiVisual'), + ('w:tbl/w:tblPr/w:bidiVisual{w:val=1}', None, + 'w:tbl/w:tblPr'), + ]) + def direction_set_fixture(self, request): + tbl_cxml, new_value, expected_cxml = request.param + table = Table(element(tbl_cxml), None) + expected_xml = xml(expected_cxml) + return table, new_value, expected_xml + @pytest.fixture def row_cells_fixture(self, _cells_, _column_count_): table = Table(None, None)