forked from mikemaccana/python-docx
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_docx.py
More file actions
118 lines (101 loc) · 4.15 KB
/
test_docx.py
File metadata and controls
118 lines (101 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python2.6
'''
Test docx module
'''
import os
import lxml
from docx import *
TEST_FILE = 'ShortTest.docx'
IMAGE1_FILE = 'image1.png'
# --- Setup & Support Functions ---
def setup_module():
'''Set up test fixtures'''
import shutil
if IMAGE1_FILE not in os.listdir('.'):
shutil.copyfile(os.path.join(os.path.pardir,IMAGE1_FILE), IMAGE1_FILE)
testsavedocument()
def teardown_module():
'''Tear down test fixtures'''
if TEST_FILE in os.listdir('.'):
os.remove(TEST_FILE)
def simpledoc():
'''Make a docx (document, relationships) for use in other docx tests'''
doc = newdocx('Python docx testnewdocument','A short example of making docx from Python','Alan Brooks',['python','Office Open XML','Word'])
document = getdocument(doc)
relationships = getrelationshiplist(doc)
docbody = document.xpath('/w:document/w:body', namespaces=nsprefixes)[0]
docbody.append(heading('Heading 1',1) )
docbody.append(heading('Heading 2',2))
docbody.append(paragraph('Paragraph 1'))
for point in ['List Item 1','List Item 2','List Item 3']:
docbody.append(paragraph(point,style='ListNumber'))
docbody.append(pagebreak(type='page'))
docbody.append(paragraph('Paragraph 2'))
docbody.append(table([['A1','A2','A3'],['B1','B2','B3'],['C1','C2','C3']]))
docbody.append(pagebreak(type='section', orient='portrait'))
relationships,picpara = picture(relationships,IMAGE1_FILE,'This is a test description')
docbody.append(picpara)
docbody.append(pagebreak(type='section', orient='landscape'))
docbody.append(paragraph('Paragraph 3'))
doc['word/document.xml'] = document
doc['word/_rels/document.xml.rels'] = wordrelationships(relationships)
return doc
# --- Test Functions ---
def testsearchandreplace():
'''Ensure search and replace functions work'''
doc = simpledoc()
document = getdocument(doc)
docbody = getdocbody(document)
assert search(docbody, 'ing 1')
assert search(docbody, 'ing 2')
assert search(docbody, 'graph 3')
assert search(docbody, 'ist Item')
assert search(docbody, 'A1')
if search(docbody, 'Paragraph 2'):
docbody = replace(docbody,'Paragraph 2','Whacko 55')
assert search(docbody, 'Whacko 55')
def testtextextraction():
'''Ensure text can be pulled out of a document'''
document = opendocx(TEST_FILE)
paratextlist = getdocumenttext(document)
assert len(paratextlist) > 0
def testunsupportedpagebreak():
'''Ensure unsupported page break types are trapped'''
document = newdocument()
docbody = document.xpath('/w:document/w:body', namespaces=nsprefixes)[0]
try:
docbody.append(pagebreak(type='unsup'))
except ValueError:
return # passed
assert False # failed
def testsavedocument():
'''Tests a new document can be saved'''
document = simpledoc()
savedocx(document, TEST_FILE)
def testgetdocument():
'''Ensure an etree element is returned'''
doc = opendocx(TEST_FILE)
document = getdocument(doc)
if isinstance(document,lxml.etree._Element):
pass
else:
assert False
def testmakeelement():
'''Ensure custom elements get created'''
testelement = makeelement('testname',attributes={'testattribute':'testvalue'},tagtext='testtagtext')
assert testelement.tag == '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}testname'
assert testelement.attrib == {'{http://schemas.openxmlformats.org/wordprocessingml/2006/main}testattribute': 'testvalue'}
assert testelement.text == 'testtagtext'
def testparagraph():
'''Ensure paragraph creates p elements'''
testpara = paragraph('paratext',style='BodyText')
assert testpara.tag == '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}p'
pass
def testtable():
'''Ensure tables make sense'''
testtable = table([['A1','A2'],['B1','B2'],['C1','C2']])
ns = '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}'
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'
if __name__=='__main__':
import nose
nose.main()