Skip to content

Commit 7fb29b1

Browse files
author
Steve Canny
committed
tbl: add Table._column_count
1 parent ce3ecf7 commit 7fb29b1

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

docx/oxml/table.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ def new(cls):
5353
tbl = parse_xml(cls._tbl_xml())
5454
return tbl
5555

56+
@property
57+
def col_count(self):
58+
"""
59+
The number of grid columns in this table.
60+
"""
61+
return len(self.tblGrid.gridCol_lst)
62+
5663
@classmethod
5764
def _tbl_xml(cls):
5865
return (

docx/table.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def _column_count(self):
120120
"""
121121
The number of grid columns in this table.
122122
"""
123-
raise NotImplementedError
123+
return self._tbl.col_count
124124

125125
@property
126126
def _tblPr(self):

tests/test_table.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ def it_knows_it_is_the_table_its_children_belong_to(self, table_fixture):
8080
table = table_fixture
8181
assert table.table is table
8282

83+
def it_knows_its_column_count_to_help(self, column_count_fixture):
84+
table, expected_value = column_count_fixture
85+
column_count = table._column_count
86+
assert column_count == expected_value
87+
8388
# fixtures -------------------------------------------------------
8489

8590
@pytest.fixture
@@ -125,6 +130,13 @@ def autofit_set_fixture(self, request):
125130
expected_xml = xml(expected_tbl_cxml)
126131
return table, new_value, expected_xml
127132

133+
@pytest.fixture
134+
def column_count_fixture(self):
135+
tbl_cxml = 'w:tbl/w:tblGrid/(w:gridCol,w:gridCol,w:gridCol)'
136+
expected_value = 3
137+
table = Table(element(tbl_cxml), None)
138+
return table, expected_value
139+
128140
@pytest.fixture
129141
def row_cells_fixture(self, _cells_, _column_count_):
130142
table = Table(None, None)

0 commit comments

Comments
 (0)