Spaces:
Running
Running
Update updated_word.py
Browse files- updated_word.py +76 -0
updated_word.py
CHANGED
|
@@ -1490,6 +1490,82 @@ def process_red_text_in_paragraph(paragraph, context_text, flat_json):
|
|
| 1490 |
|
| 1491 |
return replacements_made
|
| 1492 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1493 |
# ============================================================================
|
| 1494 |
# Orchestrator
|
| 1495 |
# ============================================================================
|
|
|
|
| 1490 |
|
| 1491 |
return replacements_made
|
| 1492 |
|
| 1493 |
+
def process_red_text_in_context_paragraph(paragraph, heading_text, flat_json, operator_name):
|
| 1494 |
+
"""Process red text found in paragraphs following headings"""
|
| 1495 |
+
replacements_made = 0
|
| 1496 |
+
red_text_segments = []
|
| 1497 |
+
|
| 1498 |
+
for run in paragraph.runs:
|
| 1499 |
+
if is_red(run) and run.text.strip():
|
| 1500 |
+
red_text_segments.append(run.text.strip())
|
| 1501 |
+
|
| 1502 |
+
if not red_text_segments:
|
| 1503 |
+
return 0
|
| 1504 |
+
|
| 1505 |
+
combined_red_text = " ".join(red_text_segments).strip()
|
| 1506 |
+
print(f" 🔍 Red text found: '{combined_red_text}'")
|
| 1507 |
+
|
| 1508 |
+
replacement_value = None
|
| 1509 |
+
|
| 1510 |
+
# Determine what to replace based on heading context
|
| 1511 |
+
if any(mgmt_type in heading_text.upper() for mgmt_type in ["MAINTENANCE MANAGEMENT", "MASS MANAGEMENT", "FATIGUE MANAGEMENT"]):
|
| 1512 |
+
# For management section headings, replace with operator name
|
| 1513 |
+
if operator_name:
|
| 1514 |
+
replacement_value = operator_name
|
| 1515 |
+
print(f" ✅ Using operator name for management section: '{operator_name}'")
|
| 1516 |
+
|
| 1517 |
+
elif "NHVAS APPROVED AUDITOR DECLARATION" in heading_text.upper():
|
| 1518 |
+
# For auditor declarations, look for auditor name
|
| 1519 |
+
auditor_name = None
|
| 1520 |
+
for key, value in flat_json.items():
|
| 1521 |
+
if "auditor" in key.lower() and "name" in key.lower():
|
| 1522 |
+
if isinstance(value, list) and value:
|
| 1523 |
+
auditor_name = str(value[0]).strip()
|
| 1524 |
+
elif value:
|
| 1525 |
+
auditor_name = str(value).strip()
|
| 1526 |
+
break
|
| 1527 |
+
|
| 1528 |
+
if auditor_name:
|
| 1529 |
+
replacement_value = auditor_name
|
| 1530 |
+
print(f" ✅ Using auditor name: '{auditor_name}'")
|
| 1531 |
+
|
| 1532 |
+
elif "OPERATOR DECLARATION" in heading_text.upper():
|
| 1533 |
+
# For operator declarations, use operator name
|
| 1534 |
+
if operator_name:
|
| 1535 |
+
replacement_value = operator_name
|
| 1536 |
+
print(f" ✅ Using operator name for operator declaration: '{operator_name}'")
|
| 1537 |
+
|
| 1538 |
+
else:
|
| 1539 |
+
# For other headings, try to find a relevant match
|
| 1540 |
+
# First try direct match
|
| 1541 |
+
kv = find_matching_json_key_and_value(combined_red_text, flat_json)
|
| 1542 |
+
if kv:
|
| 1543 |
+
replacement_value = get_value_as_string(kv[1], combined_red_text)
|
| 1544 |
+
else:
|
| 1545 |
+
# Try contextual search with heading
|
| 1546 |
+
context_queries = [f"{heading_text} {combined_red_text}", combined_red_text, heading_text]
|
| 1547 |
+
for query in context_queries:
|
| 1548 |
+
kv = find_matching_json_key_and_value(query, flat_json)
|
| 1549 |
+
if kv:
|
| 1550 |
+
replacement_value = get_value_as_string(kv[1], combined_red_text)
|
| 1551 |
+
print(f" ✅ Found match with combined query: {kv[0]}")
|
| 1552 |
+
break
|
| 1553 |
+
|
| 1554 |
+
# Apply the replacement if we found a suitable value
|
| 1555 |
+
if replacement_value:
|
| 1556 |
+
red_runs = [run for run in paragraph.runs if is_red(run) and run.text.strip()]
|
| 1557 |
+
if red_runs:
|
| 1558 |
+
red_runs[0].text = replacement_value
|
| 1559 |
+
red_runs[0].font.color.rgb = RGBColor(0, 0, 0)
|
| 1560 |
+
for run in red_runs[1:]:
|
| 1561 |
+
run.text = ''
|
| 1562 |
+
replacements_made = 1
|
| 1563 |
+
print(f" ✅ Replaced with: '{replacement_value}'")
|
| 1564 |
+
else:
|
| 1565 |
+
print(f" ❌ No suitable replacement found for: '{combined_red_text}'")
|
| 1566 |
+
|
| 1567 |
+
return replacements_made
|
| 1568 |
+
|
| 1569 |
# ============================================================================
|
| 1570 |
# Orchestrator
|
| 1571 |
# ============================================================================
|