Skip to content

Commit 38eaca6

Browse files
author
Steve Canny
committed
body: add _Body.tables
1 parent 185fbb5 commit 38eaca6

File tree

6 files changed

+57
-3
lines changed

6 files changed

+57
-3
lines changed

docx/oxml/parts.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ def p_lst(self):
4949
"""
5050
return self.findall(qn('w:p'))
5151

52+
@property
53+
def tbl_lst(self):
54+
"""
55+
List of <w:tbl> child elements.
56+
"""
57+
return self.findall(qn('w:tbl'))
58+
5259
def _append_blocklevelelt(self, block_level_elt):
5360
"""
5461
Return *block_level_elt* after appending it to end of

docx/parts.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from docx.opc.oxml import serialize_part_xml
88
from docx.opc.package import Part
99
from docx.oxml.shared import oxml_fromstring
10+
from docx.table import Table
1011
from docx.text import Paragraph
1112

1213

@@ -63,3 +64,11 @@ def clear_content(self):
6364
@property
6465
def paragraphs(self):
6566
return [Paragraph(p) for p in self._body.p_lst]
67+
68+
@property
69+
def tables(self):
70+
"""
71+
A sequence containing all the tables in the document, in the order
72+
they appear.
73+
"""
74+
return [Table(tbl) for tbl in self._body.tbl_lst]

docx/table.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@
77

88
class Table(object):
99
"""
10-
Proxy class for a WordprocessingML table.
10+
Proxy class for a WordprocessingML ``<w:tbl>`` element.
1111
"""
12+
def __init__(self, tbl_elm):
13+
super(Table, self).__init__()
14+
self._tbl = tbl_elm

features/blk-add-table.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ Feature: Add a table
33
As an python-docx developer
44
I need the ability to add a table
55

6-
@wip
76
Scenario: Access a table
87
Given a document containing a table
98
Then I can access the table

tests/oxml/unitdata/table.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# encoding: utf-8
2+
3+
"""
4+
Test data builders for text XML elements
5+
"""
6+
7+
from ...unitdata import BaseBuilder
8+
9+
10+
class CT_TblBuilder(BaseBuilder):
11+
__tag__ = 'w:tbl'
12+
__nspfxs__ = ('w',)
13+
__attrs__ = ()
14+
15+
16+
def a_tbl():
17+
return CT_TblBuilder()

tests/test_parts.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212

1313
from mock import Mock
1414

15+
from docx.table import Table
1516
from docx.text import Paragraph
1617

1718
from .oxml.unitdata.parts import a_body
19+
from .oxml.unitdata.table import a_tbl
1820
from .oxml.unitdata.text import a_p, a_sectPr
1921
from .unitutil import class_mock, function_mock, initializer_mock
2022

@@ -90,7 +92,6 @@ def it_can_clear_itself_of_all_content_it_holds(
9092
body, expected_xml = clear_content_fixture
9193
_body = body.clear_content()
9294
assert body._body.xml == expected_xml
93-
print(body._body.xml)
9495
assert _body is body
9596

9697
def it_provides_access_to_the_paragraphs_it_contains(
@@ -101,6 +102,14 @@ def it_provides_access_to_the_paragraphs_it_contains(
101102
for p in paragraphs:
102103
assert isinstance(p, Paragraph)
103104

105+
def it_provides_access_to_the_tables_it_contains(
106+
self, body_with_tables):
107+
body = body_with_tables
108+
tables = body.tables
109+
assert len(tables) == 2
110+
for table in tables:
111+
assert isinstance(table, Table)
112+
104113
# fixtures -------------------------------------------------------
105114

106115
@pytest.fixture(params=[
@@ -136,6 +145,16 @@ def body_with_paragraphs(self):
136145
)
137146
return _Body(body_elm)
138147

148+
@pytest.fixture
149+
def body_with_tables(self):
150+
body_elm = (
151+
a_body().with_nsdecls()
152+
.with_child(a_tbl())
153+
.with_child(a_tbl())
154+
.element
155+
)
156+
return _Body(body_elm)
157+
139158
@pytest.fixture(params=[False, True])
140159
def clear_content_fixture(self, request):
141160
has_sectPr = request.param

0 commit comments

Comments
 (0)