def __init__(self): dataDir = Settings.dataDir + 'quickstart/' dstDoc = Document(dataDir + 'TestFile.Destination.doc') srcDoc = Document(dataDir + 'TestFile.Source.doc') dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING) dstDoc.save(dataDir + 'AppendDocuments.doc') print("Documents appended successfully.")
def keep_source_formatting(self): """ Shows how to append a document to the end of another document using no additional options. """ dstDoc = Document(self.dataDir + "TestFile.Destination.doc") srcDoc = Document(self.dataDir + "TestFile.Source.doc") # Append the source document to the destination document using no extra options. dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING) dstDoc.save(self.dataDir + "TestFile.KeepSourceFormatting Out.docx")
def use_destination_styles(self): """ Shows how to append a document to another document using the formatting of the destination document. """ # Load the documents to join. dstDoc = Document(self.dataDir + "TestFile.Destination.doc") srcDoc = Document(self.dataDir + "TestFile.Source.doc") # Append the source document using the styles of the destination document. dstDoc.appendDocument(srcDoc, ImportFormatMode.USE_DESTINATION_STYLES) # Save the joined document to disk. dstDoc.save(self.dataDir + "TestFile.UseDestinationStyles Out.doc")
def join_continuous(self): """ Shows how to append a document to another document so the content flows continuously. """ dstDoc = Document(self.dataDir + "TestFile.Destination.doc") srcDoc = Document(self.dataDir + "TestFile.Source.doc") # Make the document appear straight after the destination documents content. srcDoc.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS) # Append the source document using the original styles found in the source document. dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING) dstDoc.save(self.dataDir + "TestFile.JoinContinuous Out.doc")
def join_new_page(self): """ Shows how to append a document to another document so it starts on a new page. """ dstDoc = Document(self.dataDir + "TestFile.Destination.doc") srcDoc = Document(self.dataDir + "TestFile.Source.doc") # Set the appended document to start on a new page. srcDoc.getFirstSection().getPageSetup().setSectionStart(SectionStart.NEW_PAGE) # Append the source document using the original styles found in the source document. dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING) dstDoc.save(self.dataDir + "TestFile.JoinNewPage Out.doc")
def unlink_headers_footers(self): """ Shows how to append a document to another document so headers and footers do not continue from the destination document. """ dstDoc = Document(self.dataDir + "TestFile.Destination.doc") srcDoc = Document(self.dataDir + "TestFile.Source.doc") # Even a document with no headers or footers can still have the LinkToPrevious setting set to True. # Unlink the headers and footers in the source document to stop this from continuing the headers and footers # from the destination document. srcDoc.getFirstSection().getHeadersFooters().linkToPrevious(0) dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING) dstDoc.save(self.dataDir + "TestFile.UnlinkHeadersFooters Out.doc")
def __init__(self): dataDir = Settings.dataDir + 'programming_documents/' # Load the document. doc = Document(dataDir + "tableDoc.doc") # Get the first table in the document. firstTable = doc.getChild(NodeType.TABLE, 0, True) # We will split the table at the third row (inclusive). row = firstTable.getRows().get(2) # Create a new container for the split table. table = firstTable.deepClone(False) # Insert the container after the original. firstTable.getParentNode().insertAfter(table, firstTable) # Add a buffer paragraph to ensure the tables stay apart. firstTable.getParentNode().insertAfter(Paragraph(doc), firstTable) currentRow = '' while (currentRow != row): currentRow = firstTable.getLastRow() table.prependChild(currentRow) doc.save(dataDir + "SplitTable.doc") print "Done."
def __init__(self): dataDir = Settings.dataDir + 'programming_documents/' doc = Document() builder = DocumentBuilder(doc) # Insert few page breaks (just for testing) i = 0 while (i < 5): builder.insertBreak(BreakType.PAGE_BREAK) i = i + 1 # Move DocumentBuilder cursor into the primary footer. builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY) # We want to insert a field like this: # { IF {PAGE} <> {NUMPAGES} "See Next Page" "Last Page" } field = builder.insertField("IF ") builder.moveTo(field.getSeparator()) builder.insertField("PAGE") builder.write(" <> ") builder.insertField("NUMPAGES") builder.write(" \"See Next Page\" \"Last Page\" ") # Finally update the outer field to recalcaluate the final value. Doing this will automatically update # the inner fields at the same time. field.update() doc.save(dataDir + "InsertNestedFields Out.docx")
def __init__(self): self.dataDir = Settings.dataDir + 'programming_documents/' # Create a blank document. doc = Document() builder = DocumentBuilder(doc) # The number of pages the document should have. numPages = 4 # The document starts with one section, insert the barcode into this existing section. self.insert_barcode_into_footer(builder, doc.getFirstSection(), 1, HeaderFooterType.FOOTER_PRIMARY) i = 1 while (i < numPages) : # Clone the first section and add it into the end of the document. cloneSection = doc.getFirstSection().deepClone(False) cloneSection.getPageSetup().setSectionStart(SectionStart.NEW_PAGE) doc.appendChild(cloneSection) # Insert the barcode and other information into the footer of the section. self.insert_barcode_into_footer(builder, cloneSection, i, HeaderFooterType.FOOTER_PRIMARY) i = i + 1 # Save the document as a PDF to disk. You can also save this directly to a stream. doc.save(self.dataDir + "InsertBarcodeOnEachPage.docx") print "Aspose Barcode Inserted..."
def restart_page_numbering(self): """ Shows how to append a document to another document with page numbering restarted. """ dstDoc = Document(self.dataDir + "TestFile.Destination.doc") srcDoc = Document(self.dataDir + "TestFile.Source.doc") # Set the appended document to appear on the next page. srcDoc.getFirstSection().getPageSetup().setSectionStart(SectionStart.NEW_PAGE) # Restart the page numbering for the document to be appended. srcDoc.getFirstSection().getPageSetup().setRestartPageNumbering(1) dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING) dstDoc.save(self.dataDir + "TestFile.RestartPageNumbering Out.doc")
def __init__(self): dataDir = Settings.dataDir + 'programming_documents/' # Open the document. doc = Document(dataDir + "TestFile.doc") # Define style names as they are specified in the Word document. PARA_STYLE = "Heading 1" RUN_STYLE = "Intense Emphasis" # Collect paragraphs with defined styles. # Show the number of collected paragraphs and display the text of this paragraphs. paragraphs = self.paragraphs_by_style_name(doc, PARA_STYLE) print "abc = " + str(paragraphs[0]) print "Paragraphs with " + PARA_STYLE + " styles " + str(len(paragraphs)) + ":" for paragraph in paragraphs : print str(paragraph.toString(SaveFormat.TEXT)) # Collect runs with defined styles. # Show the number of collected runs and display the text of this runs. runs = self.runs_by_style_name(doc, RUN_STYLE) print "Runs with " + RUN_STYLE + " styles " + str(len(runs)) + ":" for run in runs : print run.getRange().getText()
def __init__(self): dataDir = Settings.dataDir + 'quickstart/' doc = Document(dataDir + 'Document.doc') doc.save(dataDir + 'Document.pdf') print "Converted document to PDF."
def __init__(self): dataDir = Settings.dataDir + 'quickstart/' doc = Document(dataDir + 'Document.doc') doc.save(dataDir + 'Document_Out.doc') print "Document saved."
def __init__(self): self.dataDir = Settings.dataDir + 'rendering_printing/' # Load the documents which store the shapes we want to render. doc = Document(self.dataDir + "TestFile.doc") doc2 = Document(self.dataDir + "TestFile.docx") # Retrieve the target shape from the document. In our sample document this is the first shape. shape = doc.getChild(NodeType.SHAPE, 0, True) drawingML = doc2.getChild(NodeType.SHAPE, 0, True) # Test rendering of different types of nodes. self.render_shape_to_disk(shape) self.render_shape_to_stream(shape) self.render_shape_to_graphics(shape) self.render_drawingml_to_disk(drawingML) self.find_shape_sizes(shape)
def link_headers_footers(self): """ Shows how to append a document to another document and continue headers and footers from the destination document. """ dstDoc = Document(self.dataDir + "TestFile.Destination.doc") srcDoc = Document(self.dataDir + "TestFile.Source.doc") # Set the appended document to appear on a new page. srcDoc.getFirstSection().getPageSetup().setSectionStart(SectionStart.NEW_PAGE) # Link the headers and footers in the source document to the previous section. # This will override any headers or footers already found in the source document. srcDoc.getFirstSection().getHeadersFooters().linkToPrevious(1) dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING) dstDoc.save(self.dataDir + "TestFile.LinkHeadersFooters Out.doc")
def __init__(self): dataDir = Settings.dataDir + 'programming_documents/' doc = Document(dataDir + "trackDoc.doc") doc.acceptAllRevisions() doc.save(dataDir + "AcceptChanges.doc", SaveFormat.DOC) print "Done."
def __init__(self): dataDir = Settings.dataDir + 'programming_documents/' doc = Document(dataDir + "TestFile.doc") self.insert_watermark_text(doc, "CONFIDENTIAL") doc.save(dataDir + "Watermark.doc") print "Watermark added to the document successfully."
def __init__(self): dataDir = Settings.dataDir + 'programming_documents/' doc = Document(dataDir + "document.doc") clone = doc.deepClone() clone.save(dataDir + "CloneDocument.doc", SaveFormat.DOC) print "Done."
def __init__(self): dataDir = Settings.dataDir + 'loading_saving/' # The encoding of the text file is automatically detected. doc = Document(dataDir + "LoadTxt.txt") # Save as any Aspose.Words supported format, such as DOCX. doc.save(dataDir + "LoadTxt_Out.docx") print "Process Completed Successfully"
def __init__(self): dataDir = Settings.dataDir + 'quickstart/' doc = Document() builder = DocumentBuilder(doc) builder.writeln('Hello World!') doc.save(dataDir + 'HelloWorld.docx') print "Document saved."
def remove_source_headers_footers(self): """ Shows how to remove headers and footers from a document before appending it to another document. """ dstDoc = Document(self.dataDir + "TestFile.Destination.doc") srcDoc = Document(self.dataDir + "TestFile.Source.doc") # Remove the headers and footers from each of the sections in the source document. for section in srcDoc.getSections().toArray(): section.clearHeadersFooters() # Even after the headers and footers are cleared from the source document, the "LinkToPrevious" setting # for HeadersFooters can still be set. This will cause the headers and footers to continue from the destination # document. This should set to false to avoid this behaviour. srcDoc.getFirstSection().getHeadersFooters().linkToPrevious(0) dstDoc.appendDocument(srcDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING) dstDoc.save(self.dataDir + "TestFile.RemoveSourceHeadersFooters Out.doc")
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): dataDir = Settings.dataDir + 'loading_saving/' # Load the document from disk. doc = Document(dataDir + "document.doc") doc.save(dataDir + "Aspose_DocToHTML.html",SaveFormat.HTML) # Save the document in HTML format. doc.save(dataDir + "Aspose_DocToPDF.pdf",SaveFormat.PDF) # Save the document in PDF format. doc.save(dataDir + "Aspose_DocToTxt.txt",SaveFormat.TEXT) # Save the document in TXT format. doc.save(dataDir + "Aspose_DocToJPG.jpg",SaveFormat.JPEG) # Save the document in JPEG format. print "Doc file converted in specified formats"
def __init__(self): dataDir = Settings.dataDir + 'programming_documents/' doc = Document(dataDir + "document.doc") doc.protect(ProtectionType.READ_ONLY) #doc.protect(ProtectionType.ALLOW_ONLY_COMMENTS) #doc.protect(ProtectionType.ALLOW_ONLY_FORM_FIELDS) #doc.protect(ProtectionType.ALLOW_ONLY_REVISIONS) doc.save(dataDir + "ProtectDocument.doc", SaveFormat.DOC) print "Done."
def __init__(self): self.dataDir = Settings.dataDir + 'programming_documents/' # Open the document. doc = Document(self.dataDir + "TestFile.doc") self.extract_content_between_paragraphs(doc) self.extract_content_between_block_level_nodes(doc) self.extract_content_between_runs(doc) self.extract_content_using_field(doc) self.extract_content_between_bookmark(doc) self.extract_content_between_comment_range(doc) self.extract_content_between_paragraph_styles(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 __init__(self): dataDir = Settings.dataDir + 'programming_documents/' # Open the document. doc = Document(dataDir + "TestRemoveBreaks.doc") # Remove the page and section breaks from the document. # In Aspose.Words section breaks are represented as separate Section nodes in the document. # To remove these separate sections the sections are combined. self.remove_page_breaks(doc) self.remove_section_breaks(doc) # Save the document. doc.save(dataDir + "TestRemoveBreaks Out.doc")
def __init__(self): dataDir = Settings.dataDir + 'quickstart/' doc = Document(dataDir + "MailMerge.doc") # Fill the fields in the document with user data. doc.getMailMerge().execute( ["FullName", "Company", "Address", "Address2", "City"], ["James Bond", "MI5 Headquarters", "Milbank", "", "London"]) # Saves the document to disk. doc.save(dataDir + "MailMerge Result Out.docx") print "Mail merge performed successfully."
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): # Create a new document. doc = Document() # Creates and adds a paragraph node to the document. para = Paragraph(doc) # Typed access to the last section of the document. section = doc.getLastSection() section.getBody().appendChild(para) # Next print the node type of one of the nodes in the document. nodeType = doc.getFirstSection().getBody().getNodeType() print "NodeType: " + Node.nodeTypeToString(nodeType)