Skip to content

Commit 1e975b6

Browse files
author
Steve Canny
committed
acpt: add tbl-item-access.feature
1 parent eca907e commit 1e975b6

3 files changed

Lines changed: 78 additions & 15 deletions

File tree

docs/dev/analysis/features/table.rst

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,6 @@ Open questions
1616
* What about ranges? Why is that a good idea?
1717

1818

19-
Candidate Protocol
20-
------------------
21-
22-
::
23-
24-
>>> row_count, col_count = 2, 2
25-
>>> table = body.add_table(row_count, col_count)
26-
>>> cell = table.cell(0, 0)
27-
>>> p = cell.content[0]
28-
>>> p.text = 'foobar'
29-
>>> p = cell.content.add_paragraph('barfoo')
30-
>>> p.text
31-
'barfoo'
32-
33-
3419
Table access protocol
3520
---------------------
3621

@@ -43,6 +28,34 @@ Table access protocol
4328
... ..do table-y things..
4429

4530

31+
Candidate Protocol
32+
------------------
33+
34+
::
35+
36+
>>> table = body.add_table(rows=1, cols=2)
37+
>>> header_cells = table.rows[0].cells
38+
>>> header_cells[0].text = 'Foo Name'
39+
>>> header_cells[1].text = 'Bar Value'
40+
>>> for obj in data_records:
41+
... row_cells = table.add_row().cells
42+
... row_cells[0].text = obj.str_fld_a
43+
... row_cells[1].text = str(obj.int_fld_b)
44+
...
45+
46+
47+
Other protocol notions
48+
----------------------
49+
50+
::
51+
52+
>>> cell = table.cell(0, 0)
53+
>>> p = cell.content[0]
54+
>>> p.text = 'foobar'
55+
>>> p = cell.content.add_paragraph('barfoo')
56+
>>> p.text
57+
'barfoo'
58+
4659

4760
MS API
4861
------

features/steps/table.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# encoding: utf-8
2+
3+
"""
4+
Step implementations for table-related features
5+
"""
6+
7+
from __future__ import absolute_import, print_function, unicode_literals
8+
9+
from behave import given, then
10+
11+
from docx import Document
12+
from docx.table import _Row
13+
14+
from .helpers import test_docx
15+
16+
17+
# given ===================================================
18+
19+
@given('a table having two rows')
20+
def given_a_table_having_two_rows(context):
21+
docx_path = test_docx('blk-containing-table')
22+
document = Document(docx_path)
23+
context.table_ = document.body.tables[0]
24+
25+
26+
# then =====================================================
27+
28+
@then('the length of its row collection is 2')
29+
def then_len_of_row_collection_is_2(context):
30+
rows = context.table_.rows
31+
assert len(rows) == 2
32+
33+
34+
@then('each item in its row collection is a table row')
35+
def then_each_item_in_row_collection_is_a_table_row(context):
36+
rows = context.table_.rows
37+
count = 0
38+
for item in rows:
39+
count += 1
40+
assert isinstance(item, _Row)
41+
assert count == 2

features/tbl-item-access.feature

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Feature: Access table rows, columns, and cells
2+
In order to query and modify individual table items
3+
As an python-docx developer
4+
I need the ability to access table rows, columns, and cells
5+
6+
Scenario: Access table row collection
7+
Given a table having two rows
8+
Then the length of its row collection is 2
9+
And each item in its row collection is a table row

0 commit comments

Comments
 (0)