Shami96 commited on
Commit
4451af2
Β·
verified Β·
1 Parent(s): 450ea05

Update updated_word.py

Browse files
Files changed (1) hide show
  1. updated_word.py +128 -37
updated_word.py CHANGED
@@ -638,51 +638,52 @@ def fix_operator_declaration_empty_values(table, flat_json):
638
 
639
  print(f" πŸ“‹ Current values: Name='{name_text}', Position='{position_text}'")
640
 
641
- # Get the Operator Declaration section data
642
- operator_declaration = find_matching_json_value("Operator Declaration", flat_json)
 
 
 
 
 
 
 
643
 
644
- if operator_declaration and isinstance(operator_declaration, dict):
645
- print(f" πŸ” Found Operator Declaration data: {operator_declaration}")
 
 
 
 
 
 
 
 
 
 
646
 
647
- # Update Print Name
648
- if "Print Name" in operator_declaration:
649
- print_name_value = operator_declaration["Print Name"]
650
- if isinstance(print_name_value, list) and print_name_value:
651
- new_name = str(print_name_value[0]).strip()
652
- if new_name and "Pty Ltd" not in new_name and "Company" not in new_name:
 
653
  name_cell.text = new_name
654
  replacements_made += 1
655
- print(f" βœ… Updated Print Name: '{name_text}' -> '{new_name}'")
 
656
 
657
- # Update Position Title
658
- if "Position Title" in operator_declaration:
659
- position_value = operator_declaration["Position Title"]
660
- if isinstance(position_value, list) and position_value:
661
- new_position = str(position_value[0]).strip()
 
662
  if new_position:
663
  position_cell.text = new_position
664
  replacements_made += 1
665
- print(f" βœ… Updated Position Title: '{position_text}' -> '{new_position}'")
666
-
667
- else:
668
- print(f" ❌ No Operator Declaration section found in JSON")
669
-
670
- # Fallback: try individual fields
671
- name_value = find_matching_json_value("Operator Declaration.Print Name", flat_json)
672
- if name_value:
673
- new_name = get_value_as_string(name_value).strip()
674
- if new_name and "Pty Ltd" not in new_name:
675
- name_cell.text = new_name
676
- replacements_made += 1
677
- print(f" βœ… Updated Print Name (fallback): '{new_name}'")
678
-
679
- position_value = find_matching_json_value("Operator Declaration.Position Title", flat_json)
680
- if position_value:
681
- new_position = get_value_as_string(position_value).strip()
682
- if new_position:
683
- position_cell.text = new_position
684
- replacements_made += 1
685
- print(f" βœ… Updated Position Title (fallback): '{new_position}'")
686
  break
687
 
688
  return replacements_made
@@ -773,6 +774,96 @@ def handle_management_summary_fix(cell, flat_json):
773
 
774
  return replacements_made
775
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
776
  def handle_operator_declaration_fix(table, flat_json):
777
  """Handle small Operator/Auditor Declaration tables - SKIP if already processed"""
778
  replacements_made = 0
 
638
 
639
  print(f" πŸ“‹ Current values: Name='{name_text}', Position='{position_text}'")
640
 
641
+ # FORCE UPDATE - try direct fields
642
+ print(f" πŸ”§ FORCE updating Print Name")
643
+ name_value = find_matching_json_value("Operator Declaration.Print Name", flat_json)
644
+ if name_value:
645
+ new_name = get_value_as_string(name_value).strip()
646
+ if new_name and "Pty Ltd" not in new_name and "Company" not in new_name and "Farming" not in new_name:
647
+ name_cell.text = new_name # FORCE replace
648
+ replacements_made += 1
649
+ print(f" βœ… FORCE Updated Print Name: '{name_text}' -> '{new_name}'")
650
 
651
+ print(f" πŸ”§ FORCE updating Position Title")
652
+ position_value = find_matching_json_value("Operator Declaration.Position Title", flat_json)
653
+ if position_value:
654
+ new_position = get_value_as_string(position_value).strip()
655
+ if new_position:
656
+ position_cell.text = new_position # FORCE replace
657
+ replacements_made += 1
658
+ print(f" βœ… FORCE Updated Position Title: '{position_text}' -> '{new_position}'")
659
+
660
+ # If still no updates, try alternative sources
661
+ if replacements_made == 0:
662
+ print(f" πŸ”§ Trying alternative sources...")
663
 
664
+ # Try Print Name alternatives
665
+ alt_name_sources = ["Print Name"]
666
+ for source in alt_name_sources:
667
+ name_value = find_matching_json_value(source, flat_json)
668
+ if name_value:
669
+ new_name = get_value_as_string(name_value).strip()
670
+ if new_name and "Pty Ltd" not in new_name and "Company" not in new_name and "Farming" not in new_name:
671
  name_cell.text = new_name
672
  replacements_made += 1
673
+ print(f" βœ… Updated Print Name (alt): '{new_name}' from {source}")
674
+ break
675
 
676
+ # Try Position Title alternatives
677
+ alt_position_sources = ["Position Title"]
678
+ for source in alt_position_sources:
679
+ position_value = find_matching_json_value(source, flat_json)
680
+ if position_value:
681
+ new_position = get_value_as_string(position_value).strip()
682
  if new_position:
683
  position_cell.text = new_position
684
  replacements_made += 1
685
+ print(f" βœ… Updated Position Title (alt): '{new_position}' from {source}")
686
+ break
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
687
  break
688
 
689
  return replacements_made
 
774
 
775
  return replacements_made
776
 
777
+ def fix_operator_declaration_empty_values(table, flat_json):
778
+ """Fix Operator Declaration table when values are empty or need updating"""
779
+ replacements_made = 0
780
+
781
+ print(f" 🎯 FIX: Operator Declaration empty values processing")
782
+
783
+ # Check if this is an Operator Declaration table
784
+ table_context = ""
785
+ for row in table.rows:
786
+ for cell in row.cells:
787
+ table_context += get_clean_text(cell).lower() + " "
788
+
789
+ if not ("print name" in table_context and "position title" in table_context):
790
+ return 0
791
+
792
+ print(f" βœ… Confirmed Operator Declaration table")
793
+
794
+ # Find the data row with Print Name and Position Title
795
+ for row_idx, row in enumerate(table.rows):
796
+ if len(row.cells) >= 2:
797
+ cell1_text = get_clean_text(row.cells[0]).strip().lower()
798
+ cell2_text = get_clean_text(row.cells[1]).strip().lower()
799
+
800
+ # Check if this is the header row
801
+ if "print name" in cell1_text and "position" in cell2_text:
802
+ print(f" πŸ“Œ Found header row at {row_idx + 1}")
803
+
804
+ # Look for the data row (next row)
805
+ if row_idx + 1 < len(table.rows):
806
+ data_row = table.rows[row_idx + 1]
807
+ if len(data_row.cells) >= 2:
808
+ name_cell = data_row.cells[0]
809
+ position_cell = data_row.cells[1]
810
+
811
+ # Check if cells are empty or have red text
812
+ name_text = get_clean_text(name_cell).strip()
813
+ position_text = get_clean_text(position_cell).strip()
814
+
815
+ print(f" πŸ“‹ Current values: Name='{name_text}', Position='{position_text}'")
816
+
817
+ # FORCE UPDATE - try direct fields
818
+ print(f" πŸ”§ FORCE updating Print Name")
819
+ name_value = find_matching_json_value("Operator Declaration.Print Name", flat_json)
820
+ if name_value:
821
+ new_name = get_value_as_string(name_value).strip()
822
+ if new_name and "Pty Ltd" not in new_name and "Company" not in new_name and "Farming" not in new_name:
823
+ name_cell.text = new_name # FORCE replace
824
+ replacements_made += 1
825
+ print(f" βœ… FORCE Updated Print Name: '{name_text}' -> '{new_name}'")
826
+
827
+ print(f" πŸ”§ FORCE updating Position Title")
828
+ position_value = find_matching_json_value("Operator Declaration.Position Title", flat_json)
829
+ if position_value:
830
+ new_position = get_value_as_string(position_value).strip()
831
+ if new_position:
832
+ position_cell.text = new_position # FORCE replace
833
+ replacements_made += 1
834
+ print(f" βœ… FORCE Updated Position Title: '{position_text}' -> '{new_position}'")
835
+
836
+ # If still no updates, try alternative sources
837
+ if replacements_made == 0:
838
+ print(f" πŸ”§ Trying alternative sources...")
839
+
840
+ # Try Print Name alternatives
841
+ alt_name_sources = ["Print Name"]
842
+ for source in alt_name_sources:
843
+ name_value = find_matching_json_value(source, flat_json)
844
+ if name_value:
845
+ new_name = get_value_as_string(name_value).strip()
846
+ if new_name and "Pty Ltd" not in new_name and "Company" not in new_name and "Farming" not in new_name:
847
+ name_cell.text = new_name
848
+ replacements_made += 1
849
+ print(f" βœ… Updated Print Name (alt): '{new_name}' from {source}")
850
+ break
851
+
852
+ # Try Position Title alternatives
853
+ alt_position_sources = ["Position Title"]
854
+ for source in alt_position_sources:
855
+ position_value = find_matching_json_value(source, flat_json)
856
+ if position_value:
857
+ new_position = get_value_as_string(position_value).strip()
858
+ if new_position:
859
+ position_cell.text = new_position
860
+ replacements_made += 1
861
+ print(f" βœ… Updated Position Title (alt): '{new_position}' from {source}")
862
+ break
863
+ break
864
+
865
+ return replacements_made
866
+
867
  def handle_operator_declaration_fix(table, flat_json):
868
  """Handle small Operator/Auditor Declaration tables - SKIP if already processed"""
869
  replacements_made = 0