|
from docx import Document |
|
import os |
|
import sys |
|
|
|
def fill_doc(file_path, result_path, variables): |
|
template_document = Document(file_path) |
|
|
|
for variable_key, variable_value in variables.items(): |
|
for paragraph in template_document.paragraphs: |
|
replace_text_in_paragraph(paragraph, variable_key, variable_value) |
|
|
|
for table in template_document.tables: |
|
for col in table.columns: |
|
for cell in col.cells: |
|
for paragraph in cell.paragraphs: |
|
replace_text_in_paragraph(paragraph, variable_key, variable_value) |
|
|
|
template_document.save(result_path) |
|
|
|
|
|
def replace_text_in_paragraph(paragraph, key, value): |
|
if key in paragraph.text: |
|
inline = paragraph.runs |
|
for item in inline: |
|
if key in item.text: |
|
item.text = item.text.replace(key, value) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sys.modules[__name__] = fill_doc |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|