Shami96 commited on
Commit
76ff551
Β·
verified Β·
1 Parent(s): 182d927

Update updated_word.py

Browse files
Files changed (1) hide show
  1. updated_word.py +87 -61
updated_word.py CHANGED
@@ -705,94 +705,120 @@ def handle_attendance_list_table_enhanced(table, flat_json):
705
  if found_attendance_row is None:
706
  return 0
707
 
708
- # πŸ”§ FIX: Enhanced search for attendance data in JSON with nested structure handling
709
  attendance_value = None
710
- attendance_search_keys = [
 
 
 
 
 
 
 
 
 
711
  "Attendance List (Names and Position Titles)",
 
 
712
  "attendance list",
713
- "attendees",
714
- "names and position titles"
715
  ]
716
 
717
- print(f" πŸ” Searching for attendance data in JSON...")
718
-
719
- for search_key in attendance_search_keys:
720
- # Try exact match first
721
- attendance_value = find_matching_json_value(search_key, flat_json)
722
- if attendance_value is not None:
723
- print(f" βœ… Found attendance data with key: '{search_key}'")
724
- print(f" πŸ“Š Data type: {type(attendance_value)}")
725
  break
726
 
727
- # πŸ”§ FIX: Handle nested structure - if we get a dict, try to extract the inner array
728
- if isinstance(attendance_value, dict):
729
- print(f" πŸ”§ Attendance value is dict, looking for nested array...")
730
- # Look for the same key inside the dict (nested structure)
731
- for inner_key, inner_value in attendance_value.items():
732
- if isinstance(inner_value, list):
733
- print(f" βœ… Found nested list with key: '{inner_key}'")
734
- attendance_value = inner_value
735
  break
736
-
737
- # If still a dict, try to get any list value
738
- if isinstance(attendance_value, dict):
739
- for inner_value in attendance_value.values():
740
- if isinstance(inner_value, list):
741
- attendance_value = inner_value
742
- break
743
 
744
  if attendance_value is None:
745
  print(f" ❌ No attendance data found in JSON")
746
-
747
- # πŸ”§ FIX: Additional debug - let's see what keys we have that might match
748
- print(f" πŸ” Available keys containing 'attendance':")
749
- for key in flat_json.keys():
750
- if 'attendance' in key.lower():
751
- print(f" - {key}: {flat_json[key]}")
752
-
753
  return 0
754
 
755
- # Process the attendance cell
756
- target_row = table.rows[found_attendance_row]
757
- target_cell = target_row.cells[found_attendance_cell]
758
-
759
- # πŸ”§ FIX: Better formatting of attendance data
760
- if isinstance(attendance_value, list):
761
- # Handle each item in the list
762
- formatted_items = []
763
- for item in attendance_value:
764
- if isinstance(item, str):
765
- # Split on common separators and format each person on new line
766
- if '–' in item or '-' in item:
767
- # Split by – or - and clean up
768
- people = item.replace('–', '\n').replace('-', '\n').strip()
769
- formatted_items.append(people)
770
- else:
771
- formatted_items.append(str(item).strip())
772
- else:
773
- formatted_items.append(str(item).strip())
774
 
775
- formatted_attendance = '\n'.join(formatted_items)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
776
  else:
777
  formatted_attendance = str(attendance_value)
778
- # Handle embedded separators
779
- if '–' in formatted_attendance or '-' in formatted_attendance:
780
- formatted_attendance = formatted_attendance.replace('–', '\n').replace('-', '\n')
781
 
782
- print(f" πŸ“ Formatted attendance data:\n{formatted_attendance}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
783
 
784
  # Replace red text or entire cell content if needed
785
  if has_red_text(target_cell):
786
  cell_replacements = replace_red_text_in_cell(target_cell, formatted_attendance)
787
  replacements_made += cell_replacements
788
- print(f" βœ… Replaced red text in attendance list with: '{formatted_attendance[:50]}...'")
 
789
  else:
790
  # If no red text, check if cell is mostly empty and fill it
791
  current_text = get_clean_text(target_cell).strip()
 
 
792
  if not current_text or len(current_text) < 20: # Likely placeholder
793
- target_cell.text = formatted_attendance
 
 
 
 
 
 
 
 
 
 
794
  replacements_made += 1
795
- print(f" βœ… Filled empty attendance cell with: '{formatted_attendance[:50]}...'")
 
 
796
 
797
  return replacements_made
798
 
 
705
  if found_attendance_row is None:
706
  return 0
707
 
708
+ # πŸ”§ SPECIFIC FIX: Look for your exact JSON structure
709
  attendance_value = None
710
+
711
+ # Debug: Print all keys to see what we have
712
+ print(f" πŸ” DEBUG: All available keys:")
713
+ for key in sorted(flat_json.keys()):
714
+ if 'attendance' in key.lower():
715
+ print(f" - '{key}': {flat_json[key]}")
716
+
717
+ # Try multiple variations of the key
718
+ possible_keys = [
719
+ "Attendance List (Names and Position Titles).Attendance List (Names and Position Titles)",
720
  "Attendance List (Names and Position Titles)",
721
+ "attendance list (names and position titles).attendance list (names and position titles)",
722
+ "attendance list (names and position titles)",
723
  "attendance list",
724
+ "attendees"
 
725
  ]
726
 
727
+ for key in possible_keys:
728
+ if key in flat_json:
729
+ attendance_value = flat_json[key]
730
+ print(f" βœ… Found attendance data with exact key: '{key}'")
731
+ print(f" πŸ“Š Raw value: {attendance_value}")
 
 
 
732
  break
733
 
734
+ # If still not found, try case-insensitive search
735
+ if attendance_value is None:
736
+ for key, value in flat_json.items():
737
+ if 'attendance' in key.lower() and 'names' in key.lower():
738
+ attendance_value = value
739
+ print(f" βœ… Found attendance data with case-insensitive key: '{key}'")
740
+ print(f" πŸ“Š Raw value: {attendance_value}")
 
741
  break
 
 
 
 
 
 
 
742
 
743
  if attendance_value is None:
744
  print(f" ❌ No attendance data found in JSON")
 
 
 
 
 
 
 
745
  return 0
746
 
747
+ # πŸ”§ SPECIFIC FIX: Handle your exact data format
748
+ print(f" πŸ“Š Processing attendance value type: {type(attendance_value)}")
749
+
750
+ if isinstance(attendance_value, list) and len(attendance_value) > 0:
751
+ # Get the first item from the list
752
+ first_item = attendance_value[0]
753
+ print(f" πŸ“Š First item: '{first_item}'")
 
 
 
 
 
 
 
 
 
 
 
 
754
 
755
+ # Your data is "Liam Herbig – Manager Deborah Herbig – Manager"
756
+ # Split this into separate people
757
+ if isinstance(first_item, str):
758
+ # Split by Manager and reconstruct
759
+ parts = first_item.split('Manager')
760
+ formatted_people = []
761
+
762
+ for part in parts:
763
+ part = part.strip()
764
+ if part:
765
+ # Remove any trailing/leading dashes
766
+ part = part.strip('–').strip('-').strip()
767
+ if part:
768
+ formatted_people.append(f"{part.strip()} - Manager")
769
+
770
+ # Join with newlines
771
+ formatted_attendance = '\n'.join(formatted_people)
772
+ else:
773
+ formatted_attendance = str(first_item)
774
  else:
775
  formatted_attendance = str(attendance_value)
 
 
 
776
 
777
+ print(f" πŸ“ Final formatted attendance:\n{formatted_attendance}")
778
+
779
+ # Process the attendance cell - look in ALL cells of the found row
780
+ target_row = table.rows[found_attendance_row]
781
+
782
+ # Try to find a cell with red text in this row
783
+ target_cell = None
784
+ for cell in target_row.cells:
785
+ if has_red_text(cell):
786
+ target_cell = cell
787
+ print(f" 🎯 Found red text in attendance row")
788
+ break
789
+
790
+ # If no red text found, use the cell we originally found
791
+ if target_cell is None:
792
+ target_cell = target_row.cells[found_attendance_cell]
793
+ print(f" πŸ“ Using original cell for attendance")
794
 
795
  # Replace red text or entire cell content if needed
796
  if has_red_text(target_cell):
797
  cell_replacements = replace_red_text_in_cell(target_cell, formatted_attendance)
798
  replacements_made += cell_replacements
799
+ print(f" βœ… Replaced red text in attendance list")
800
+ print(f" πŸ“Š Replacements made: {cell_replacements}")
801
  else:
802
  # If no red text, check if cell is mostly empty and fill it
803
  current_text = get_clean_text(target_cell).strip()
804
+ print(f" πŸ“‹ Current cell text: '{current_text}'")
805
+
806
  if not current_text or len(current_text) < 20: # Likely placeholder
807
+ # Clear all existing content and set new text
808
+ for paragraph in target_cell.paragraphs:
809
+ for run in paragraph.runs:
810
+ run.text = ''
811
+
812
+ # Add new content
813
+ if target_cell.paragraphs:
814
+ target_cell.paragraphs[0].text = formatted_attendance
815
+ else:
816
+ target_cell.text = formatted_attendance
817
+
818
  replacements_made += 1
819
+ print(f" βœ… Filled attendance cell with new content")
820
+ else:
821
+ print(f" ⚠️ Cell has content but no red text - manual check needed")
822
 
823
  return replacements_made
824