Shami96 commited on
Commit
4091415
Β·
verified Β·
1 Parent(s): 6067db5

Update updated_word.py

Browse files
Files changed (1) hide show
  1. 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 with better data matching"""
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
- # Look for management data in the JSON - FIXED: Use exact key matching
604
- mgmt_data = None
605
 
606
- # Try direct key match first
607
  if mgmt_type in flat_json:
608
- mgmt_data = flat_json[mgmt_type]
609
- print(f" βœ… Found direct match for: '{mgmt_type}'")
 
 
610
 
611
- # FIXED: Look for the actual keys in the JSON data
612
  if not mgmt_data:
613
- # Search for keys that contain the management type
614
  for key, value in flat_json.items():
615
- if mgmt_type.lower().replace(" ", "") in key.lower().replace(" ", ""):
616
- mgmt_data = value
617
- print(f" βœ… Found data using key: '{key}'")
618
- break
 
 
 
 
619
 
620
- # FIXED: Also check for flattened keys like "Mass Management Summary.Std 5. Verification"
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
- # Extract the standard part (after the dot)
626
- if "." in key:
627
- std_key = key.split(".", 1)[1] # Get everything after first dot
628
- mgmt_data[std_key] = value
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
- # FIXED: Handle both dict and direct value formats
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
- # FIXED: Better standard matching
 
 
658
  replacement_value = None
659
 
660
- # Look for specific standards with better matching
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
- elif "std 5" in standard_text and ("verification" in standard_text or "internal review" in standard_text):
 
 
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
- replacement_text = get_value_as_string(replacement_value, f"Standard for {mgmt_type}")
 
 
 
 
 
 
 
 
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
- # Try fuzzy matching
693
- for key, value in mgmt_data.items():
694
- for candidate in candidate_keys:
 
 
 
 
 
 
 
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
  # ============================================================================