Spaces:
Runtime error
Runtime error
Update updated_word.py
Browse files704 unmodified lines705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798704 unmodified linesif found_attendance_row is None:return 0# π§ FIX: Enhanced search for attendance data in JSON with nested structure handlingattendance_value = Noneattendance_search_keys = ["Attendance List (Names and Position Titles)","attendance list","attendees","names and position titles"]print(f" π Searching for attendance data in JSON...")for search_key in attendance_search_keys:# Try exact match firstattendance_value = find_matching_json_value(search_key, flat_json)if attendance_value is not None:print(f" β Found attendance data with key: '{search_key}'")print(f" π Data type: {type(attendance_value)}")break# π§ FIX: Handle nested structure - if we get a dict, try to extract the inner arrayif isinstance(attendance_value, dict):print(f" π§ Attendance value is dict, looking for nested array...")# Look for the same key inside the dict (nested structure)for inner_key, inner_value in attendance_value.items():if isinstance(inner_value, list):print(f" β Found nested list with key: '{inner_key}'")attendance_value = inner_valuebreak# If still a dict, try to get any list valueif isinstance(attendance_value, dict):for inner_value in attendance_value.values():if isinstance(inner_value, list):attendance_value = inner_valuebreakif attendance_value is None:print(f" β No attendance data found in JSON")# π§ FIX: Additional debug - let's see what keys we have that might matchprint(f" π Available keys containing 'attendance':")for key in flat_json.keys():if 'attendance' in key.lower():print(f" - {key}: {flat_json[key]}")return 0# Process the attendance celltarget_row = table.rows[found_attendance_row]target_cell = target_row.cells[found_attendance_cell]# π§ FIX: Better formatting of attendance dataif isinstance(attendance_value, list):# Handle each item in the listformatted_items = []for item in attendance_value:if isinstance(item, str):# Split on common separators and format each person on new lineif 'β' in item or '-' in item:# Split by β or - and clean uppeople = item.replace('β', '\n').replace('-', '\n').strip()formatted_items.append(people)else:formatted_items.append(str(item).strip())else:formatted_items.append(str(item).strip())formatted_attendance = '\n'.join(formatted_items)else:formatted_attendance = str(attendance_value)# Handle embedded separatorsif 'β' in formatted_attendance or '-' in formatted_attendance:formatted_attendance = formatted_attendance.replace('β', '\n').replace('-', '\n')print(f" π Formatted attendance data:\n{formatted_attendance}")# Replace red text or entire cell content if neededif has_red_text(target_cell):cell_replacements = replace_red_text_in_cell(target_cell, formatted_attendance)replacements_made += cell_replacementsprint(f" β Replaced red text in attendance list with: '{formatted_attendance[:50]}...'")else:# If no red text, check if cell is mostly empty and fill itcurrent_text = get_clean_text(target_cell).strip()if not current_text or len(current_text) < 20: # Likely placeholdertarget_cell.text = formatted_attendancereplacements_made += 1print(f" β Filled empty attendance cell with: '{formatted_attendance[:50]}...'")return replacements_made704 unmodified lines705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824704 unmodified linesif found_attendance_row is None:return 0# π§ SPECIFIC FIX: Look for your exact JSON structureattendance_value = None# Debug: Print all keys to see what we haveprint(f" π DEBUG: All available keys:")for key in sorted(flat_json.keys()):if 'attendance' in key.lower():print(f" - '{key}': {flat_json[key]}")# Try multiple variations of the keypossible_keys = ["Attendance List (Names and Position Titles).Attendance List (Names and Position Titles)","Attendance List (Names and Position Titles)","attendance list (names and position titles).attendance list (names and position titles)","attendance list (names and position titles)","attendance list","attendees"]for key in possible_keys:if key in flat_json:attendance_value = flat_json[key]print(f" β Found attendance data with exact key: '{key}'")print(f" π Raw value: {attendance_value}")break# If still not found, try case-insensitive searchif attendance_value is None:for key, value in flat_json.items():if 'attendance' in key.lower() and 'names' in key.lower():attendance_value = valueprint(f" β Found attendance data with case-insensitive key: '{key}'")print(f" π Raw value: {attendance_value}")breakif attendance_value is None:print(f" β No attendance data found in JSON")return 0# π§ SPECIFIC FIX: Handle your exact data formatprint(f" π Processing attendance value type: {type(attendance_value)}")if isinstance(attendance_value, list) and len(attendance_value) > 0:# Get the first item from the listfirst_item = attendance_value[0]print(f" π First item: '{first_item}'")# Your data is "Liam Herbig β Manager Deborah Herbig β Manager"# Split this into separate peopleif isinstance(first_item, str):# Split by Manager and reconstructparts = first_item.split('Manager')formatted_people = []for part in parts:part = part.strip()if part:# Remove any trailing/leading dashespart = part.strip('β').strip('-').strip()if part:formatted_people.append(f"{part.strip()} - Manager")# Join with newlinesformatted_attendance = '\n'.join(formatted_people)else:formatted_attendance = str(first_item)else:formatted_attendance = str(attendance_value)print(f" π Final formatted attendance:\n{formatted_attendance}")# Process the attendance cell - look in ALL cells of the found rowtarget_row = table.rows[found_attendance_row]# Try to find a cell with red text in this rowtarget_cell = Nonefor cell in target_row.cells:if has_red_text(cell):target_cell = cellprint(f" π― Found red text in attendance row")break# If no red text found, use the cell we originally foundif target_cell is None:target_cell = target_row.cells[found_attendance_cell]print(f" π Using original cell for attendance")# Replace red text or entire cell content if neededif has_red_text(target_cell):cell_replacements = replace_red_text_in_cell(target_cell, formatted_attendance)replacements_made += cell_replacementsprint(f" β Replaced red text in attendance list")print(f" π Replacements made: {cell_replacements}")else:# If no red text, check if cell is mostly empty and fill itcurrent_text = get_clean_text(target_cell).strip()print(f" π Current cell text: '{current_text}'")if not current_text or len(current_text) < 20: # Likely placeholder# Clear all existing content and set new textfor paragraph in target_cell.paragraphs:for run in paragraph.runs:run.text = ''# Add new contentif target_cell.paragraphs:target_cell.paragraphs[0].text = formatted_attendanceelse:target_cell.text = formatted_attendancereplacements_made += 1print(f" β Filled attendance cell with new content")else:print(f" β οΈ Cell has content but no red text - manual check needed")return replacements_made
updated_word.py+87-61