Skip to content

Commit dbf5cd2

Browse files
author
Steve Canny
committed
oxml: add CT_Document type
1 parent a382f5a commit dbf5cd2

File tree

5 files changed

+51
-3
lines changed

5 files changed

+51
-3
lines changed

docx/oxml/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"""
1414

1515
from docx.oxml.base import register_custom_element_class
16-
from docx.oxml.parts import CT_Body
16+
from docx.oxml.parts import CT_Body, CT_Document
1717
from docx.oxml.text import CT_P, CT_R, CT_Text
1818

1919

@@ -22,6 +22,7 @@
2222
# ===========================================================================
2323

2424
register_custom_element_class('w:body', CT_Body)
25+
register_custom_element_class('w:document', CT_Document)
2526
register_custom_element_class('w:p', CT_P)
2627
register_custom_element_class('w:r', CT_R)
2728
register_custom_element_class('w:t', CT_Text)

docx/oxml/parts.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
from docx.oxml.text import CT_P
1616

1717

18+
class CT_Document(OxmlBaseElement):
19+
"""
20+
``<w:document>`` element, the root element of a document.xml file.
21+
"""
22+
23+
1824
class CT_Body(OxmlBaseElement):
1925
"""
2026
``<w:body>``, the container element for the main document story in

features/add_paragraph.feature

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

6-
@wip
76
Scenario: Add a paragraph to a document created from the default template
87
Given a new document created from the default template
98
When I add a new paragraph to the body

tests/oxml/test_parts.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99

1010
"""Test suite for the docx.oxml.parts module."""
1111

12+
from docx.oxml.parts import CT_Body
1213
from docx.oxml.text import CT_P
1314

14-
from ..unitdata import a_body
15+
from ..unitdata import a_body, a_document
1516

1617

1718
class DescribeCT_Body(object):
@@ -32,3 +33,10 @@ def it_can_add_a_p_to_itself(self):
3233
# verify -------------------
3334
assert body.xml == after_body_bldr.xml
3435
assert isinstance(p, CT_P)
36+
37+
38+
class DescribeCT_Document(object):
39+
40+
def it_holds_a_body_element(self):
41+
document = a_document().with_body().element
42+
assert isinstance(document.body, CT_Body)

tests/unitdata.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,35 @@ def xml(self):
8484
return xml
8585

8686

87+
class CT_DocumentBuilder(BaseBuilder):
88+
"""
89+
XML data builder for CT_Document (<w:document>) element, the root element
90+
in document.xml files.
91+
"""
92+
def __init__(self):
93+
"""Establish instance variables with default values"""
94+
super(CT_DocumentBuilder, self).__init__()
95+
self._body = None
96+
97+
def with_body(self):
98+
"""Add an empty body element"""
99+
self._body = a_body().with_indent(2)
100+
return self
101+
102+
@property
103+
def xml(self):
104+
"""
105+
Return XML string based on settings accumulated via method calls.
106+
"""
107+
if not self._body:
108+
return '<w:document %s/>\n' % nsdecls('w')
109+
110+
xml = '<w:document %s>\n' % nsdecls('w')
111+
xml += self._body.xml
112+
xml += '</w:document>\n'
113+
return xml
114+
115+
87116
class CT_PBuilder(BaseBuilder):
88117
"""
89118
Test data builder for a CT_P (<w:p>) XML element that appears within the
@@ -190,6 +219,11 @@ def a_body():
190219
return CT_BodyBuilder()
191220

192221

222+
def a_document():
223+
"""Return a CT_DocumentBuilder instance"""
224+
return CT_DocumentBuilder()
225+
226+
193227
def a_p():
194228
"""Return a CT_PBuilder instance"""
195229
return CT_PBuilder()

0 commit comments

Comments
 (0)