Spaces:
Running
Running
Update updated_word.py
Browse files- updated_word.py +81 -38
updated_word.py
CHANGED
|
@@ -570,7 +570,7 @@ def handle_attendance_list_table_enhanced(table, flat_json):
|
|
| 570 |
return replacements_made
|
| 571 |
|
| 572 |
def fix_management_summary_details_column(table, flat_json):
|
| 573 |
-
"""Enhanced management summary processing
|
| 574 |
replacements_made = 0
|
| 575 |
print(f" π― FIX: Management Summary DETAILS column processing")
|
| 576 |
|
|
@@ -600,45 +600,42 @@ def fix_management_summary_details_column(table, flat_json):
|
|
| 600 |
for mgmt_type in mgmt_types:
|
| 601 |
print(f" β
Confirmed {mgmt_type} table processing")
|
| 602 |
|
| 603 |
-
#
|
| 604 |
-
mgmt_data =
|
| 605 |
|
| 606 |
-
#
|
| 607 |
if mgmt_type in flat_json:
|
| 608 |
-
|
| 609 |
-
|
|
|
|
|
|
|
| 610 |
|
| 611 |
-
#
|
| 612 |
if not mgmt_data:
|
| 613 |
-
# Search for keys that contain the management type
|
| 614 |
for key, value in flat_json.items():
|
| 615 |
-
if
|
| 616 |
-
|
| 617 |
-
|
| 618 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 619 |
|
| 620 |
-
#
|
| 621 |
if not mgmt_data:
|
| 622 |
-
mgmt_data = {}
|
| 623 |
for key, value in flat_json.items():
|
| 624 |
-
if mgmt_type.lower() in key.lower():
|
| 625 |
-
|
| 626 |
-
|
| 627 |
-
|
| 628 |
-
|
| 629 |
-
print(f" β
Found standard data: '{std_key}' = {value}")
|
| 630 |
-
|
| 631 |
-
if mgmt_data:
|
| 632 |
-
print(f" β
Collected {len(mgmt_data)} standards for {mgmt_type}")
|
| 633 |
|
| 634 |
if not mgmt_data:
|
| 635 |
print(f" β οΈ No JSON data found for {mgmt_type}")
|
| 636 |
continue
|
| 637 |
|
| 638 |
-
|
| 639 |
-
if not isinstance(mgmt_data, dict):
|
| 640 |
-
print(f" β οΈ Management data is not a dict for {mgmt_type}: {type(mgmt_data)}")
|
| 641 |
-
continue
|
| 642 |
|
| 643 |
# Process the table rows
|
| 644 |
for row_idx, row in enumerate(table.rows):
|
|
@@ -654,47 +651,93 @@ def fix_management_summary_details_column(table, flat_json):
|
|
| 654 |
if not has_red_text(details_cell):
|
| 655 |
continue
|
| 656 |
|
| 657 |
-
|
|
|
|
|
|
|
| 658 |
replacement_value = None
|
| 659 |
|
| 660 |
-
#
|
| 661 |
if "std 1" in standard_text and ("daily" in standard_text or "check" in standard_text):
|
| 662 |
replacement_value = find_best_standard_value(mgmt_data, ["Std 1. Daily Check", "Std 1", "Daily Check"])
|
| 663 |
-
|
|
|
|
|
|
|
| 664 |
if "mass" in mgmt_type.lower():
|
| 665 |
replacement_value = find_best_standard_value(mgmt_data, ["Std 5. Verification", "Std 5", "Verification"])
|
|
|
|
| 666 |
else:
|
| 667 |
replacement_value = find_best_standard_value(mgmt_data, ["Std 5. Internal Review", "Std 5", "Internal Review"])
|
|
|
|
|
|
|
| 668 |
elif "std 6" in standard_text or "internal review" in standard_text:
|
| 669 |
replacement_value = find_best_standard_value(mgmt_data, ["Std 6. Internal Review", "Std 6", "Internal Review"])
|
|
|
|
|
|
|
| 670 |
elif "std 7" in standard_text:
|
| 671 |
-
replacement_value = find_best_standard_value(mgmt_data, ["Std 7. Internal Review", "Std 7"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 672 |
|
| 673 |
# Apply replacement if found
|
| 674 |
if replacement_value:
|
| 675 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 676 |
cell_replacements = replace_red_text_in_cell(details_cell, replacement_text)
|
| 677 |
replacements_made += cell_replacements
|
| 678 |
if cell_replacements:
|
| 679 |
-
print(f" β
Replaced {standard_text} details in {mgmt_type}")
|
| 680 |
else:
|
| 681 |
print(f" β οΈ No replacement found for '{standard_text}' in {mgmt_type}")
|
|
|
|
| 682 |
|
| 683 |
return replacements_made
|
| 684 |
|
| 685 |
-
|
| 686 |
def find_best_standard_value(mgmt_data, candidate_keys):
|
| 687 |
-
"""Find the best matching value for a standard from management data"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 688 |
for candidate in candidate_keys:
|
| 689 |
if candidate in mgmt_data:
|
|
|
|
| 690 |
return mgmt_data[candidate]
|
| 691 |
|
| 692 |
-
#
|
| 693 |
-
for
|
| 694 |
-
for
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 695 |
if candidate.lower() in key.lower() or key.lower() in candidate.lower():
|
|
|
|
| 696 |
return value
|
| 697 |
|
|
|
|
| 698 |
return None
|
| 699 |
|
| 700 |
# ============================================================================
|
|
|
|
| 570 |
return replacements_made
|
| 571 |
|
| 572 |
def fix_management_summary_details_column(table, flat_json):
|
| 573 |
+
"""FIXED: Enhanced management summary processing that handles both dict and flattened JSON structures"""
|
| 574 |
replacements_made = 0
|
| 575 |
print(f" π― FIX: Management Summary DETAILS column processing")
|
| 576 |
|
|
|
|
| 600 |
for mgmt_type in mgmt_types:
|
| 601 |
print(f" β
Confirmed {mgmt_type} table processing")
|
| 602 |
|
| 603 |
+
# FIXED: Build management data dict from multiple sources
|
| 604 |
+
mgmt_data = {}
|
| 605 |
|
| 606 |
+
# Strategy 1: Look for direct nested dict in original JSON (before flattening)
|
| 607 |
if mgmt_type in flat_json:
|
| 608 |
+
direct_data = flat_json[mgmt_type]
|
| 609 |
+
if isinstance(direct_data, dict):
|
| 610 |
+
mgmt_data = direct_data
|
| 611 |
+
print(f" β
Found direct nested dict for: '{mgmt_type}' with {len(mgmt_data)} standards")
|
| 612 |
|
| 613 |
+
# Strategy 2: Look for flattened keys like "Mass Management Summary.Std 5. Verification"
|
| 614 |
if not mgmt_data:
|
|
|
|
| 615 |
for key, value in flat_json.items():
|
| 616 |
+
if key.startswith(mgmt_type + "."):
|
| 617 |
+
# Extract the standard part (after the management type)
|
| 618 |
+
std_key = key[len(mgmt_type) + 1:] # Remove "Mass Management Summary." prefix
|
| 619 |
+
mgmt_data[std_key] = value
|
| 620 |
+
print(f" β
Found flattened standard: '{std_key}' = {value}")
|
| 621 |
+
|
| 622 |
+
if mgmt_data:
|
| 623 |
+
print(f" β
Collected {len(mgmt_data)} standards from flattened keys for {mgmt_type}")
|
| 624 |
|
| 625 |
+
# Strategy 3: Search for keys that contain the management type
|
| 626 |
if not mgmt_data:
|
|
|
|
| 627 |
for key, value in flat_json.items():
|
| 628 |
+
if mgmt_type.lower().replace(" ", "") in key.lower().replace(" ", ""):
|
| 629 |
+
if isinstance(value, dict):
|
| 630 |
+
mgmt_data = value
|
| 631 |
+
print(f" β
Found data using key variation: '{key}'")
|
| 632 |
+
break
|
|
|
|
|
|
|
|
|
|
|
|
|
| 633 |
|
| 634 |
if not mgmt_data:
|
| 635 |
print(f" β οΈ No JSON data found for {mgmt_type}")
|
| 636 |
continue
|
| 637 |
|
| 638 |
+
print(f" π Processing {mgmt_type} with standards: {list(mgmt_data.keys())}")
|
|
|
|
|
|
|
|
|
|
| 639 |
|
| 640 |
# Process the table rows
|
| 641 |
for row_idx, row in enumerate(table.rows):
|
|
|
|
| 651 |
if not has_red_text(details_cell):
|
| 652 |
continue
|
| 653 |
|
| 654 |
+
print(f" π Processing standard: '{standard_text}'")
|
| 655 |
+
|
| 656 |
+
# FIXED: Better standard matching with multiple strategies
|
| 657 |
replacement_value = None
|
| 658 |
|
| 659 |
+
# Strategy 1: Direct standard matching
|
| 660 |
if "std 1" in standard_text and ("daily" in standard_text or "check" in standard_text):
|
| 661 |
replacement_value = find_best_standard_value(mgmt_data, ["Std 1. Daily Check", "Std 1", "Daily Check"])
|
| 662 |
+
print(f" π― Looking for Std 1 Daily Check")
|
| 663 |
+
|
| 664 |
+
elif "std 5" in standard_text:
|
| 665 |
if "mass" in mgmt_type.lower():
|
| 666 |
replacement_value = find_best_standard_value(mgmt_data, ["Std 5. Verification", "Std 5", "Verification"])
|
| 667 |
+
print(f" π― Looking for Std 5 Verification (Mass)")
|
| 668 |
else:
|
| 669 |
replacement_value = find_best_standard_value(mgmt_data, ["Std 5. Internal Review", "Std 5", "Internal Review"])
|
| 670 |
+
print(f" π― Looking for Std 5 Internal Review (Fatigue)")
|
| 671 |
+
|
| 672 |
elif "std 6" in standard_text or "internal review" in standard_text:
|
| 673 |
replacement_value = find_best_standard_value(mgmt_data, ["Std 6. Internal Review", "Std 6", "Internal Review"])
|
| 674 |
+
print(f" π― Looking for Std 6 Internal Review")
|
| 675 |
+
|
| 676 |
elif "std 7" in standard_text:
|
| 677 |
+
replacement_value = find_best_standard_value(mgmt_data, ["Std 7. Internal Review", "Std 7", "Internal Review"])
|
| 678 |
+
print(f" π― Looking for Std 7 Internal Review")
|
| 679 |
+
|
| 680 |
+
# Strategy 2: Fuzzy matching if direct doesn't work
|
| 681 |
+
if not replacement_value:
|
| 682 |
+
print(f" π No direct match, trying fuzzy matching...")
|
| 683 |
+
for std_key, std_value in mgmt_data.items():
|
| 684 |
+
std_key_lower = std_key.lower()
|
| 685 |
+
if "std" in standard_text:
|
| 686 |
+
# Extract std number from both
|
| 687 |
+
std_match = re.search(r'std\s*(\d+)', standard_text)
|
| 688 |
+
key_match = re.search(r'std\s*(\d+)', std_key_lower)
|
| 689 |
+
if std_match and key_match and std_match.group(1) == key_match.group(1):
|
| 690 |
+
replacement_value = std_value
|
| 691 |
+
print(f" β
Fuzzy matched by std number: {std_key}")
|
| 692 |
+
break
|
| 693 |
|
| 694 |
# Apply replacement if found
|
| 695 |
if replacement_value:
|
| 696 |
+
# Handle list values properly
|
| 697 |
+
if isinstance(replacement_value, list):
|
| 698 |
+
if len(replacement_value) == 1:
|
| 699 |
+
replacement_text = str(replacement_value[0])
|
| 700 |
+
else:
|
| 701 |
+
replacement_text = "\n".join(str(item) for item in replacement_value)
|
| 702 |
+
else:
|
| 703 |
+
replacement_text = str(replacement_value)
|
| 704 |
+
|
| 705 |
cell_replacements = replace_red_text_in_cell(details_cell, replacement_text)
|
| 706 |
replacements_made += cell_replacements
|
| 707 |
if cell_replacements:
|
| 708 |
+
print(f" β
Replaced '{standard_text}' details in {mgmt_type} with: '{replacement_text[:50]}...'")
|
| 709 |
else:
|
| 710 |
print(f" β οΈ No replacement found for '{standard_text}' in {mgmt_type}")
|
| 711 |
+
print(f" π Available standards: {list(mgmt_data.keys())}")
|
| 712 |
|
| 713 |
return replacements_made
|
| 714 |
|
|
|
|
| 715 |
def find_best_standard_value(mgmt_data, candidate_keys):
|
| 716 |
+
"""FIXED: Find the best matching value for a standard from management data"""
|
| 717 |
+
print(f" π Searching for candidates: {candidate_keys}")
|
| 718 |
+
print(f" π In available keys: {list(mgmt_data.keys())}")
|
| 719 |
+
|
| 720 |
+
# Direct match
|
| 721 |
for candidate in candidate_keys:
|
| 722 |
if candidate in mgmt_data:
|
| 723 |
+
print(f" β
Direct match found: '{candidate}'")
|
| 724 |
return mgmt_data[candidate]
|
| 725 |
|
| 726 |
+
# Case insensitive match
|
| 727 |
+
for candidate in candidate_keys:
|
| 728 |
+
for key, value in mgmt_data.items():
|
| 729 |
+
if candidate.lower() == key.lower():
|
| 730 |
+
print(f" β
Case-insensitive match found: '{key}' for '{candidate}'")
|
| 731 |
+
return value
|
| 732 |
+
|
| 733 |
+
# Partial match
|
| 734 |
+
for candidate in candidate_keys:
|
| 735 |
+
for key, value in mgmt_data.items():
|
| 736 |
if candidate.lower() in key.lower() or key.lower() in candidate.lower():
|
| 737 |
+
print(f" β
Partial match found: '{key}' for '{candidate}'")
|
| 738 |
return value
|
| 739 |
|
| 740 |
+
print(f" β No match found for any candidate")
|
| 741 |
return None
|
| 742 |
|
| 743 |
# ============================================================================
|