import logging from docx import Document logger = logging.getLogger(__name__) def process_file(path_to_docx, path_to_new_docx, changes_to_apply): """ Tries to open the docx document using given path to file. Then, applies the transformations- replaces template tags in format of ##name## to values specified in the second argument. """ doc = Document(path_to_docx) for paragraph in doc.paragraphs: for placeholder, replacement in changes_to_apply.items(): if placeholder in paragraph.text: paragraph.text = paragraph.text.replace(placeholder, replacement) doc.save(path_to_new_docx)