Spaces:
Running
Running
Update updated_word.py
Browse files- updated_word.py +52 -71
updated_word.py
CHANGED
|
@@ -705,120 +705,101 @@ def handle_attendance_list_table_enhanced(table, flat_json):
|
|
| 705 |
if found_attendance_row is None:
|
| 706 |
return 0
|
| 707 |
|
| 708 |
-
# π§
|
| 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
|
| 728 |
-
|
| 729 |
-
|
| 730 |
-
|
|
|
|
|
|
|
| 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 |
-
#
|
| 748 |
-
|
| 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 |
-
#
|
| 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 |
-
|
| 785 |
-
|
| 786 |
-
|
| 787 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 788 |
break
|
| 789 |
|
| 790 |
-
# If no red text found, use the
|
| 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
|
| 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,
|
| 803 |
current_text = get_clean_text(target_cell).strip()
|
| 804 |
-
print(f" π
|
| 805 |
|
| 806 |
-
if
|
| 807 |
-
|
|
|
|
| 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" β
|
| 820 |
-
else:
|
| 821 |
-
print(f" β οΈ Cell has content but no red text - manual check needed")
|
| 822 |
|
| 823 |
return replacements_made
|
| 824 |
|
|
|
|
| 705 |
if found_attendance_row is None:
|
| 706 |
return 0
|
| 707 |
|
| 708 |
+
# π§ FIX: Look for attendance data in JSON
|
| 709 |
attendance_value = None
|
| 710 |
+
attendance_search_keys = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 711 |
"Attendance List (Names and Position Titles).Attendance List (Names and Position Titles)",
|
| 712 |
"Attendance List (Names and Position Titles)",
|
|
|
|
|
|
|
| 713 |
"attendance list",
|
| 714 |
"attendees"
|
| 715 |
]
|
| 716 |
|
| 717 |
+
print(f" π Searching for attendance data in JSON...")
|
| 718 |
+
|
| 719 |
+
for search_key in attendance_search_keys:
|
| 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 |
print(f" π Raw value: {attendance_value}")
|
| 724 |
break
|
| 725 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 726 |
if attendance_value is None:
|
| 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 |
+
formatted_attendance = '\n'.join(str(item) for item in attendance_value if str(item).strip())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 733 |
else:
|
| 734 |
formatted_attendance = str(attendance_value)
|
| 735 |
|
| 736 |
print(f" π Final formatted attendance:\n{formatted_attendance}")
|
| 737 |
|
| 738 |
+
# π§ CRITICAL FIX: Look for red text in ALL cells of the table, not just the header
|
|
|
|
|
|
|
|
|
|
| 739 |
target_cell = None
|
| 740 |
+
target_row_idx = None
|
| 741 |
+
target_cell_idx = None
|
| 742 |
+
|
| 743 |
+
print(f" π Scanning ALL cells in attendance table for red text...")
|
| 744 |
+
|
| 745 |
+
for row_idx, row in enumerate(table.rows):
|
| 746 |
+
for cell_idx, cell in enumerate(row.cells):
|
| 747 |
+
if has_red_text(cell):
|
| 748 |
+
print(f" π― Found red text in row {row_idx + 1}, cell {cell_idx + 1}")
|
| 749 |
+
|
| 750 |
+
# Get the red text to see if it looks like attendance data
|
| 751 |
+
red_text = ""
|
| 752 |
+
for paragraph in cell.paragraphs:
|
| 753 |
+
for run in paragraph.runs:
|
| 754 |
+
if is_red(run):
|
| 755 |
+
red_text += run.text
|
| 756 |
+
|
| 757 |
+
print(f" π Red text content: '{red_text[:50]}...'")
|
| 758 |
+
|
| 759 |
+
# Check if this red text looks like attendance data (contains names/manager/etc)
|
| 760 |
+
red_text_lower = red_text.lower()
|
| 761 |
+
if any(indicator in red_text_lower for indicator in ['manager', 'herbig', 'palin', 'β', '-']):
|
| 762 |
+
target_cell = cell
|
| 763 |
+
target_row_idx = row_idx
|
| 764 |
+
target_cell_idx = cell_idx
|
| 765 |
+
print(f" β
This looks like attendance data - using this cell")
|
| 766 |
+
break
|
| 767 |
+
|
| 768 |
+
if target_cell is not None:
|
| 769 |
break
|
| 770 |
|
| 771 |
+
# If no red text found that looks like attendance data, use the original approach
|
| 772 |
if target_cell is None:
|
| 773 |
+
print(f" β οΈ No red text found that looks like attendance data, using header row approach")
|
| 774 |
+
target_row = table.rows[found_attendance_row]
|
| 775 |
target_cell = target_row.cells[found_attendance_cell]
|
|
|
|
| 776 |
|
| 777 |
+
# Replace the content
|
| 778 |
if has_red_text(target_cell):
|
| 779 |
+
print(f" π§ Replacing red text in target cell...")
|
| 780 |
cell_replacements = replace_red_text_in_cell(target_cell, formatted_attendance)
|
| 781 |
replacements_made += cell_replacements
|
| 782 |
print(f" β
Replaced red text in attendance list")
|
| 783 |
print(f" π Replacements made: {cell_replacements}")
|
| 784 |
else:
|
| 785 |
+
# If no red text, try to replace content anyway
|
| 786 |
current_text = get_clean_text(target_cell).strip()
|
| 787 |
+
print(f" π No red text found, current cell text: '{current_text[:50]}...'")
|
| 788 |
|
| 789 |
+
if len(current_text) > 50: # If it contains what looks like attendance data
|
| 790 |
+
print(f" π§ Replacing entire cell content...")
|
| 791 |
+
# Clear and replace
|
| 792 |
for paragraph in target_cell.paragraphs:
|
| 793 |
for run in paragraph.runs:
|
| 794 |
run.text = ''
|
| 795 |
|
|
|
|
| 796 |
if target_cell.paragraphs:
|
| 797 |
target_cell.paragraphs[0].text = formatted_attendance
|
| 798 |
else:
|
| 799 |
target_cell.text = formatted_attendance
|
| 800 |
|
| 801 |
replacements_made += 1
|
| 802 |
+
print(f" β
Replaced entire cell content")
|
|
|
|
|
|
|
| 803 |
|
| 804 |
return replacements_made
|
| 805 |
|