Shami96 commited on
Commit
2c767ad
Β·
verified Β·
1 Parent(s): 54d9a7f

Update updated_word.py

Browse files
Files changed (1) hide show
  1. updated_word.py +17 -78
updated_word.py CHANGED
@@ -727,19 +727,8 @@ def handle_attendance_list_table_enhanced(table, flat_json):
727
  print(f" ❌ No attendance data found in JSON")
728
  return 0
729
 
730
- # Format the attendance data
731
- if isinstance(attendance_value, list):
732
- # Each item in the list should be on a separate line
733
- formatted_attendance = '\n'.join(str(item).strip() for item in attendance_value if str(item).strip())
734
- else:
735
- formatted_attendance = str(attendance_value)
736
-
737
- print(f" πŸ“ Final formatted attendance:\n{formatted_attendance}")
738
-
739
  # πŸ”§ CRITICAL FIX: Look for red text in ALL cells of the table, not just the header
740
  target_cell = None
741
- target_row_idx = None
742
- target_cell_idx = None
743
 
744
  print(f" πŸ” Scanning ALL cells in attendance table for red text...")
745
 
@@ -761,86 +750,36 @@ def handle_attendance_list_table_enhanced(table, flat_json):
761
  red_text_lower = red_text.lower()
762
  if any(indicator in red_text_lower for indicator in ['manager', 'herbig', 'palin', '–', '-']):
763
  target_cell = cell
764
- target_row_idx = row_idx
765
- target_cell_idx = cell_idx
766
  print(f" βœ… This looks like attendance data - using this cell")
767
  break
768
 
769
  if target_cell is not None:
770
  break
771
 
772
- # If no red text found that looks like attendance data, use the original approach
773
  if target_cell is None:
774
- print(f" ⚠️ No red text found that looks like attendance data, using header row approach")
775
- target_row = table.rows[found_attendance_row]
776
- target_cell = target_row.cells[found_attendance_cell]
777
 
778
- # Replace the content
779
  if has_red_text(target_cell):
780
- print(f" πŸ”§ Replacing red text in target cell...")
781
 
782
- # πŸ”§ SPECIAL FIX: Handle line breaks properly in Word
783
- if isinstance(attendance_value, list) and len(attendance_value) > 1:
784
- # Clear existing content first
785
- for paragraph in target_cell.paragraphs:
786
- for run in paragraph.runs:
787
- run.text = ''
788
-
789
- # Add first item to first paragraph
790
- if target_cell.paragraphs:
791
- target_cell.paragraphs[0].text = str(attendance_value[0]).strip()
792
-
793
- # Add remaining items as new paragraphs
794
- for item in attendance_value[1:]:
795
- item_text = str(item).strip()
796
- if item_text:
797
- new_paragraph = target_cell.add_paragraph()
798
- new_paragraph.text = item_text
799
-
800
- replacements_made += 1
801
- print(f" βœ… Added {len(attendance_value)} separate lines to attendance cell")
802
  else:
803
- # Single item - use normal replacement
804
- cell_replacements = replace_red_text_in_cell(target_cell, formatted_attendance)
805
- replacements_made += cell_replacements
806
 
807
- print(f" πŸ“Š Replacements made: {replacements_made}")
808
- else:
809
- # If no red text, try to replace content anyway
810
- current_text = get_clean_text(target_cell).strip()
811
- print(f" πŸ“‹ No red text found, current cell text: '{current_text[:50]}...'")
812
 
813
- if len(current_text) > 50: # If it contains what looks like attendance data
814
- print(f" πŸ”§ Replacing entire cell content...")
815
-
816
- # πŸ”§ SPECIAL FIX: Handle line breaks properly for non-red text too
817
- if isinstance(attendance_value, list) and len(attendance_value) > 1:
818
- # Clear existing content
819
- for paragraph in target_cell.paragraphs:
820
- for run in paragraph.runs:
821
- run.text = ''
822
-
823
- # Add first item to first paragraph
824
- if target_cell.paragraphs:
825
- target_cell.paragraphs[0].text = str(attendance_value[0]).strip()
826
-
827
- # Add remaining items as new paragraphs
828
- for item in attendance_value[1:]:
829
- item_text = str(item).strip()
830
- if item_text:
831
- new_paragraph = target_cell.add_paragraph()
832
- new_paragraph.text = item_text
833
-
834
- replacements_made += 1
835
- print(f" βœ… Added {len(attendance_value)} separate lines to cell")
836
- else:
837
- # Single item
838
- if target_cell.paragraphs:
839
- target_cell.paragraphs[0].text = formatted_attendance
840
- else:
841
- target_cell.text = formatted_attendance
842
- replacements_made += 1
843
- print(f" βœ… Replaced entire cell content")
844
 
845
  return replacements_made
846
 
 
727
  print(f" ❌ No attendance data found in JSON")
728
  return 0
729
 
 
 
 
 
 
 
 
 
 
730
  # πŸ”§ CRITICAL FIX: Look for red text in ALL cells of the table, not just the header
731
  target_cell = None
 
 
732
 
733
  print(f" πŸ” Scanning ALL cells in attendance table for red text...")
734
 
 
750
  red_text_lower = red_text.lower()
751
  if any(indicator in red_text_lower for indicator in ['manager', 'herbig', 'palin', '–', '-']):
752
  target_cell = cell
 
 
753
  print(f" βœ… This looks like attendance data - using this cell")
754
  break
755
 
756
  if target_cell is not None:
757
  break
758
 
759
+ # If no red text found that looks like attendance data, return
760
  if target_cell is None:
761
+ print(f" ⚠️ No red text found that looks like attendance data")
762
+ return 0
 
763
 
764
+ # πŸ”§ CRITICAL FIX: ONLY replace red text, preserve everything else
765
  if has_red_text(target_cell):
766
+ print(f" πŸ”§ Replacing ONLY red text in target cell...")
767
 
768
+ # Format the attendance data for replacement
769
+ if isinstance(attendance_value, list):
770
+ # Join with line breaks for Word
771
+ formatted_attendance = '\n'.join(str(item).strip() for item in attendance_value if str(item).strip())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
772
  else:
773
+ formatted_attendance = str(attendance_value)
 
 
774
 
775
+ print(f" πŸ“ Replacement text:\n{formatted_attendance}")
 
 
 
 
776
 
777
+ # Use the existing replace_red_text_in_cell function which preserves non-red text
778
+ cell_replacements = replace_red_text_in_cell(target_cell, formatted_attendance)
779
+ replacements_made += cell_replacements
780
+
781
+ print(f" βœ… Replaced only red text, preserved other content")
782
+ print(f" πŸ“Š Replacements made: {cell_replacements}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
783
 
784
  return replacements_made
785