Copying a table to another document and place it in the footer

doc = aw.Document("Table.docx")
table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table()
table_clone = table.clone(True).as_table()

I have the above syntax and I want to copy the Table_clone to another document and place it in the header.

@Mark030 You can use following code:

src_doc = aw.Document("Tables.docx")
dst_doc = aw.Document()

# Get collection of Tables from the source document
table = src_doc.get_child(aw.NodeType.TABLE, 0, True)
# Import this table into the destination document
dst_node = dst_doc.import_node(table, True, aw.ImportFormatMode.KEEP_SOURCE_FORMATTING)
# Insert table into the destination document
header = dst_doc.first_section.headers_footers[aw.HeaderFooterType.HEADER_PRIMARY]

if header is None:
    dst_doc.first_section.headers_footers.add(aw.HeaderFooter(dst_doc, aw.HeaderFooterType.HEADER_PRIMARY))

dst_doc.first_section.headers_footers.header_primary.append_child(dst_node)

dst_doc.save("output.docx")

Also, please check:

1 Like

Thanks a lot.
Follow up question.
So append_child adds the node to the last iteration.

How do I add it at the beginning?.

@Mark030 You can use InsertBefore for this:

header = dst_doc.first_section.headers_footers.header_primary
header.first_paragraph.parent_node.insert_before(dst_node, header.first_paragraph)

Thanks, This works.

But this only works if the 1st node is a paragraph. how to also do it if the 1st node is a table?

NVM, I just answered my question with my question. By using the node.

Thanks

@Mark030 It is perfect that you managed to resolve the problem.