def testsearchandreplace(): """Ensure search and replace functions work""" document, docbody, relationships = simpledoc() docbody = document.xpath('/w:document/w:body', namespaces=nsprefixes)[0] 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 testsearchandreplace(): """Ensure search and replace functions work""" document, docbody, relationships = simpledoc() docbody = document.xpath("/w:document/w:body", namespaces=nsprefixes)[0] 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 search(self, string): return docx.search(self.body, string)
'vAlign': { 'val': 'center' } }] * 3, rowstyle={'height': CELL_SIZE}, table_props={ 'jc': { 'val': 'center' }, '__margin__': t_prop_margin, }, )) # Search and replace print 'Searching for something in a paragraph ...', if dx.search(docbody, 'the awesomeness'): print 'found it!' else: print 'nope.' print 'Searching for something in a heading ...', if dx.search(docbody, '200 lines'): print 'found it!' else: print 'nope.' print 'Replacing ...', docbody = dx.replace(docbody, 'the awesomeness', 'the goshdarned awesomeness') print 'done.'
['B1','B2','B3'], ['C1','C2','C3']], heading=False, colw=[CELL_SIZE]*3, cwunit='dxa', # twenties of a point borders={'all': {'color': 'AAAAAA'}}, celstyle=[{'align': 'center', 'vAlign': {'val': 'center'}}]*3, rowstyle={'height': CELL_SIZE}, table_props={'jc': {'val': 'center'}, '__margin__': t_prop_margin, }, )) # Search and replace print 'Searching for something in a paragraph ...', if dx.search(docbody, 'the awesomeness'): print 'found it!' else: print 'nope.' print 'Searching for something in a heading ...', if dx.search(docbody, '200 lines'): print 'found it!' else: print 'nope.' print 'Replacing ...', docbody = dx.replace(docbody,'the awesomeness','the goshdarned awesomeness') print 'done.' # Add a pagebreak
docbody.append( docx.paragraph( 'Tables are just lists of lists, like this:' ) ) # Append a table docbody.append( docx.table( [['A1', 'A2', 'A3'], ['B1', 'B2', 'B3'], ['C1', 'C2', 'C3']] ) ) docbody.append( docx.heading( 'Editing documents', 2 ) ) docbody.append( docx.paragraph( 'Thanks to the awesomeness of the lxml module, we can:' ) ) for point in ['Search and replace', 'Extract plain text of document', 'Add and delete items anywhere within the document']: docbody.append( docx.paragraph( point, style = 'ListBullet' ) ) # Add an image relationships, picpara = docx.picture( relationships, 'image1.png', 'This is a test description' ) docbody.append( picpara ) # Search and replace print 'Searching for something in a paragraph ...', if docx.search( docbody, 'the awesomeness' ): print 'found it!' else: print 'nope.' print 'Searching for something in a heading ...', if docx.search( docbody, '200 lines' ): print 'found it!' else: print 'nope.' print 'Replacing ...', docbody = docx.replace( docbody, 'the awesomeness', 'the goshdarned awesomeness' ) print 'done.' paratext = [ ( '\nBig blue text\n', {'bold': '', 'color': '0000FF', 'size': '18'} ), ( '\nBold text\n', {'bold': ''} ) ] docbody.append( docx.paragraph( paratext ) )