1010
1111from docx import Document
1212from docx .shared import Inches
13- from docx .table import (
14- _Cell , _Column , _ColumnCells , _Columns , _Row , _RowCells , _Rows
15- )
13+ from docx .table import _Column , _Columns , _Row , _Rows
1614
1715from helpers import test_docx
1816
@@ -24,11 +22,16 @@ def given_a_2x2_table(context):
2422 context .table_ = Document ().add_table (rows = 2 , cols = 2 )
2523
2624
27- @given ('a column cell collection having two cells' )
28- def given_a_column_cell_collection_having_two_cells (context ):
29- docx_path = test_docx ('blk-containing-table' )
30- document = Document (docx_path )
31- context .cells = document .tables [0 ].columns [0 ].cells
25+ @given ('a 3x3 table having {span_state}' )
26+ def given_a_3x3_table_having_span_state (context , span_state ):
27+ table_idx = {
28+ 'only uniform cells' : 0 ,
29+ 'a horizontal span' : 1 ,
30+ 'a vertical span' : 2 ,
31+ 'a combined span' : 3 ,
32+ }[span_state ]
33+ document = Document (test_docx ('tbl-cell-access' ))
34+ context .table_ = document .tables [table_idx ]
3235
3336
3437@given ('a column collection having two columns' )
@@ -38,13 +41,6 @@ def given_a_column_collection_having_two_columns(context):
3841 context .columns = document .tables [0 ].columns
3942
4043
41- @given ('a row cell collection having two cells' )
42- def given_a_row_cell_collection_having_two_cells (context ):
43- docx_path = test_docx ('blk-containing-table' )
44- document = Document (docx_path )
45- context .cells = document .tables [0 ].rows [0 ].cells
46-
47-
4844@given ('a row collection having two rows' )
4945def given_a_row_collection_having_two_rows (context ):
5046 docx_path = test_docx ('blk-containing-table' )
@@ -111,20 +107,6 @@ def given_a_table_having_two_rows(context):
111107 context .table_ = document .tables [0 ]
112108
113109
114- @given ('a table column having two cells' )
115- def given_a_table_column_having_two_cells (context ):
116- docx_path = test_docx ('blk-containing-table' )
117- document = Document (docx_path )
118- context .column = document .tables [0 ].columns [0 ]
119-
120-
121- @given ('a table row having two cells' )
122- def given_a_table_row_having_two_cells (context ):
123- docx_path = test_docx ('blk-containing-table' )
124- document = Document (docx_path )
125- context .row = document .tables [0 ].rows [0 ]
126-
127-
128110# when =====================================================
129111
130112@when ('I add a column to the table' )
@@ -166,15 +148,6 @@ def when_I_set_the_table_autofit_to_setting(context, setting):
166148
167149# then =====================================================
168150
169- @then ('I can access a cell using its row and column indices' )
170- def then_can_access_cell_using_its_row_and_col_indices (context ):
171- table = context .table_
172- for row_idx in range (2 ):
173- for col_idx in range (2 ):
174- cell = table .cell (row_idx , col_idx )
175- assert isinstance (cell , _Cell )
176-
177-
178151@then ('I can access a collection column by index' )
179152def then_can_access_collection_column_by_index (context ):
180153 columns = context .columns
@@ -191,36 +164,6 @@ def then_can_access_collection_row_by_index(context):
191164 assert isinstance (row , _Row )
192165
193166
194- @then ('I can access a column cell by index' )
195- def then_can_access_column_cell_by_index (context ):
196- cells = context .cells
197- for idx in range (2 ):
198- cell = cells [idx ]
199- assert isinstance (cell , _Cell )
200-
201-
202- @then ('I can access a row cell by index' )
203- def then_can_access_row_cell_by_index (context ):
204- cells = context .cells
205- for idx in range (2 ):
206- cell = cells [idx ]
207- assert isinstance (cell , _Cell )
208-
209-
210- @then ('I can access the cell collection of the column' )
211- def then_can_access_cell_collection_of_column (context ):
212- column = context .column
213- cells = column .cells
214- assert isinstance (cells , _ColumnCells )
215-
216-
217- @then ('I can access the cell collection of the row' )
218- def then_can_access_cell_collection_of_row (context ):
219- row = context .row
220- cells = row .cells
221- assert isinstance (cells , _RowCells )
222-
223-
224167@then ('I can access the column collection of the table' )
225168def then_can_access_column_collection_of_table (context ):
226169 table = context .table_
@@ -235,37 +178,13 @@ def then_can_access_row_collection_of_table(context):
235178 assert isinstance (rows , _Rows )
236179
237180
238- @then ('I can get the length of the column cell collection' )
239- def then_can_get_length_of_column_cell_collection (context ):
240- column = context .column
241- cells = column .cells
242- assert len (cells ) == 2
243-
244-
245- @then ('I can get the length of the row cell collection' )
246- def then_can_get_length_of_row_cell_collection (context ):
247- row = context .row
248- cells = row .cells
249- assert len (cells ) == 2
250-
251-
252181@then ('I can get the table style name' )
253182def then_can_get_table_style_name (context ):
254183 table = context .table_
255184 msg = "got '%s'" % table .style
256185 assert table .style == 'LightShading-Accent1' , msg
257186
258187
259- @then ('I can iterate over the column cells' )
260- def then_can_iterate_over_the_column_cells (context ):
261- cells = context .cells
262- actual_count = 0
263- for cell in cells :
264- actual_count += 1
265- assert isinstance (cell , _Cell )
266- assert actual_count == 2
267-
268-
269188@then ('I can iterate over the column collection' )
270189def then_can_iterate_over_column_collection (context ):
271190 columns = context .columns
@@ -276,16 +195,6 @@ def then_can_iterate_over_column_collection(context):
276195 assert actual_count == 2
277196
278197
279- @then ('I can iterate over the row cells' )
280- def then_can_iterate_over_the_row_cells (context ):
281- cells = context .cells
282- actual_count = 0
283- for cell in cells :
284- actual_count += 1
285- assert isinstance (cell , _Cell )
286- assert actual_count == 2
287-
288-
289198@then ('I can iterate over the row collection' )
290199def then_can_iterate_over_row_collection (context ):
291200 rows = context .rows
@@ -296,6 +205,21 @@ def then_can_iterate_over_row_collection(context):
296205 assert actual_count == 2
297206
298207
208+ @then ('table.cell({row}, {col}).text is {expected_text}' )
209+ def then_table_cell_row_col_text_is_text (context , row , col , expected_text ):
210+ table = context .table_
211+ row_idx , col_idx = int (row ), int (col )
212+ cell_text = table .cell (row_idx , col_idx ).text
213+ assert cell_text == expected_text , 'got %s' % cell_text
214+
215+
216+ @then ('the column cells text is {expected_text}' )
217+ def then_the_column_cells_text_is_expected_text (context , expected_text ):
218+ table = context .table_
219+ cells_text = ' ' .join (c .text for col in table .columns for c in col .cells )
220+ assert cells_text == expected_text , 'got %s' % cells_text
221+
222+
299223@then ('the length of the column collection is 2' )
300224def then_len_of_column_collection_is_2 (context ):
301225 columns = context .table_ .columns
@@ -342,6 +266,13 @@ def then_the_reported_width_of_the_cell_is_width(context, width):
342266 )
343267
344268
269+ @then ('the row cells text is {expected_text}' )
270+ def then_the_row_cells_text_is_expected_text (context , expected_text ):
271+ table = context .table_
272+ cells_text = ' ' .join (c .text for row in table .rows for c in row .cells )
273+ assert cells_text == expected_text , 'got %s' % cells_text
274+
275+
345276@then ('the table style matches the name I applied' )
346277def then_table_style_matches_name_applied (context ):
347278 table = context .table_
0 commit comments