insert_document method

insert_document(src_doc, import_format_mode)

Inserts a document at the cursor position.

def insert_document(self, src_doc: aspose.words.Document, import_format_mode: aspose.words.ImportFormatMode):
    ...
ParameterTypeDescription
src_docDocumentSource document for inserting.
import_format_modeImportFormatModeSpecifies how to merge style formatting that clashes.

Remarks

This method mimics the MS Word behavior, as if CTRL+‘A’ (select all content) was pressed, then CTRL+‘C’ (copy selected into the buffer) inside one document and then CTRL+‘V’ (insert content from the buffer) inside another document.

Returns

First node of the inserted content.

insert_document(src_doc, import_format_mode, import_format_options)

Inserts a document at the cursor position.

def insert_document(self, src_doc: aspose.words.Document, import_format_mode: aspose.words.ImportFormatMode, import_format_options: aspose.words.ImportFormatOptions):
    ...
ParameterTypeDescription
src_docDocumentSource document for inserting.
import_format_modeImportFormatModeSpecifies how to merge style formatting that clashes.
import_format_optionsImportFormatOptionsAllows to specify options that affect formatting of a result document.

Remarks

This method mimics the MS Word behavior, as if CTRL+‘A’ (select all content) was pressed, then CTRL+‘C’ (copy selected into the buffer) inside one document and then CTRL+‘V’ (insert content from the buffer) inside another document.

Returns

First node of the inserted content.

Examples

Shows how to insert a document into another document.

doc = aw.Document(MY_DIR + "Document.docx")

builder = aw.DocumentBuilder(doc)
builder.move_to_document_end()
builder.insert_break(aw.BreakType.PAGE_BREAK)

doc_to_insert = aw.Document(MY_DIR + "Formatted elements.docx")

builder.insert_document(doc_to_insert, aw.ImportFormatMode.KEEP_SOURCE_FORMATTING)
builder.document.save(ARTIFACTS_DIR + "DocumentBuilder.insert_document.docx")

Shows how to resolve duplicate styles while inserting documents.

dst_doc = aw.Document()
builder = aw.DocumentBuilder(dst_doc)

my_style = builder.document.styles.add(aw.StyleType.PARAGRAPH, "MyStyle")
my_style.font.size = 14
my_style.font.name = "Courier New"
my_style.font.color = drawing.Color.blue

builder.paragraph_format.style_name = my_style.name
builder.writeln("Hello world!")

# Clone the document and edit the clone's "MyStyle" style, so it is a different color than that of the original.
# If we insert the clone into the original document, the two styles with the same name will cause a clash.
src_doc = dst_doc.clone()
src_doc.styles.get_by_name("MyStyle").font.color = drawing.Color.red

# When we enable "smart_style_behavior" and use the KEEP_SOURCE_FORMATTING import format mode,
# Aspose.Words will resolve style clashes by converting source document styles.
# with the same names as destination styles into direct paragraph attributes.
options = aw.ImportFormatOptions()
options.smart_style_behavior = True

builder.insert_document(src_doc, aw.ImportFormatMode.KEEP_SOURCE_FORMATTING, options)

dst_doc.save(ARTIFACTS_DIR + "DocumentBuilder.smart_style_behavior.docx")

See Also