Append all child nodes including paragraphs, tables, images etc

Hi @alexey.noskov, can you please provide the same code using python. Seems some of the functions like get_child_nodes() needs node type as method parameter. We have a scenarion where we want to append all child nodes including paragraphs, tables, images etc., Code here only seems to cover appending paragraphs.

@user1211 You should use the following code to get the direct children of all types:

composite_node.get_child_nodes(aw.NodeType.ANY, False)

The code from the referenced topic is quite obsolete and currently there is a built-in method for inserting one document into another. Please see DocumentBuilder.insert_document or DocumentBuilder.insert_document_inline method. So if your requirement is to insert one document into another at the specific bookmark, you can simply use the following code:

dst = aw.Document("C:\\Temp\\dst.docx")
dst_builder = aw.DocumentBuilder(dst)

src = aw.Document("C:\\Temp\\src.docx")

# Move DocumentBuilder to the bookmark.
dst_builder.move_to_bookmark("bk", False, True)
# Insert source document
dst_builder.insert_document(src, aw.ImportFormatMode.USE_DESTINATION_STYLES)"C:\\Temp\\out.docx")
1 Like

Thanks! This helps.

1 Like

@alexey.noskov , I’m inserting a PARAGRAPH_BREAK first, before inserting the document so the document text is added from the next line. However, this is leaving an empty paragraph at the end of the inserted document. How can I delete the para break that is added?

@user1211 You can use DocumentBuilder.insert_document_inline method instead of DocumentBuilder.insert_document method. When insert_document_inline is used the paragraph break of the last inserted paragraph is removed.

@alexey.noskov Thanks! I see the para break is being removed after using insert_document_inline method. However, I see that the src_doc styles are changed after inserting. I have used the following parametersDocumentBuilder.insert_document_inline(src_doc, aw.ImportFormatMode.KEEP_DIFFERENT_STYLES, aw.ImportFormatOptions()). Am I missing anything here?

@user1211 Please , try using aw.ImportFormatMode.KEEP_SOURCE_FORMATTING. IF this does not help, please attach your input documents here for testing. We will check the issue and provide you more information.

@user1211 As I can see the attached destination document does not have any bookmarks. Could you please attach your destination, source, output and expected output documents? This will help us to better understand your requirements and provide a solution.

If the bookmark in your destination document is located at the end of list item paragraph, you should remove numbers before inserting the document, like this:

builder.move_to_bookmark("bk", False, True)
builder.insert_document(qvidian_doc, aw.ImportFormatMode.KEEP_DIFFERENT_STYLES)

@alexey.noskov I’m using if (builder.current_paragraph.runs.count == 0): builder.current_paragraph.remove() to remove the line break after inserting the document. This is helping so far. Assuming runs.count == 0 means its a line break. Correct me if I’m wrong. Thanks!

@user1211 runs.count == 0 means there re no Run child nodes in the paragraph, i.e. no text. but paragraphs might also contain other child nodes. If it is required to check whether paragraph is empty, you can check whether it’s text is empty string and there are no shapes. For example see the following code:

dst = aw.Document("C:\\Temp\\Destination_bookmark_out.docx") 
builder = aw.DocumentBuilder(dst)

# Open source document.
src = aw.Document("C:\\Temp\\src_doc1.docx")

# Insert source document into bookmakr.
builder.move_to_bookmark("bookmark57E9AD27", False, True)
builder.insert_document(src, aw.ImportFormatMode.KEEP_DIFFERENT_STYLES)
# check whether current paragraph is empty and remove it.
if builder.current_paragraph.to_string(aw.SaveFormat.TEXT).strip() == "" and builder.current_paragraph.get_child_nodes(aw.NodeType.SHAPE, True).count == 0:
1 Like

@alexey.noskov ,I have an observation while using aw.ImportFormatMode.KEEP_DIFFERENT_STYLES i.e., If multiple documents are inserted into destination document which has same styles say CustomeStyle1, then multiple style names are created in the document with the following naming convention CustomeStyle1_0,CustomeStyle1_1,CustomeStyle1_2..... I tried using aw.ImportFormatMode.KEEP_SOURCE_FORMATTING but noticed that if a style already exists in the dest doc, then the source style formatting is expanded
into direct Node attributes and the style is changed to Normal. What can I do to avoid both situations and just get the styles imported from source documents just once.

@user1211 Have you tired using ImportFormatOptions.smart_style_behavior property?

Also, you can use ImportFormatMode.USE_DESTINATION_STYLES. In this case Aspose.Words will use the destination document styles if the destination document already has styles with the same name.

Using ImportFormatMode.USE_DESTINATION_STYLES fixed the issue. Thanks!

1 Like