Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,13 +8,24 @@ from docx.shared import RGBColor
|
|
| 8 |
from docx import Document
|
| 9 |
from docx.shared import RGBColor
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
def replace_red_text_with_data(word_path, data_dict):
|
| 12 |
doc = Document(word_path)
|
| 13 |
|
| 14 |
for para in doc.paragraphs:
|
| 15 |
full_text = para.text
|
| 16 |
for i, run in enumerate(para.runs):
|
| 17 |
-
if run
|
| 18 |
# Search for the key (label) before this red text
|
| 19 |
preceding_text = ''.join(r.text for r in para.runs[:i]).lower()
|
| 20 |
for key in data_dict:
|
|
|
|
| 8 |
from docx import Document
|
| 9 |
from docx.shared import RGBColor
|
| 10 |
|
| 11 |
+
def is_red_color(run):
|
| 12 |
+
color = run.font.color
|
| 13 |
+
if color is None:
|
| 14 |
+
return False
|
| 15 |
+
if color.rgb:
|
| 16 |
+
r, g, b = color.rgb[0], color.rgb[1], color.rgb[2]
|
| 17 |
+
return r >= 200 and g <= 80 and b <= 80 # flexible red check
|
| 18 |
+
if color.theme_color: # fallback if theme_color is set instead of RGB
|
| 19 |
+
return str(color.theme_color).lower().endswith("accent2") or str(color.theme_color).lower().endswith("red")
|
| 20 |
+
return False
|
| 21 |
+
|
| 22 |
def replace_red_text_with_data(word_path, data_dict):
|
| 23 |
doc = Document(word_path)
|
| 24 |
|
| 25 |
for para in doc.paragraphs:
|
| 26 |
full_text = para.text
|
| 27 |
for i, run in enumerate(para.runs):
|
| 28 |
+
if is_red_color(run):
|
| 29 |
# Search for the key (label) before this red text
|
| 30 |
preceding_text = ''.join(r.text for r in para.runs[:i]).lower()
|
| 31 |
for key in data_dict:
|