怎么往指定标题下读取另一个文件的表格copy进去
@Tiaohh 您可以使用以下代码:
src_doc = aw.Document("source.docx")
dst_doc = aw.Document("destination.docx")
found_table = None
# 查找指定段落下的表格。
paragraphs = src_doc.get_child_nodes(aw.NodeType.PARAGRAPH, True)
for paragraph in paragraphs:
paragraph = paragraph.as_paragraph()
if "This is my" in paragraph.get_text().strip():
next_node = paragraph.next_sibling
while next_node is not None and next_node.node_type != aw.NodeType.TABLE:
next_node = next_node.next_sibling
found_table = next_node
break
# 如果找到表格,则克隆该表格并将其插入目标文档。
if found_table is not None:
# 创建一个 NodeImporter,将表格导入目标文档。
importer = aw.NodeImporter(src_doc, dst_doc, aw.ImportFormatMode.KEEP_SOURCE_FORMATTING)
cloned_table = importer.import_node(found_table, True)
# 在目标文档中查找目标段落。
dst_paragraphs = dst_doc.get_child_nodes(aw.NodeType.PARAGRAPH, True)
for dst_paragraph in dst_paragraphs:
if "start_insert" in dst_paragraph.get_text().strip():
# Insert the cloned table after the target paragraph
dst_paragraph.parent_node.insert_after(cloned_table, dst_paragraph)
break
dst_doc.save("output.docx")
以下链接可以提供帮助: NodeImporter class | Aspose.Words for Python