Skip to content

Commit 8d8b3a8

Browse files
committed
- search() fixed by using searchre.search, not .match
- Search tests expanded, we now have 99% test coverage - Running test_docx.py now runs nose tests also
1 parent a501b85 commit 8d8b3a8

File tree

4 files changed

+26
-14
lines changed

4 files changed

+26
-14
lines changed

docx.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -338,22 +338,22 @@ def picture(relationshiplist, picname, picdescription, pixelwidth=None,
338338

339339

340340
def search(document,search):
341-
'''Search a document for a regex, return success / fail results'''
342-
results = False
341+
'''Search a document for a regex, return success / fail result'''
342+
result = False
343343
searchre = re.compile(search)
344344
for element in document.iter():
345-
if element.tag == '{%s}t' % nsprefixes['w']:
345+
if element.tag == '{%s}t' % nsprefixes['w']: # t (text) elements
346346
if element.text:
347-
if searchre.match(element.text):
348-
results = True
349-
return results
347+
if searchre.search(element.text):
348+
result = True
349+
return result
350350

351351
def replace(document,search,replace):
352352
'''Replace all occurences of string with a different string, return updated document'''
353353
newdocument = document
354354
searchre = re.compile(search)
355355
for element in newdocument.iter():
356-
if element.tag == '{%s}t' % nsprefixes['w']:
356+
if element.tag == '{%s}t' % nsprefixes['w']: # t (text) elements
357357
if element.text:
358358
if searchre.search(element.text):
359359
element.text = re.sub(search,replace,element.text)

example-makedocument.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,15 @@
4949
relationships,picpara = picture(relationships,'image1.png','This is a test description')
5050
docbody.append(picpara)
5151

52-
# Search and replace
53-
# FIXME: search broken
54-
print 'Search ...',
52+
# Search and replace
53+
print 'Searching for something in a paragraph ...',
5554
if search(docbody, 'the awesomeness'): print 'found it!'
5655
else: print 'nope.'
56+
57+
print 'Searching for something in a heading ...',
58+
if search(docbody, '200 lines'): print 'found it!'
59+
else: print 'nope.'
60+
5761
print 'Replacing ...',
5862
docbody = replace(docbody,'the awesomeness','the goshdarned awesomeness')
5963
print 'done.'

tests/image1.png

44.1 KB
Loading

tests/test_docx.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44
'''
55
import os
66
import lxml
7-
import nose
87
from docx import *
98

10-
TEST_FILE = 'Short python-docx test.docx'
9+
TEST_FILE = 'ShortTest.docx'
10+
IMAGE1_FILE = 'image1.png'
1111

1212
# --- Setup & Support Functions ---
1313
def setup_module():
1414
'''Set up test fixtures'''
15+
import shutil
16+
if IMAGE1_FILE not in os.listdir('.'):
17+
shutil.copyfile(os.path.join(os.path.pardir,IMAGE1_FILE), IMAGE1_FILE)
1518
testnewdocument()
1619

1720
def teardown_module():
@@ -33,7 +36,7 @@ def simpledoc():
3336
docbody.append(paragraph('Paragraph 2'))
3437
docbody.append(table([['A1','A2','A3'],['B1','B2','B3'],['C1','C2','C3']]))
3538
docbody.append(pagebreak(type='section', orient='portrait'))
36-
relationships,picpara = picture(relationships,'image1.png','This is a test description')
39+
relationships,picpara = picture(relationships,IMAGE1_FILE,'This is a test description')
3740
docbody.append(picpara)
3841
docbody.append(pagebreak(type='section', orient='landscape'))
3942
docbody.append(paragraph('Paragraph 3'))
@@ -45,10 +48,14 @@ def testsearchandreplace():
4548
'''Ensure search and replace functions work'''
4649
document, docbody, relationships = simpledoc()
4750
docbody = document.xpath('/w:document/w:body', namespaces=nsprefixes)[0]
51+
assert search(docbody, 'ing 1')
52+
assert search(docbody, 'ing 2')
53+
assert search(docbody, 'graph 3')
54+
assert search(docbody, 'ist Item')
55+
assert search(docbody, 'A1')
4856
if search(docbody, 'Paragraph 2'):
4957
docbody = replace(docbody,'Paragraph 2','Whacko 55')
5058
assert search(docbody, 'Whacko 55')
51-
assert False # replace works, search fails for some cases
5259

5360
def testtextextraction():
5461
'''Ensure text can be pulled out of a document'''
@@ -99,4 +106,5 @@ def testtable():
99106
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'
100107

101108
if __name__=='__main__':
109+
import nose
102110
nose.main()

0 commit comments

Comments
 (0)