Spaces:
Running
Running
Update updated_word.py
Browse files- updated_word.py +46 -48
updated_word.py
CHANGED
|
@@ -727,10 +727,9 @@ 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 |
# Format the attendance data
|
| 732 |
if isinstance(attendance_value, list):
|
| 733 |
-
|
| 734 |
formatted_attendance = '\n'.join(str(item).strip() for item in attendance_value if str(item).strip())
|
| 735 |
else:
|
| 736 |
formatted_attendance = str(attendance_value)
|
|
@@ -777,47 +776,12 @@ def handle_attendance_list_table_enhanced(table, flat_json):
|
|
| 777 |
target_cell = target_row.cells[found_attendance_cell]
|
| 778 |
|
| 779 |
# Replace the content
|
| 780 |
-
|
| 781 |
-
|
| 782 |
-
print(f" π§ Replacing red text in target cell...")
|
| 783 |
-
|
| 784 |
-
# π§ SPECIAL FIX: Handle line breaks properly in Word
|
| 785 |
-
if isinstance(attendance_value, list) and len(attendance_value) > 1:
|
| 786 |
-
# Clear existing content first
|
| 787 |
-
for paragraph in target_cell.paragraphs:
|
| 788 |
-
for run in paragraph.runs:
|
| 789 |
-
run.text = ''
|
| 790 |
-
|
| 791 |
-
# Add first item to first paragraph
|
| 792 |
-
if target_cell.paragraphs:
|
| 793 |
-
target_cell.paragraphs[0].text = str(attendance_value[0]).strip()
|
| 794 |
|
| 795 |
-
#
|
| 796 |
-
for item in attendance_value[1:]:
|
| 797 |
-
item_text = str(item).strip()
|
| 798 |
-
if item_text:
|
| 799 |
-
new_paragraph = target_cell.add_paragraph()
|
| 800 |
-
new_paragraph.text = item_text
|
| 801 |
-
|
| 802 |
-
replacements_made += 1
|
| 803 |
-
print(f" β
Added {len(attendance_value)} separate lines to attendance cell")
|
| 804 |
-
else:
|
| 805 |
-
# Single item - use normal replacement
|
| 806 |
-
cell_replacements = replace_red_text_in_cell(target_cell, formatted_attendance)
|
| 807 |
-
replacements_made += cell_replacements
|
| 808 |
-
|
| 809 |
-
print(f" π Replacements made: {replacements_made}")
|
| 810 |
-
else:
|
| 811 |
-
# If no red text, try to replace content anyway
|
| 812 |
-
current_text = get_clean_text(target_cell).strip()
|
| 813 |
-
print(f" π No red text found, current cell text: '{current_text[:50]}...'")
|
| 814 |
-
|
| 815 |
-
if len(current_text) > 50: # If it contains what looks like attendance data
|
| 816 |
-
print(f" π§ Replacing entire cell content...")
|
| 817 |
-
|
| 818 |
-
# π§ SPECIAL FIX: Handle line breaks properly for non-red text too
|
| 819 |
if isinstance(attendance_value, list) and len(attendance_value) > 1:
|
| 820 |
-
# Clear existing content
|
| 821 |
for paragraph in target_cell.paragraphs:
|
| 822 |
for run in paragraph.runs:
|
| 823 |
run.text = ''
|
|
@@ -834,15 +798,49 @@ else:
|
|
| 834 |
new_paragraph.text = item_text
|
| 835 |
|
| 836 |
replacements_made += 1
|
| 837 |
-
print(f" β
Added {len(attendance_value)} separate lines to cell")
|
| 838 |
else:
|
| 839 |
-
# Single item
|
| 840 |
-
|
| 841 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 842 |
else:
|
| 843 |
-
|
| 844 |
-
|
| 845 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 846 |
|
| 847 |
return replacements_made
|
| 848 |
|
|
|
|
| 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)
|
|
|
|
| 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 = ''
|
|
|
|
| 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 |
|