Shami96 commited on
Commit
428b626
Β·
verified Β·
1 Parent(s): 2c767ad

Update updated_word.py

Browse files
Files changed (1) hide show
  1. updated_word.py +56 -10
updated_word.py CHANGED
@@ -673,6 +673,51 @@ def process_single_column_sections(cell, field_name, flat_json):
673
  return cell_replacements
674
  return 0
675
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
676
  def handle_attendance_list_table_enhanced(table, flat_json):
677
  """Enhanced Attendance List processing with better detection"""
678
  replacements_made = 0
@@ -761,24 +806,25 @@ def handle_attendance_list_table_enhanced(table, flat_json):
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
 
673
  return cell_replacements
674
  return 0
675
 
676
+ def replace_red_text_with_line_breaks(cell, attendance_list):
677
+ """Custom function to replace red text with properly formatted attendance list"""
678
+ replacements_made = 0
679
+
680
+ # Find all red text runs
681
+ red_runs = []
682
+ for paragraph in cell.paragraphs:
683
+ for run in paragraph.runs:
684
+ if is_red(run) and run.text.strip():
685
+ red_runs.append(run)
686
+
687
+ if not red_runs:
688
+ return 0
689
+
690
+ # Clear all red text first
691
+ for run in red_runs:
692
+ run.text = ''
693
+
694
+ # Find the first red run's paragraph to insert our content
695
+ if red_runs:
696
+ first_run = red_runs[0]
697
+ paragraph = first_run.element.getparent()
698
+
699
+ # Add first attendance item to the first red run
700
+ if len(attendance_list) > 0:
701
+ first_run.text = str(attendance_list[0]).strip()
702
+ first_run.font.color.rgb = RGBColor(0, 0, 0) # Make it black
703
+ replacements_made += 1
704
+
705
+ # Add remaining items with line breaks
706
+ for item in attendance_list[1:]:
707
+ item_text = str(item).strip()
708
+ if item_text:
709
+ # Add a line break
710
+ from docx.oxml import OxmlElement, ns
711
+ br = OxmlElement('w:br')
712
+ first_run.element.append(br)
713
+
714
+ # Add the text
715
+ new_run = paragraph.add_run(item_text)
716
+ new_run.font.color.rgb = RGBColor(0, 0, 0) # Make it black
717
+ replacements_made += 1
718
+
719
+ return replacements_made
720
+
721
  def handle_attendance_list_table_enhanced(table, flat_json):
722
  """Enhanced Attendance List processing with better detection"""
723
  replacements_made = 0
 
806
  print(f" ⚠️ No red text found that looks like attendance data")
807
  return 0
808
 
809
+ # πŸ”§ NEW FIX: Use custom function to handle line breaks properly
810
  if has_red_text(target_cell):
811
+ print(f" πŸ”§ Replacing red text with properly formatted attendance list...")
812
 
813
+ # Ensure attendance_value is a list
814
  if isinstance(attendance_value, list):
815
+ attendance_list = [str(item).strip() for item in attendance_value if str(item).strip()]
 
816
  else:
817
+ attendance_list = [str(attendance_value).strip()]
818
 
819
+ print(f" πŸ“ Attendance items to add:")
820
+ for i, item in enumerate(attendance_list):
821
+ print(f" {i+1}. {item}")
822
 
823
+ # Use our custom function that handles line breaks properly
824
+ cell_replacements = replace_red_text_with_line_breaks(target_cell, attendance_list)
825
  replacements_made += cell_replacements
826
 
827
+ print(f" βœ… Added {len(attendance_list)} attendance items with proper line breaks")
828
  print(f" πŸ“Š Replacements made: {cell_replacements}")
829
 
830
  return replacements_made