File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1313"""
1414
1515from 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
1717from docx .oxml .text import CT_P , CT_R , CT_Text
1818
1919
2222# ===========================================================================
2323
2424register_custom_element_class ('w:body' , CT_Body )
25+ register_custom_element_class ('w:document' , CT_Document )
2526register_custom_element_class ('w:p' , CT_P )
2627register_custom_element_class ('w:r' , CT_R )
2728register_custom_element_class ('w:t' , CT_Text )
Original file line number Diff line number Diff line change 1515from 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+
1824class CT_Body (OxmlBaseElement ):
1925 """
2026 ``<w:body>``, the container element for the main document story in
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 99
1010"""Test suite for the docx.oxml.parts module."""
1111
12+ from docx .oxml .parts import CT_Body
1213from docx .oxml .text import CT_P
1314
14- from ..unitdata import a_body
15+ from ..unitdata import a_body , a_document
1516
1617
1718class 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 )
Original file line number Diff line number Diff 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+
87116class 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+
193227def a_p ():
194228 """Return a CT_PBuilder instance"""
195229 return CT_PBuilder ()
You can’t perform that action at this time.
0 commit comments