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

Update updated_word.py

Browse files
Files changed (1) hide show
  1. updated_word.py +59 -64
updated_word.py CHANGED
@@ -600,41 +600,44 @@ 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
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
 
610
- # Try variations of the key
611
  if not mgmt_data:
612
- for key in flat_json.keys():
613
- key_lower = key.lower()
614
- mgmt_lower = mgmt_type.lower()
615
- if mgmt_lower in key_lower or key_lower in mgmt_lower:
616
- mgmt_data = flat_json[key]
617
- print(f" βœ… Found data using key variation: '{key}'")
618
  break
619
 
620
- # If still no data, look for individual standard data
621
  if not mgmt_data:
622
- # Collect individual standard entries
623
  mgmt_data = {}
624
  for key, value in flat_json.items():
625
- key_lower = key.lower()
626
- # Look for standard entries related to this management type
627
- if ("std " in key_lower and
628
- (("mass" in mgmt_type.lower() and any(term in key_lower for term in ["verification", "internal review"])) or
629
- ("maintenance" in mgmt_type.lower() and any(term in key_lower for term in ["daily check", "internal review"])) or
630
- ("fatigue" in mgmt_type.lower() and any(term in key_lower for term in ["internal review"])))):
631
- mgmt_data[key] = value
632
 
633
  if mgmt_data:
634
- print(f" βœ… Collected individual standard data: {list(mgmt_data.keys())}")
 
 
 
 
635
 
636
- if not mgmt_data or not isinstance(mgmt_data, dict):
637
- print(f" ⚠️ No JSON management dict found for {mgmt_type}, skipping this type")
 
638
  continue
639
 
640
  # Process the table rows
@@ -648,46 +651,34 @@ def fix_management_summary_details_column(table, flat_json):
648
  if "standard" in standard_text or "requirement" in standard_text or "details" in standard_text:
649
  continue
650
 
651
- # Look for specific standards
652
- if "std 5" in standard_text or "verification" in standard_text:
653
- if has_red_text(details_cell):
654
- std_val = find_best_standard_value(mgmt_data, ["Std 5. Verification", "Std 5 Verification", "Std 5", "Verification"])
655
- if std_val:
656
- replacement_text = get_value_as_string(std_val, "Std 5. Verification")
657
- cell_replacements = replace_red_text_in_cell(details_cell, replacement_text)
658
- replacements_made += cell_replacements
659
- if cell_replacements:
660
- print(f" βœ… Replaced Std 5. Verification details for {mgmt_type}")
661
-
662
- elif "std 6" in standard_text or "internal review" in standard_text:
663
- if has_red_text(details_cell):
664
- std_val = find_best_standard_value(mgmt_data, ["Std 6. Internal Review", "Std 6 Internal Review", "Std 6", "Internal Review"])
665
- if std_val:
666
- replacement_text = get_value_as_string(std_val, "Std 6. Internal Review")
667
- cell_replacements = replace_red_text_in_cell(details_cell, replacement_text)
668
- replacements_made += cell_replacements
669
- if cell_replacements:
670
- print(f" βœ… Replaced Std 6. Internal Review details for {mgmt_type}")
671
 
672
- elif "std 1" in standard_text or "daily check" in standard_text:
673
- if has_red_text(details_cell):
674
- std_val = find_best_standard_value(mgmt_data, ["Std 1. Daily Check", "Std 1 Daily Check", "Std 1", "Daily Check"])
675
- if std_val:
676
- replacement_text = get_value_as_string(std_val, "Std 1. Daily Check")
677
- cell_replacements = replace_red_text_in_cell(details_cell, replacement_text)
678
- replacements_made += cell_replacements
679
- if cell_replacements:
680
- print(f" βœ… Replaced Std 1. Daily Check details for {mgmt_type}")
681
 
 
 
 
 
 
 
 
 
 
 
682
  elif "std 7" in standard_text:
683
- if has_red_text(details_cell):
684
- std_val = find_best_standard_value(mgmt_data, ["Std 7. Internal Review", "Std 7 Internal Review", "Std 7"])
685
- if std_val:
686
- replacement_text = get_value_as_string(std_val, "Std 7. Internal Review")
687
- cell_replacements = replace_red_text_in_cell(details_cell, replacement_text)
688
- replacements_made += cell_replacements
689
- if cell_replacements:
690
- print(f" βœ… Replaced Std 7. Internal Review details for {mgmt_type}")
 
 
 
691
 
692
  return replacements_made
693
 
@@ -1346,7 +1337,7 @@ def process_paragraphs(document, flat_json):
1346
 
1347
  def process_headings(document, flat_json):
1348
  """
1349
- IMPROVED: Better heading processing that avoids mixing company data
1350
  """
1351
  replacements_made = 0
1352
  print(f"\nπŸ” Processing headings:")
@@ -1435,7 +1426,7 @@ def process_headings(document, flat_json):
1435
  return replacements_made
1436
 
1437
  def process_red_text_in_heading_paragraph(paragraph, paragraph_text, flat_json, operator_name):
1438
- """Process red text found in heading paragraphs"""
1439
  replacements_made = 0
1440
  red_text_segments = []
1441
 
@@ -1495,23 +1486,25 @@ def process_red_text_in_heading_paragraph(paragraph, paragraph_text, flat_json,
1495
  print(f" βœ… Found match with combined query: {kv[0]}")
1496
  break
1497
 
1498
- # Apply the replacement if we found a suitable value
1499
  if replacement_value:
1500
  red_runs = [run for run in paragraph.runs if is_red(run) and run.text.strip()]
1501
  if red_runs:
 
1502
  red_runs[0].text = replacement_value
1503
  red_runs[0].font.color.rgb = RGBColor(0, 0, 0)
 
1504
  for run in red_runs[1:]:
1505
  run.text = ''
1506
  replacements_made = 1
1507
- print(f" βœ… Replaced with: '{replacement_value}'")
1508
  else:
1509
  print(f" ❌ No suitable replacement found for: '{combined_red_text}'")
1510
 
1511
  return replacements_made
1512
 
1513
  def process_red_text_in_context_paragraph(paragraph, heading_text, flat_json, operator_name):
1514
- """Process red text found in paragraphs following headings"""
1515
  replacements_made = 0
1516
  red_text_segments = []
1517
 
@@ -1571,16 +1564,18 @@ def process_red_text_in_context_paragraph(paragraph, heading_text, flat_json, op
1571
  print(f" βœ… Found match with combined query: {kv[0]}")
1572
  break
1573
 
1574
- # Apply the replacement if we found a suitable value
1575
  if replacement_value:
1576
  red_runs = [run for run in paragraph.runs if is_red(run) and run.text.strip()]
1577
  if red_runs:
 
1578
  red_runs[0].text = replacement_value
1579
  red_runs[0].font.color.rgb = RGBColor(0, 0, 0)
 
1580
  for run in red_runs[1:]:
1581
  run.text = ''
1582
  replacements_made = 1
1583
- print(f" βœ… Replaced with: '{replacement_value}'")
1584
  else:
1585
  print(f" ❌ No suitable replacement found for: '{combined_red_text}'")
1586
 
 
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
 
651
  if "standard" in standard_text or "requirement" in standard_text or "details" in standard_text:
652
  continue
653
 
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
 
 
1337
 
1338
  def process_headings(document, flat_json):
1339
  """
1340
+ FIXED: Better heading processing with proper red text replacement
1341
  """
1342
  replacements_made = 0
1343
  print(f"\nπŸ” Processing headings:")
 
1426
  return replacements_made
1427
 
1428
  def process_red_text_in_heading_paragraph(paragraph, paragraph_text, flat_json, operator_name):
1429
+ """Process red text found in heading paragraphs - FIXED"""
1430
  replacements_made = 0
1431
  red_text_segments = []
1432
 
 
1486
  print(f" βœ… Found match with combined query: {kv[0]}")
1487
  break
1488
 
1489
+ # FIXED: Apply the replacement if we found a suitable value
1490
  if replacement_value:
1491
  red_runs = [run for run in paragraph.runs if is_red(run) and run.text.strip()]
1492
  if red_runs:
1493
+ # Replace the first red run with the new text
1494
  red_runs[0].text = replacement_value
1495
  red_runs[0].font.color.rgb = RGBColor(0, 0, 0)
1496
+ # Clear subsequent red runs
1497
  for run in red_runs[1:]:
1498
  run.text = ''
1499
  replacements_made = 1
1500
+ print(f" βœ… Replaced heading red text with: '{replacement_value}'")
1501
  else:
1502
  print(f" ❌ No suitable replacement found for: '{combined_red_text}'")
1503
 
1504
  return replacements_made
1505
 
1506
  def process_red_text_in_context_paragraph(paragraph, heading_text, flat_json, operator_name):
1507
+ """Process red text found in paragraphs following headings - FIXED"""
1508
  replacements_made = 0
1509
  red_text_segments = []
1510
 
 
1564
  print(f" βœ… Found match with combined query: {kv[0]}")
1565
  break
1566
 
1567
+ # FIXED: Apply the replacement if we found a suitable value
1568
  if replacement_value:
1569
  red_runs = [run for run in paragraph.runs if is_red(run) and run.text.strip()]
1570
  if red_runs:
1571
+ # Replace the first red run with the new text
1572
  red_runs[0].text = replacement_value
1573
  red_runs[0].font.color.rgb = RGBColor(0, 0, 0)
1574
+ # Clear subsequent red runs
1575
  for run in red_runs[1:]:
1576
  run.text = ''
1577
  replacements_made = 1
1578
+ print(f" βœ… Replaced context red text with: '{replacement_value}'")
1579
  else:
1580
  print(f" ❌ No suitable replacement found for: '{combined_red_text}'")
1581