Spaces:
Running
Running
Update updated_word.py
Browse files- updated_word.py +87 -61
updated_word.py
CHANGED
|
@@ -705,94 +705,120 @@ def handle_attendance_list_table_enhanced(table, flat_json):
|
|
| 705 |
if found_attendance_row is None:
|
| 706 |
return 0
|
| 707 |
|
| 708 |
-
# π§ FIX:
|
| 709 |
attendance_value = None
|
| 710 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 711 |
"Attendance List (Names and Position Titles)",
|
|
|
|
|
|
|
| 712 |
"attendance list",
|
| 713 |
-
"attendees"
|
| 714 |
-
"names and position titles"
|
| 715 |
]
|
| 716 |
|
| 717 |
-
|
| 718 |
-
|
| 719 |
-
|
| 720 |
-
|
| 721 |
-
|
| 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 |
-
#
|
| 728 |
-
if
|
| 729 |
-
|
| 730 |
-
|
| 731 |
-
|
| 732 |
-
|
| 733 |
-
print(f"
|
| 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 |
-
#
|
| 756 |
-
|
| 757 |
-
|
| 758 |
-
|
| 759 |
-
|
| 760 |
-
|
| 761 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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" π
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 783 |
|
| 784 |
# Replace red text or entire cell content if needed
|
| 785 |
if has_red_text(target_cell):
|
| 786 |
cell_replacements = replace_red_text_in_cell(target_cell, formatted_attendance)
|
| 787 |
replacements_made += cell_replacements
|
| 788 |
-
print(f" β
Replaced red text in attendance list
|
|
|
|
| 789 |
else:
|
| 790 |
# If no red text, check if cell is mostly empty and fill it
|
| 791 |
current_text = get_clean_text(target_cell).strip()
|
|
|
|
|
|
|
| 792 |
if not current_text or len(current_text) < 20: # Likely placeholder
|
| 793 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 794 |
replacements_made += 1
|
| 795 |
-
print(f" β
Filled
|
|
|
|
|
|
|
| 796 |
|
| 797 |
return replacements_made
|
| 798 |
|
|
|
|
| 705 |
if found_attendance_row is None:
|
| 706 |
return 0
|
| 707 |
|
| 708 |
+
# π§ SPECIFIC FIX: Look for your exact JSON structure
|
| 709 |
attendance_value = None
|
| 710 |
+
|
| 711 |
+
# Debug: Print all keys to see what we have
|
| 712 |
+
print(f" π DEBUG: All available keys:")
|
| 713 |
+
for key in sorted(flat_json.keys()):
|
| 714 |
+
if 'attendance' in key.lower():
|
| 715 |
+
print(f" - '{key}': {flat_json[key]}")
|
| 716 |
+
|
| 717 |
+
# Try multiple variations of the key
|
| 718 |
+
possible_keys = [
|
| 719 |
+
"Attendance List (Names and Position Titles).Attendance List (Names and Position Titles)",
|
| 720 |
"Attendance List (Names and Position Titles)",
|
| 721 |
+
"attendance list (names and position titles).attendance list (names and position titles)",
|
| 722 |
+
"attendance list (names and position titles)",
|
| 723 |
"attendance list",
|
| 724 |
+
"attendees"
|
|
|
|
| 725 |
]
|
| 726 |
|
| 727 |
+
for key in possible_keys:
|
| 728 |
+
if key in flat_json:
|
| 729 |
+
attendance_value = flat_json[key]
|
| 730 |
+
print(f" β
Found attendance data with exact key: '{key}'")
|
| 731 |
+
print(f" π Raw value: {attendance_value}")
|
|
|
|
|
|
|
|
|
|
| 732 |
break
|
| 733 |
|
| 734 |
+
# If still not found, try case-insensitive search
|
| 735 |
+
if attendance_value is None:
|
| 736 |
+
for key, value in flat_json.items():
|
| 737 |
+
if 'attendance' in key.lower() and 'names' in key.lower():
|
| 738 |
+
attendance_value = value
|
| 739 |
+
print(f" β
Found attendance data with case-insensitive key: '{key}'")
|
| 740 |
+
print(f" π Raw value: {attendance_value}")
|
|
|
|
| 741 |
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 742 |
|
| 743 |
if attendance_value is None:
|
| 744 |
print(f" β No attendance data found in JSON")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 745 |
return 0
|
| 746 |
|
| 747 |
+
# π§ SPECIFIC FIX: Handle your exact data format
|
| 748 |
+
print(f" π Processing attendance value type: {type(attendance_value)}")
|
| 749 |
+
|
| 750 |
+
if isinstance(attendance_value, list) and len(attendance_value) > 0:
|
| 751 |
+
# Get the first item from the list
|
| 752 |
+
first_item = attendance_value[0]
|
| 753 |
+
print(f" π First item: '{first_item}'")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 754 |
|
| 755 |
+
# Your data is "Liam Herbig β Manager Deborah Herbig β Manager"
|
| 756 |
+
# Split this into separate people
|
| 757 |
+
if isinstance(first_item, str):
|
| 758 |
+
# Split by Manager and reconstruct
|
| 759 |
+
parts = first_item.split('Manager')
|
| 760 |
+
formatted_people = []
|
| 761 |
+
|
| 762 |
+
for part in parts:
|
| 763 |
+
part = part.strip()
|
| 764 |
+
if part:
|
| 765 |
+
# Remove any trailing/leading dashes
|
| 766 |
+
part = part.strip('β').strip('-').strip()
|
| 767 |
+
if part:
|
| 768 |
+
formatted_people.append(f"{part.strip()} - Manager")
|
| 769 |
+
|
| 770 |
+
# Join with newlines
|
| 771 |
+
formatted_attendance = '\n'.join(formatted_people)
|
| 772 |
+
else:
|
| 773 |
+
formatted_attendance = str(first_item)
|
| 774 |
else:
|
| 775 |
formatted_attendance = str(attendance_value)
|
|
|
|
|
|
|
|
|
|
| 776 |
|
| 777 |
+
print(f" π Final formatted attendance:\n{formatted_attendance}")
|
| 778 |
+
|
| 779 |
+
# Process the attendance cell - look in ALL cells of the found row
|
| 780 |
+
target_row = table.rows[found_attendance_row]
|
| 781 |
+
|
| 782 |
+
# Try to find a cell with red text in this row
|
| 783 |
+
target_cell = None
|
| 784 |
+
for cell in target_row.cells:
|
| 785 |
+
if has_red_text(cell):
|
| 786 |
+
target_cell = cell
|
| 787 |
+
print(f" π― Found red text in attendance row")
|
| 788 |
+
break
|
| 789 |
+
|
| 790 |
+
# If no red text found, use the cell we originally found
|
| 791 |
+
if target_cell is None:
|
| 792 |
+
target_cell = target_row.cells[found_attendance_cell]
|
| 793 |
+
print(f" π Using original cell for attendance")
|
| 794 |
|
| 795 |
# Replace red text or entire cell content if needed
|
| 796 |
if has_red_text(target_cell):
|
| 797 |
cell_replacements = replace_red_text_in_cell(target_cell, formatted_attendance)
|
| 798 |
replacements_made += cell_replacements
|
| 799 |
+
print(f" β
Replaced red text in attendance list")
|
| 800 |
+
print(f" π Replacements made: {cell_replacements}")
|
| 801 |
else:
|
| 802 |
# If no red text, check if cell is mostly empty and fill it
|
| 803 |
current_text = get_clean_text(target_cell).strip()
|
| 804 |
+
print(f" π Current cell text: '{current_text}'")
|
| 805 |
+
|
| 806 |
if not current_text or len(current_text) < 20: # Likely placeholder
|
| 807 |
+
# Clear all existing content and set new text
|
| 808 |
+
for paragraph in target_cell.paragraphs:
|
| 809 |
+
for run in paragraph.runs:
|
| 810 |
+
run.text = ''
|
| 811 |
+
|
| 812 |
+
# Add new content
|
| 813 |
+
if target_cell.paragraphs:
|
| 814 |
+
target_cell.paragraphs[0].text = formatted_attendance
|
| 815 |
+
else:
|
| 816 |
+
target_cell.text = formatted_attendance
|
| 817 |
+
|
| 818 |
replacements_made += 1
|
| 819 |
+
print(f" β
Filled attendance cell with new content")
|
| 820 |
+
else:
|
| 821 |
+
print(f" β οΈ Cell has content but no red text - manual check needed")
|
| 822 |
|
| 823 |
return replacements_made
|
| 824 |
|