Skip to content

Commit

Permalink
tbl: rewrite Table.add_column()
Browse files Browse the repository at this point in the history
Add width parameter.
  • Loading branch information
Steve Canny committed Feb 21, 2015
1 parent 96ce9e3 commit 7541f20
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 10 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ NEXT
++++++++++++++++++

- Fix #149: KeyError on Document.add_table()
- Fix #78: feature: add_table() sets cell widths


0.8.4 (2015-02-20)
Expand Down
9 changes: 6 additions & 3 deletions docx/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ def __init__(self, tbl, parent):
super(Table, self).__init__(parent)
self._element = self._tbl = tbl

def add_column(self):
def add_column(self, width):
"""
Return a |_Column| instance, newly added rightmost to the table.
Return a |_Column| object of *width*, newly added rightmost to the
table.
"""
tblGrid = self._tbl.tblGrid
gridCol = tblGrid.add_gridCol()
gridCol.w = width
for tr in self._tbl.tr_lst:
tr.add_tc()
tc = tr.add_tc()
tc.width = width
return _Column(gridCol, self)

def add_row(self):
Expand Down
1 change: 0 additions & 1 deletion features/tbl-add-row-or-col.feature
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Feature: Add a row or column to a table
And the width of each cell is 3.0 inches


@wip
Scenario: Add a column to a table
Given a 2 x 2 table
When I add a 1.0 inch column to the table
Expand Down
52 changes: 52 additions & 0 deletions tests/test_files/snippets/add-row-col.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,55 @@
</w:tc>
</w:tr>
</w:tbl>

<w:tbl xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:tblPr>
<w:tblW w:type="auto" w:w="0"/>
<w:tblLook w:firstColumn="1" w:firstRow="1" w:lastColumn="0" w:lastRow="0" w:noHBand="0" w:noVBand="1" w:val="04A0"/>
</w:tblPr>
<w:tblGrid>
<w:gridCol w:w="720"/>
<w:gridCol w:w="1440"/>
<w:gridCol w:w="2160"/>
</w:tblGrid>
<w:tr>
<w:tc>
<w:tcPr>
<w:tcW w:type="dxa" w:w="720"/>
</w:tcPr>
<w:p/>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:type="dxa" w:w="1440"/>
</w:tcPr>
<w:p/>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:type="dxa" w:w="2160"/>
</w:tcPr>
<w:p/>
</w:tc>
</w:tr>
<w:tr>
<w:tc>
<w:tcPr>
<w:tcW w:type="dxa" w:w="720"/>
</w:tcPr>
<w:p/>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:type="dxa" w:w="1440"/>
</w:tcPr>
<w:p/>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:type="dxa" w:w="2160"/>
</w:tcPr>
<w:p/>
</w:tc>
</w:tr>
</w:tbl>
15 changes: 9 additions & 6 deletions tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,12 @@ def it_can_add_a_row(self, add_row_fixture):
assert row._parent is table

def it_can_add_a_column(self, add_column_fixture):
table, expected_xml = add_column_fixture
column = table.add_column()
table, width, expected_xml = add_column_fixture
column = table.add_column(width)
assert table._tbl.xml == expected_xml
assert isinstance(column, _Column)
assert column._gridCol is table._tbl.tblGrid.gridCol_lst[1]
assert column._gridCol is table._tbl.tblGrid.gridCol_lst[-1]
assert column._parent is table

def it_provides_access_to_its_cells_to_help(self, cells_fixture):
table, cell_count, unique_count, matches = cells_fixture
Expand All @@ -125,10 +126,12 @@ def it_provides_access_to_its_cells_to_help(self, cells_fixture):

@pytest.fixture
def add_column_fixture(self):
tbl = _tbl_bldr(2, 1).element
snippets = snippet_seq('add-row-col')
tbl = parse_xml(snippets[0])
table = Table(tbl, None)
expected_xml = _tbl_bldr(2, 2).xml()
return table, expected_xml
width = Inches(1.5)
expected_xml = snippets[2]
return table, width, expected_xml

@pytest.fixture
def add_row_fixture(self):
Expand Down

0 comments on commit 7541f20

Please sign in to comment.