Spaces:
Runtime error
Runtime error
Update updated_word.py
Browse files569 unmodified lines57057157257357457557623 unmodified lines60060160260360460560610 unmodified lines6176186196206216226236246256266276286296306316326336346356366376386396406416426436446457 unmodified lines6536546556566576586596603 unmodified lines66466566666766866967067167267367467567667767867968012 unmodified lines693694695696697698699700701702703704705706707708709569 unmodified linesreturn replacements_madedef fix_management_summary_details_column(table, flat_json):"""FINAL FIX: Enhanced management summary processing that processes ALL standards correctly"""replacements_made = 0print(f" π― FIX: Management Summary DETAILS column processing")23 unmodified linesfor mgmt_type in mgmt_types:print(f" β Confirmed {mgmt_type} table processing")# FIXED: Build management data dict from flattened keysmgmt_data = {}# Look for flattened keys like "Mass Management Summary.Std 5. Verification"10 unmodified linesprint(f" π Processing {mgmt_type} with standards: {list(mgmt_data.keys())}")# Process the table rows - FIXED: Better row processingfor row_idx, row in enumerate(table.rows):if len(row.cells) >= 2:standard_cell = row.cells[0]details_cell = row.cells[1]standard_text = get_clean_text(standard_cell).strip()standard_text_lower = standard_text.lower()# Skip header rowsif any(header in standard_text_lower for header in ["standard", "requirement", "details", "management"]):continue# Only process cells with red text in details columnif not has_red_text(details_cell):continueprint(f" π Processing row {row_idx + 1}: '{standard_text}'")# FIXED: Comprehensive standard matchingreplacement_value = Nonematched_std = None# Strategy 1: Extract standard number and matchstd_match = re.search(r'std\s*(\d+)', standard_text_lower)if std_match:std_num = std_match.group(1)7 unmodified linesprint(f" β Found match by std number: '{std_key}'")break# Strategy 2: Keyword-based matching if std number doesn't workif not replacement_value:if "daily" in standard_text_lower and "check" in standard_text_lower:replacement_value = find_best_standard_value(mgmt_data, ["Std 1. Daily Check", "Daily Check"])matched_std = "Daily Check related"3 unmodified lineselif "internal review" in standard_text_lower:replacement_value = find_best_standard_value(mgmt_data, ["Std 6. Internal Review", "Std 7. Internal Review", "Std 5. Internal Review", "Internal Review"])matched_std = "Internal Review related"elif "fault" in standard_text_lower and "recording" in standard_text_lower:replacement_value = find_best_standard_value(mgmt_data, ["Std 2. Fault Recording", "Fault Recording"])matched_std = "Fault Recording related"elif "fault" in standard_text_lower and "repair" in standard_text_lower:replacement_value = find_best_standard_value(mgmt_data, ["Std 3. Fault Repair", "Fault Repair"])matched_std = "Fault Repair related"# Strategy 3: Try all available standards if nothing specific matchesif not replacement_value and mgmt_data:print(f" π No specific match, trying all available standards...")# Just take the first available standard for this rowfor std_key, std_value in mgmt_data.items():replacement_value = std_valuematched_std = std_key12 unmodified linesreplacement_text = str(replacement_value)print(f" π― About to replace red text with: '{replacement_text[:100]}...'")cell_replacements = replace_red_text_in_cell(details_cell, replacement_text)replacements_made += cell_replacementsif cell_replacements > 0:print(f" β SUCCESSFULLY replaced '{standard_text}' details in {mgmt_type}")print(f" π Used data from: '{matched_std}'")else:print(f" β Failed to replace red text in cell")else:print(f" β οΈ No replacement found for '{standard_text}' in {mgmt_type}")print(f" π Available standards: {list(mgmt_data.keys())}")print(f" π Total management summary replacements: {replacements_made}")return replacements_made569 unmodified lines57057157257357457557623 unmodified lines60060160260360460560610 unmodified lines6176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536547 unmodified lines6626636646656666676686696703 unmodified lines67467567667767867968068168268312 unmodified lines696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726569 unmodified linesreturn replacements_madedef fix_management_summary_details_column(table, flat_json):"""DEBUG VERSION: Enhanced management summary processing with detailed debugging - FIXED FOR FLATTENED JSON"""replacements_made = 0print(f" π― FIX: Management Summary DETAILS column processing")23 unmodified linesfor mgmt_type in mgmt_types:print(f" β Confirmed {mgmt_type} table processing")# Build management data dict from flattened keysmgmt_data = {}# Look for flattened keys like "Mass Management Summary.Std 5. Verification"10 unmodified linesprint(f" π Processing {mgmt_type} with standards: {list(mgmt_data.keys())}")# DEBUG: Check every row in the tableprint(f" π Analyzing all {len(table.rows)} rows in table:")for row_idx, row in enumerate(table.rows):if len(row.cells) >= 2:standard_cell = row.cells[0]details_cell = row.cells[1]standard_text = get_clean_text(standard_cell).strip()details_text = get_clean_text(details_cell).strip()standard_text_lower = standard_text.lower()print(f" π Row {row_idx + 1}:")print(f" π Standard: '{standard_text}'")print(f" π Details: '{details_text[:50]}...' (length: {len(details_text)})")print(f" π΄ Has red text: {has_red_text(details_cell)}")# Skip header rowsif any(header in standard_text_lower for header in ["standard", "requirement", "details", "management"]):print(f" βοΈ Skipping header row")continue# Check if this row has red textif not has_red_text(details_cell):print(f" βοΈ No red text found, skipping")continueprint(f" π― PROCESSING row {row_idx + 1}: '{standard_text}'")# Extract standard number and matchreplacement_value = Nonematched_std = Nonestd_match = re.search(r'std\s*(\d+)', standard_text_lower)if std_match:std_num = std_match.group(1)7 unmodified linesprint(f" β Found match by std number: '{std_key}'")break# Keyword-based matching if std number doesn't workif not replacement_value:print(f" π No std number match, trying keyword matching...")if "daily" in standard_text_lower and "check" in standard_text_lower:replacement_value = find_best_standard_value(mgmt_data, ["Std 1. Daily Check", "Daily Check"])matched_std = "Daily Check related"3 unmodified lineselif "internal review" in standard_text_lower:replacement_value = find_best_standard_value(mgmt_data, ["Std 6. Internal Review", "Std 7. Internal Review", "Std 5. Internal Review", "Internal Review"])matched_std = "Internal Review related"# Last resort: use any available standardif not replacement_value and mgmt_data:print(f" π No specific match, using first available standard...")for std_key, std_value in mgmt_data.items():replacement_value = std_valuematched_std = std_key12 unmodified linesreplacement_text = str(replacement_value)print(f" π― About to replace red text with: '{replacement_text[:100]}...'")# DEBUG: Show red text segments before replacementred_segments = extract_red_text_segments(details_cell)print(f" π Found {len(red_segments)} red text segments:")for i, segment in enumerate(red_segments):print(f" Segment {i+1}: '{segment['text'][:50]}...'")cell_replacements = replace_red_text_in_cell(details_cell, replacement_text)replacements_made += cell_replacementsif cell_replacements > 0:print(f" β SUCCESSFULLY replaced '{standard_text}' details in {mgmt_type}")print(f" π Used data from: '{matched_std}'")# Verify the replacement workednew_details_text = get_clean_text(details_cell).strip()print(f" π New details text: '{new_details_text[:100]}...'")else:print(f" β Failed to replace red text in cell")print(f" π Cell still contains: '{get_clean_text(details_cell)[:100]}...'")else:print(f" β οΈ No replacement found for '{standard_text}' in {mgmt_type}")print(f" π Available standards: {list(mgmt_data.keys())}")else:print(f" β οΈ Row {row_idx + 1} has insufficient columns ({len(row.cells)})")print(f" π Total management summary replacements: {replacements_made}")return replacements_made
updated_word.py+34-17