Spaces:
Running
Running
Update updated_word.py
Browse files- 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 |
-
# π§
|
| 765 |
if has_red_text(target_cell):
|
| 766 |
-
print(f" π§ Replacing
|
| 767 |
|
| 768 |
-
#
|
| 769 |
if isinstance(attendance_value, list):
|
| 770 |
-
|
| 771 |
-
formatted_attendance = '\n'.join(str(item).strip() for item in attendance_value if str(item).strip())
|
| 772 |
else:
|
| 773 |
-
|
| 774 |
|
| 775 |
-
print(f" π
|
|
|
|
|
|
|
| 776 |
|
| 777 |
-
# Use
|
| 778 |
-
cell_replacements =
|
| 779 |
replacements_made += cell_replacements
|
| 780 |
|
| 781 |
-
print(f" β
|
| 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
|