Spaces:
Running
Running
Update updated_word.py
Browse files- 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,
|
| 773 |
if target_cell is None:
|
| 774 |
-
print(f" β οΈ No red text found that looks like attendance data
|
| 775 |
-
|
| 776 |
-
target_cell = target_row.cells[found_attendance_cell]
|
| 777 |
|
| 778 |
-
#
|
| 779 |
if has_red_text(target_cell):
|
| 780 |
-
print(f" π§ Replacing red text in target cell...")
|
| 781 |
|
| 782 |
-
#
|
| 783 |
-
if isinstance(attendance_value, list)
|
| 784 |
-
#
|
| 785 |
-
for
|
| 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 |
-
|
| 804 |
-
cell_replacements = replace_red_text_in_cell(target_cell, formatted_attendance)
|
| 805 |
-
replacements_made += cell_replacements
|
| 806 |
|
| 807 |
-
print(f"
|
| 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 |
-
|
| 814 |
-
|
| 815 |
-
|
| 816 |
-
|
| 817 |
-
|
| 818 |
-
|
| 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 |
|