0

I have several documents in docx format. They are created as docxtpl templates. I want to merge them into one final file. If condition 1 is fulfilled, then one document is selected. If condition 2 is fulfilled, then another document is selected.

I tried to use this method, but the document is incorrect. The document does not open and displays a message about incorrect content

import os
import docxtpl
from docx import Document


if __name__ == '__main__':
    docs_dir = "Some/way/to/docx-files"
    result_doc = docxtpl.Template(os.path.join(docs_dir), "result_doc.docx")

    if condition1:
        merged_doc = Document(os.path.join(docs_dir), "First_var.docx")
    else:
        merged_doc = Document(os.path.join(docs_dir), "Second_var.docx")

    for elem in merged_doc.element.body:
        result_doc.docx.element.body.append(elem)

A search using Google led to similar code, but the results remained the same. Has anyone encountered similar problems?

0 Answers0