Spaces:
Running
Running
Create word_updater.py
Browse files- word_updater.py +22 -0
word_updater.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# word_updater.py
|
| 2 |
+
|
| 3 |
+
from docx import Document
|
| 4 |
+
from docx.shared import RGBColor
|
| 5 |
+
|
| 6 |
+
def is_red(run):
|
| 7 |
+
color = run.font.color
|
| 8 |
+
return color and color.rgb == RGBColor(255, 0, 0)
|
| 9 |
+
|
| 10 |
+
def fill_template_with_data(template_path, output_path, data):
|
| 11 |
+
doc = Document(template_path)
|
| 12 |
+
|
| 13 |
+
for para in doc.paragraphs:
|
| 14 |
+
for run in para.runs:
|
| 15 |
+
if is_red(run):
|
| 16 |
+
for key in data:
|
| 17 |
+
if key.lower() in run.text.lower():
|
| 18 |
+
run.text = run.text.replace(run.text, data[key])
|
| 19 |
+
break
|
| 20 |
+
|
| 21 |
+
doc.save(output_path)
|
| 22 |
+
return output_path
|