def __init__(self): dataDir = Settings.dataDir + 'quickstart/' doc = Document(dataDir + 'ReplaceSimple.doc') # Check the text of the document. print "Original document text: " + doc.getRange().getText() # Replace the text in the document. doc.getRange().replace("_CustomerName_", "James Bond", False, False) # Check the replacement was made. print "Document text after replace: " + doc.getRange().getText() doc.save(dataDir + 'ReplaceSimple_Out.doc')
def __init__(self): dataDir = Settings.dataDir + 'programming_documents/' doc = Document(dataDir + "RemoveField.doc") field = doc.getRange().getFields().get(0) # Calling this method completely removes the field from the document. field.remove() doc.save(dataDir + "RemoveField Out.docx") print "Field removed from the document successfully."
def copy_bookmarked_text(self): # Load the source document. srcDoc = Document(self.dataDir + "Template.doc") # This is the bookmark whose content we want to copy. srcBookmark = srcDoc.getRange().getBookmarks().get("ntf010145060") # We will be adding to this document. dstDoc = Document() # Let's say we will be appending to the end of the body of the last section. dstNode = dstDoc.getLastSection().getBody() # It is a good idea to use this import context object because multiple nodes are being imported. # If you import multiple times without a single context, it will result in many styles created. importer = NodeImporter(srcDoc, dstDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING) # Do it once. self.append_bookmarked_text(importer, srcBookmark, dstNode) # Do it one more time for fun. self.append_bookmarked_text(importer, srcBookmark, dstNode) # Save the finished document. dstDoc.save(self.dataDir + "Template Out.doc")
def __init__(self): self.dataDir = Settings.dataDir + 'programming_documents/' self.copy_bookmarked_text() # Load a document. doc = Document(self.dataDir + "TestDefect1352.doc") # This perform the custom task of putting the row bookmark ends into the same row with the bookmark starts. self.untangle_row_bookmarks(doc) # Now we can easily delete rows by a bookmark without damaging any other row's bookmarks. self.delete_row_by_bookmark(doc, "ROW2") # This is just to check that the other bookmark was not damaged. if doc.getRange().getBookmarks().get("ROW1").getBookmarkEnd() is None: raise ValueError('Wrong, the end of the bookmark was deleted.') # Save the finished document. doc.save(self.dataDir + "TestDefect1352 Out.doc")