1
- #!/usr/bin/env python2.6
2
- '''
1
+ #!/usr/bin/env python
2
+
3
+ """
3
4
Test docx module
4
- '''
5
+ """
6
+
5
7
import os
6
8
import lxml
7
- from docx import *
9
+ from docx import (
10
+ appproperties , contenttypes , coreproperties , getdocumenttext , heading ,
11
+ makeelement , newdocument , nsprefixes , opendocx , pagebreak , paragraph ,
12
+ picture , relationshiplist , replace , savedocx , search , table , websettings ,
13
+ wordrelationships
14
+ )
8
15
9
16
TEST_FILE = 'ShortTest.docx'
10
17
IMAGE1_FILE = 'image1.png'
11
18
19
+
12
20
# --- Setup & Support Functions ---
13
21
def setup_module ():
14
- ''' Set up test fixtures'''
22
+ """ Set up test fixtures"""
15
23
import shutil
16
24
if IMAGE1_FILE not in os .listdir ('.' ):
17
- shutil .copyfile (os .path .join (os .path .pardir ,IMAGE1_FILE ), IMAGE1_FILE )
25
+ shutil .copyfile (os .path .join (os .path .pardir , IMAGE1_FILE ), IMAGE1_FILE )
18
26
testnewdocument ()
19
27
28
+
20
29
def teardown_module ():
21
- ''' Tear down test fixtures'''
30
+ """ Tear down test fixtures"""
22
31
if TEST_FILE in os .listdir ('.' ):
23
32
os .remove (TEST_FILE )
24
33
34
+
25
35
def simpledoc (noimagecopy = False ):
26
- ''' Make a docx (document, relationships) for use in other docx tests'''
36
+ """ Make a docx (document, relationships) for use in other docx tests"""
27
37
relationships = relationshiplist ()
28
38
imagefiledict = {}
29
39
document = newdocument ()
30
40
docbody = document .xpath ('/w:document/w:body' , namespaces = nsprefixes )[0 ]
31
- docbody .append (heading ('Heading 1' ,1 ) )
32
- docbody .append (heading ('Heading 2' ,2 ))
41
+ docbody .append (heading ('Heading 1' , 1 ))
42
+ docbody .append (heading ('Heading 2' , 2 ))
33
43
docbody .append (paragraph ('Paragraph 1' ))
34
- for point in ['List Item 1' ,'List Item 2' ,'List Item 3' ]:
35
- docbody .append (paragraph (point ,style = 'ListNumber' ))
44
+ for point in ['List Item 1' , 'List Item 2' , 'List Item 3' ]:
45
+ docbody .append (paragraph (point , style = 'ListNumber' ))
36
46
docbody .append (pagebreak (type = 'page' ))
37
- docbody .append (paragraph ('Paragraph 2' ))
38
- docbody .append (table ([['A1' ,'A2' ,'A3' ],['B1' ,'B2' ,'B3' ],['C1' ,'C2' ,'C3' ]]))
47
+ docbody .append (paragraph ('Paragraph 2' ))
48
+ docbody .append (
49
+ table (
50
+ [
51
+ ['A1' , 'A2' , 'A3' ],
52
+ ['B1' , 'B2' , 'B3' ],
53
+ ['C1' , 'C2' , 'C3' ]
54
+ ]
55
+ )
56
+ )
39
57
docbody .append (pagebreak (type = 'section' , orient = 'portrait' ))
40
58
if noimagecopy :
41
- relationships ,picpara ,imagefiledict = picture (relationships ,IMAGE1_FILE ,'This is a test description' , imagefiledict = imagefiledict )
59
+ relationships , picpara , imagefiledict = picture (
60
+ relationships , IMAGE1_FILE , 'This is a test description' ,
61
+ imagefiledict = imagefiledict
62
+ )
42
63
else :
43
- relationships ,picpara = picture (relationships ,IMAGE1_FILE ,'This is a test description' )
64
+ relationships , picpara = picture (
65
+ relationships , IMAGE1_FILE , 'This is a test description'
66
+ )
44
67
docbody .append (picpara )
45
68
docbody .append (pagebreak (type = 'section' , orient = 'landscape' ))
46
69
docbody .append (paragraph ('Paragraph 3' ))
@@ -52,72 +75,117 @@ def simpledoc(noimagecopy=False):
52
75
53
76
# --- Test Functions ---
54
77
def testsearchandreplace ():
55
- ''' Ensure search and replace functions work'''
78
+ """ Ensure search and replace functions work"""
56
79
document , docbody , relationships = simpledoc ()
57
80
docbody = document .xpath ('/w:document/w:body' , namespaces = nsprefixes )[0 ]
58
81
assert search (docbody , 'ing 1' )
59
82
assert search (docbody , 'ing 2' )
60
83
assert search (docbody , 'graph 3' )
61
84
assert search (docbody , 'ist Item' )
62
85
assert search (docbody , 'A1' )
63
- if search (docbody , 'Paragraph 2' ):
64
- docbody = replace (docbody ,'Paragraph 2' ,'Whacko 55' )
86
+ if search (docbody , 'Paragraph 2' ):
87
+ docbody = replace (docbody , 'Paragraph 2' , 'Whacko 55' )
65
88
assert search (docbody , 'Whacko 55' )
66
89
90
+
67
91
def testtextextraction ():
68
- ''' Ensure text can be pulled out of a document'''
92
+ """ Ensure text can be pulled out of a document"""
69
93
document = opendocx (TEST_FILE )
70
94
paratextlist = getdocumenttext (document )
71
95
assert len (paratextlist ) > 0
72
96
97
+
73
98
def testunsupportedpagebreak ():
74
- ''' Ensure unsupported page break types are trapped'''
99
+ """ Ensure unsupported page break types are trapped"""
75
100
document = newdocument ()
76
101
docbody = document .xpath ('/w:document/w:body' , namespaces = nsprefixes )[0 ]
77
102
try :
78
103
docbody .append (pagebreak (type = 'unsup' ))
79
104
except ValueError :
80
- return # passed
81
- assert False # failed
105
+ return # passed
106
+ assert False # failed
107
+
82
108
83
109
def testnewdocument ():
84
- ''' Test that a new document can be created'''
110
+ """ Test that a new document can be created"""
85
111
document , docbody , relationships = simpledoc ()
86
- coreprops = coreproperties ('Python docx testnewdocument' ,'A short example of making docx from Python' ,'Alan Brooks' ,['python' ,'Office Open XML' ,'Word' ])
87
- savedocx (document , coreprops , appproperties (), contenttypes (), websettings (), wordrelationships (relationships ), TEST_FILE )
112
+ coreprops = coreproperties (
113
+ 'Python docx testnewdocument' ,
114
+ 'A short example of making docx from Python' , 'Alan Brooks' ,
115
+ ['python' , 'Office Open XML' , 'Word' ]
116
+ )
117
+ savedocx (
118
+ document , coreprops , appproperties (), contenttypes (), websettings (),
119
+ wordrelationships (relationships ), TEST_FILE
120
+ )
121
+
88
122
89
123
def testnewdocument_noimagecopy ():
90
- '''Test that a new document can be created'''
91
- document , docbody , relationships , imagefiledict = simpledoc (noimagecopy = True )
92
- coreprops = coreproperties ('Python docx testnewdocument' ,'A short example of making docx from Python' ,'Alan Brooks' ,['python' ,'Office Open XML' ,'Word' ])
93
- savedocx (document , coreprops , appproperties (), contenttypes (), websettings (), wordrelationships (relationships ), TEST_FILE , imagefiledict = imagefiledict )
124
+ """
125
+ Test that a new document can be created
126
+ """
127
+ document , docbody , relationships , imagefiledict = simpledoc (
128
+ noimagecopy = True
129
+ )
130
+ coreprops = coreproperties (
131
+ 'Python docx testnewdocument' ,
132
+ 'A short example of making docx from Python' , 'Alan Brooks' ,
133
+ ['python' , 'Office Open XML' , 'Word' ]
134
+ )
135
+ savedocx (
136
+ document , coreprops , appproperties (), contenttypes (), websettings (),
137
+ wordrelationships (relationships ), TEST_FILE ,
138
+ imagefiledict = imagefiledict
139
+ )
140
+
94
141
95
142
def testopendocx ():
96
- ''' Ensure an etree element is returned'''
97
- if isinstance (opendocx (TEST_FILE ),lxml .etree ._Element ):
143
+ """ Ensure an etree element is returned"""
144
+ if isinstance (opendocx (TEST_FILE ), lxml .etree ._Element ):
98
145
pass
99
146
else :
100
147
assert False
101
148
149
+
102
150
def testmakeelement ():
103
- '''Ensure custom elements get created'''
104
- testelement = makeelement ('testname' ,attributes = {'testattribute' :'testvalue' },tagtext = 'testtagtext' )
105
- assert testelement .tag == '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}testname'
106
- assert testelement .attrib == {'{http://schemas.openxmlformats.org/wordprocessingml/2006/main}testattribute' : 'testvalue' }
151
+ """Ensure custom elements get created"""
152
+ testelement = makeelement (
153
+ 'testname' ,
154
+ attributes = {'testattribute' : 'testvalue' },
155
+ tagtext = 'testtagtext'
156
+ )
157
+ assert testelement .tag == (
158
+ '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}testn'
159
+ 'ame'
160
+ )
161
+ assert testelement .attrib == {
162
+ '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}testa'
163
+ 'ttribute' : 'testvalue'
164
+ }
107
165
assert testelement .text == 'testtagtext'
108
166
167
+
109
168
def testparagraph ():
110
- '''Ensure paragraph creates p elements'''
111
- testpara = paragraph ('paratext' ,style = 'BodyText' )
112
- assert testpara .tag == '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}p'
169
+ """Ensure paragraph creates p elements"""
170
+ testpara = paragraph ('paratext' , style = 'BodyText' )
171
+ assert testpara .tag == (
172
+ '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}p'
173
+ )
113
174
pass
114
-
175
+
176
+
115
177
def testtable ():
116
- '''Ensure tables make sense'''
117
- testtable = table ([['A1' ,'A2' ],['B1' ,'B2' ],['C1' ,'C2' ]])
118
- ns = '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}'
119
- assert testtable .xpath ('/ns0:tbl/ns0:tr[2]/ns0:tc[2]/ns0:p/ns0:r/ns0:t' ,namespaces = {'ns0' :'http://schemas.openxmlformats.org/wordprocessingml/2006/main' })[0 ].text == 'B2'
178
+ """Ensure tables make sense"""
179
+ testtable = table ([['A1' , 'A2' ], ['B1' , 'B2' ], ['C1' , 'C2' ]])
180
+ assert (
181
+ testtable .xpath (
182
+ '/ns0:tbl/ns0:tr[2]/ns0:tc[2]/ns0:p/ns0:r/ns0:t' ,
183
+ namespaces = {'ns0' : ('http://schemas.openxmlformats.org/wordproce'
184
+ 'ssingml/2006/main' )}
185
+ )[0 ].text == 'B2'
186
+ )
187
+
120
188
121
- if __name__ == '__main__' :
189
+ if __name__ == '__main__' :
122
190
import nose
123
191
nose .main ()
0 commit comments