Spaces:
Running
Running
Update updated_word.py
Browse files- updated_word.py +49 -4
updated_word.py
CHANGED
|
@@ -673,7 +673,6 @@ def process_single_column_sections(cell, field_name, flat_json):
|
|
| 673 |
return cell_replacements
|
| 674 |
return 0
|
| 675 |
|
| 676 |
-
# π― FINAL FIX 1: Add this function to handle Attendance List (unchanged)
|
| 677 |
def handle_attendance_list_table_enhanced(table, flat_json):
|
| 678 |
"""Enhanced Attendance List processing with better detection"""
|
| 679 |
replacements_made = 0
|
|
@@ -706,7 +705,7 @@ def handle_attendance_list_table_enhanced(table, flat_json):
|
|
| 706 |
if found_attendance_row is None:
|
| 707 |
return 0
|
| 708 |
|
| 709 |
-
#
|
| 710 |
attendance_value = None
|
| 711 |
attendance_search_keys = [
|
| 712 |
"Attendance List (Names and Position Titles)",
|
|
@@ -715,26 +714,72 @@ def handle_attendance_list_table_enhanced(table, flat_json):
|
|
| 715 |
"names and position titles"
|
| 716 |
]
|
| 717 |
|
|
|
|
|
|
|
| 718 |
for search_key in attendance_search_keys:
|
| 719 |
# Try exact match first
|
| 720 |
attendance_value = find_matching_json_value(search_key, flat_json)
|
| 721 |
if attendance_value is not None:
|
| 722 |
print(f" β
Found attendance data with key: '{search_key}'")
|
|
|
|
| 723 |
break
|
| 724 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 725 |
if attendance_value is None:
|
| 726 |
print(f" β No attendance data found in JSON")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 727 |
return 0
|
| 728 |
|
| 729 |
# Process the attendance cell
|
| 730 |
target_row = table.rows[found_attendance_row]
|
| 731 |
target_cell = target_row.cells[found_attendance_cell]
|
| 732 |
|
| 733 |
-
#
|
| 734 |
if isinstance(attendance_value, list):
|
| 735 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 736 |
else:
|
| 737 |
formatted_attendance = str(attendance_value)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 738 |
|
| 739 |
# Replace red text or entire cell content if needed
|
| 740 |
if has_red_text(target_cell):
|
|
|
|
| 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
|
|
|
|
| 705 |
if found_attendance_row is None:
|
| 706 |
return 0
|
| 707 |
|
| 708 |
+
# π§ FIX: Enhanced search for attendance data in JSON with nested structure handling
|
| 709 |
attendance_value = None
|
| 710 |
attendance_search_keys = [
|
| 711 |
"Attendance List (Names and Position Titles)",
|
|
|
|
| 714 |
"names and position titles"
|
| 715 |
]
|
| 716 |
|
| 717 |
+
print(f" π Searching for attendance data in JSON...")
|
| 718 |
+
|
| 719 |
for search_key in attendance_search_keys:
|
| 720 |
# Try exact match first
|
| 721 |
attendance_value = find_matching_json_value(search_key, flat_json)
|
| 722 |
if attendance_value is not None:
|
| 723 |
print(f" β
Found attendance data with key: '{search_key}'")
|
| 724 |
+
print(f" π Data type: {type(attendance_value)}")
|
| 725 |
break
|
| 726 |
|
| 727 |
+
# π§ FIX: Handle nested structure - if we get a dict, try to extract the inner array
|
| 728 |
+
if isinstance(attendance_value, dict):
|
| 729 |
+
print(f" π§ Attendance value is dict, looking for nested array...")
|
| 730 |
+
# Look for the same key inside the dict (nested structure)
|
| 731 |
+
for inner_key, inner_value in attendance_value.items():
|
| 732 |
+
if isinstance(inner_value, list):
|
| 733 |
+
print(f" β
Found nested list with key: '{inner_key}'")
|
| 734 |
+
attendance_value = inner_value
|
| 735 |
+
break
|
| 736 |
+
|
| 737 |
+
# If still a dict, try to get any list value
|
| 738 |
+
if isinstance(attendance_value, dict):
|
| 739 |
+
for inner_value in attendance_value.values():
|
| 740 |
+
if isinstance(inner_value, list):
|
| 741 |
+
attendance_value = inner_value
|
| 742 |
+
break
|
| 743 |
+
|
| 744 |
if attendance_value is None:
|
| 745 |
print(f" β No attendance data found in JSON")
|
| 746 |
+
|
| 747 |
+
# π§ FIX: Additional debug - let's see what keys we have that might match
|
| 748 |
+
print(f" π Available keys containing 'attendance':")
|
| 749 |
+
for key in flat_json.keys():
|
| 750 |
+
if 'attendance' in key.lower():
|
| 751 |
+
print(f" - {key}: {flat_json[key]}")
|
| 752 |
+
|
| 753 |
return 0
|
| 754 |
|
| 755 |
# Process the attendance cell
|
| 756 |
target_row = table.rows[found_attendance_row]
|
| 757 |
target_cell = target_row.cells[found_attendance_cell]
|
| 758 |
|
| 759 |
+
# π§ FIX: Better formatting of attendance data
|
| 760 |
if isinstance(attendance_value, list):
|
| 761 |
+
# Handle each item in the list
|
| 762 |
+
formatted_items = []
|
| 763 |
+
for item in attendance_value:
|
| 764 |
+
if isinstance(item, str):
|
| 765 |
+
# Split on common separators and format each person on new line
|
| 766 |
+
if 'β' in item or '-' in item:
|
| 767 |
+
# Split by β or - and clean up
|
| 768 |
+
people = item.replace('β', '\n').replace('-', '\n').strip()
|
| 769 |
+
formatted_items.append(people)
|
| 770 |
+
else:
|
| 771 |
+
formatted_items.append(str(item).strip())
|
| 772 |
+
else:
|
| 773 |
+
formatted_items.append(str(item).strip())
|
| 774 |
+
|
| 775 |
+
formatted_attendance = '\n'.join(formatted_items)
|
| 776 |
else:
|
| 777 |
formatted_attendance = str(attendance_value)
|
| 778 |
+
# Handle embedded separators
|
| 779 |
+
if 'β' in formatted_attendance or '-' in formatted_attendance:
|
| 780 |
+
formatted_attendance = formatted_attendance.replace('β', '\n').replace('-', '\n')
|
| 781 |
+
|
| 782 |
+
print(f" π Formatted attendance data:\n{formatted_attendance}")
|
| 783 |
|
| 784 |
# Replace red text or entire cell content if needed
|
| 785 |
if has_red_text(target_cell):
|