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): 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/' # 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 + 'quickstart/' doc = Document(dataDir + 'Document.doc') doc.save(dataDir + 'Document_Out.doc') print "Document saved."
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 + '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 + "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 + '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 + 'quickstart/' # Demonstrates how to insert fields and update them using Aspose.Words. # First create a blank document. doc = Document() # Use the document builder to insert some content and fields. builder = DocumentBuilder(doc) # Insert a table of contents at the beginning of the document. builder.insertTableOfContents("\\o \"1-3\" \\h \\z \\u") builder.writeln() # Insert some other fields. builder.write("Page: ") builder.insertField("PAGE") builder.write(" of ") builder.insertField("NUMPAGES") builder.writeln() builder.write("Date: ") builder.insertField("DATE") # Start the actual document content on the second page. builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE) # Build a document with complex structure by applying different heading styles thus creating TOC entries. builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1) builder.writeln("Heading 1") builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2) builder.writeln("Heading 1.1") builder.writeln("Heading 1.2") builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1) builder.writeln("Heading 2") builder.writeln("Heading 3") # Move to the next page. builder.insertBreak(BreakType.PAGE_BREAK) builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2) builder.writeln("Heading 3.1") builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_3) builder.writeln("Heading 3.1.1") builder.writeln("Heading 3.1.2") builder.writeln("Heading 3.1.3") builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_2) builder.writeln("Heading 3.2") builder.writeln("Heading 3.3") print "Updating all fields in the document." # Call the method below to update the TOC. doc.updateFields() doc.save(dataDir + "Document Field Update Out.docx") print "Fields updated in the document successfully."
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 __init__(self): dataDir = Settings.dataDir + 'quickstart/' doc = Document() builder = DocumentBuilder(doc) builder.writeln('Hello World!') doc.save(dataDir + 'HelloWorld.docx') print "Document saved."
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): 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 __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): # 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)
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 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 generate_document(self,srcDoc,nodes): # Create a blank document. dstDoc = Document() # Remove the first paragraph from the empty document. dstDoc.getFirstSection().getBody().removeAllChildren() # Import each node from the list into the new document. Keep the original formatting of the node. importer = NodeImporter(srcDoc, dstDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING) for node in nodes: importNode = importer.importNode(node, True) dstDoc.getFirstSection().getBody().appendChild(importNode) # Return the generated document. return dstDoc
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 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/' doc = Document() builder = DocumentBuilder(doc) pageSetup = builder.getPageSetup() pageSetup.setTopMargin(0.5) pageSetup.setBottomMargin(0.5) pageSetup.setLeftMargin(0.5) pageSetup.setRightMargin(0.5) pageSetup.setHeaderDistance(0.2) pageSetup.setFooterDistance(0.2) doc.save(dataDir + "PageBorders.docx") print "Done."
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 + 'rendering_printing/' # Open the document. doc = Document(dataDir + "TestFile.doc") # Save the document as multipage TIFF. doc.save(dataDir + "TestFile Out.tiff") # Create an ImageSaveOptions object to pass to the Save method options = ImageSaveOptions(SaveFormat.TIFF) options.setPageIndex(0) options.setPageCount(2) options.setTiffCompression(TiffCompression.CCITT_4) options.setResolution(160) doc.save(dataDir + "TestFileWithOptions Out.tiff", options) "Document saved as multi page TIFF successfully."
def __init__(self): dataDir = Settings.dataDir + 'programming_documents/' doc = Document() builder = DocumentBuilder(doc) builder.insertImage(dataDir + "background.jpg"); builder.insertImage(dataDir + "background.jpg", RelativeHorizontalPosition.MARGIN, 100, RelativeVerticalPosition.MARGIN, 200, 200, 100, WrapType.SQUARE) doc.save(dataDir + "InsertImage.docx") print "Done."
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")
def __init__(self): dataDir = Settings.dataDir + 'quickstart/' # Open the stream. Read only access is enough for Aspose.Words to load a document. stream = FileInputStream(dataDir + 'Document.doc') # Load the entire document into memory. doc = Document(stream) # You can close the stream now, it is no longer needed because the document is in memory. stream.close() # ... do something with the document # Convert the document to a different format and save to stream. dstStream = ByteArrayOutputStream() doc.save(dstStream, SaveFormat.RTF) output = FileOutputStream(dataDir + "Document Out.rtf") output.write(dstStream.toByteArray()) output.close() print "Document loaded from stream and then saved successfully."
def __init__(self): dataDir = Settings.dataDir + 'programming_documents/' # Open the document. doc = Document(dataDir + "TestFile.doc") #ExStart #ExId:ProcessComments_Main #ExSummary: The demo-code that illustrates the methods for the comments extraction and removal. # Extract the information about the comments of all the authors. comments = self.extract_comments(doc) for comment in comments: print comment # Remove all comments. self.remove_comments(doc) print "All comments are removed!" # Save the document. doc.save(dataDir + "Comments.doc")
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/' # Load the document. doc = Document(dataDir + "tableDoc.doc") # Get the first and second table in the document. # The rows from the second table will be appended to the end of the first table. firstTable = doc.getChild(NodeType.TABLE, 0, True) secondTable = doc.getChild(NodeType.TABLE, 1, True) # Append all rows from the current table to the next. # Due to the design of tables even tables with different cell count and widths can be joined into one table. while (secondTable.hasChildNodes()) : firstTable.getRows().add(secondTable.getFirstRow()) # Remove the empty table container. secondTable.remove() doc.save(dataDir + "JoinTables.doc") print "Done."
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 __init__(self): dataDir = Settings.dataDir + 'programming_documents/' doc = Document(dataDir + "document.doc") builder = DocumentBuilder(doc) #Shows how to access the current node in a document builder. curNode = builder.getCurrentNode() curParagraph = builder.getCurrentParagraph() # Shows how to move a cursor position to a specified node. builder.moveTo(doc.getFirstSection().getBody().getLastParagraph()) # Shows how to move a cursor position to the beginning or end of a document. builder.moveToDocumentEnd() builder.writeln("This is the end of the document.") builder.moveToDocumentStart() builder.writeln("This is the beginning of the document.") doc.save(dataDir + "MovingCursor.doc") print "Done."
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")