Spaces:
Running
Running
Update updated_word.py
Browse files- updated_word.py +170 -2
updated_word.py
CHANGED
|
@@ -949,6 +949,168 @@ def handle_management_summary_fix(cell, flat_json):
|
|
| 949 |
|
| 950 |
return replacements_made
|
| 951 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 952 |
def process_tables(document, flat_json):
|
| 953 |
"""Your original function with ALL surgical fixes added"""
|
| 954 |
replacements_made = 0
|
|
@@ -969,6 +1131,8 @@ def process_tables(document, flat_json):
|
|
| 969 |
|
| 970 |
if has_management and has_details:
|
| 971 |
print(f" π Detected Management Summary table")
|
|
|
|
|
|
|
| 972 |
# Process each cell in the table to find red text and apply the existing fix
|
| 973 |
summary_replacements = 0
|
| 974 |
for row_idx, row in enumerate(table.rows):
|
|
@@ -1115,8 +1279,12 @@ def process_tables(document, flat_json):
|
|
| 1115 |
if len(table.rows) <= 4: # Only small tables
|
| 1116 |
declaration_fix = handle_operator_declaration_fix(table, flat_json)
|
| 1117 |
replacements_made += declaration_fix
|
| 1118 |
-
|
| 1119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1120 |
|
| 1121 |
def process_paragraphs(document, flat_json):
|
| 1122 |
"""Your original function (unchanged)"""
|
|
|
|
| 949 |
|
| 950 |
return replacements_made
|
| 951 |
|
| 952 |
+
def fix_management_summary_details_column(table, flat_json):
|
| 953 |
+
"""Fix the DETAILS column in Management Summary table"""
|
| 954 |
+
replacements_made = 0
|
| 955 |
+
|
| 956 |
+
print(f" π― FIX 1: Management Summary DETAILS column processing")
|
| 957 |
+
|
| 958 |
+
# Check if this is a Management Summary table
|
| 959 |
+
table_text = ""
|
| 960 |
+
for row in table.rows[:2]:
|
| 961 |
+
for cell in row.cells:
|
| 962 |
+
table_text += get_clean_text(cell).lower() + " "
|
| 963 |
+
|
| 964 |
+
if not ("mass management" in table_text and "details" in table_text):
|
| 965 |
+
return 0
|
| 966 |
+
|
| 967 |
+
print(f" β
Confirmed Mass Management Summary table")
|
| 968 |
+
|
| 969 |
+
# Process each row looking for Std 5. and Std 6. with red text
|
| 970 |
+
for row_idx, row in enumerate(table.rows):
|
| 971 |
+
if len(row.cells) >= 2:
|
| 972 |
+
standard_cell = row.cells[0]
|
| 973 |
+
details_cell = row.cells[1]
|
| 974 |
+
|
| 975 |
+
standard_text = get_clean_text(standard_cell).strip()
|
| 976 |
+
|
| 977 |
+
# Look for Std 5. Verification and Std 6. Internal Review specifically
|
| 978 |
+
if "Std 5." in standard_text and "Verification" in standard_text:
|
| 979 |
+
if has_red_text(details_cell):
|
| 980 |
+
print(f" π Found Std 5. Verification with red text")
|
| 981 |
+
|
| 982 |
+
# Use the exact data from your JSON
|
| 983 |
+
json_value = find_matching_json_value("Std 5. Verification", flat_json)
|
| 984 |
+
if json_value is not None:
|
| 985 |
+
replacement_text = get_value_as_string(json_value, "Std 5. Verification")
|
| 986 |
+
cell_replacements = replace_red_text_in_cell(details_cell, replacement_text)
|
| 987 |
+
replacements_made += cell_replacements
|
| 988 |
+
print(f" β
Replaced Std 5. Verification details")
|
| 989 |
+
|
| 990 |
+
elif "Std 6." in standard_text and "Internal Review" in standard_text:
|
| 991 |
+
if has_red_text(details_cell):
|
| 992 |
+
print(f" π Found Std 6. Internal Review with red text")
|
| 993 |
+
|
| 994 |
+
# Use the exact data from your JSON
|
| 995 |
+
json_value = find_matching_json_value("Std 6. Internal Review", flat_json)
|
| 996 |
+
if json_value is not None:
|
| 997 |
+
replacement_text = get_value_as_string(json_value, "Std 6. Internal Review")
|
| 998 |
+
cell_replacements = replace_red_text_in_cell(details_cell, replacement_text)
|
| 999 |
+
replacements_made += cell_replacements
|
| 1000 |
+
print(f" β
Replaced Std 6. Internal Review details")
|
| 1001 |
+
|
| 1002 |
+
return replacements_made
|
| 1003 |
+
|
| 1004 |
+
def fix_operator_declaration_empty_values(table, flat_json):
|
| 1005 |
+
"""Fix Operator Declaration table when values are empty"""
|
| 1006 |
+
replacements_made = 0
|
| 1007 |
+
|
| 1008 |
+
print(f" π― FIX 2: Operator Declaration empty values processing")
|
| 1009 |
+
|
| 1010 |
+
# Check if this is an Operator Declaration table
|
| 1011 |
+
table_context = ""
|
| 1012 |
+
for row in table.rows:
|
| 1013 |
+
for cell in row.cells:
|
| 1014 |
+
table_context += get_clean_text(cell).lower() + " "
|
| 1015 |
+
|
| 1016 |
+
if not ("print name" in table_context and "position title" in table_context):
|
| 1017 |
+
return 0
|
| 1018 |
+
|
| 1019 |
+
print(f" β
Confirmed Operator Declaration table")
|
| 1020 |
+
|
| 1021 |
+
# Find the data row with Print Name and Position Title
|
| 1022 |
+
for row_idx, row in enumerate(table.rows):
|
| 1023 |
+
if len(row.cells) >= 2:
|
| 1024 |
+
cell1_text = get_clean_text(row.cells[0]).strip().lower()
|
| 1025 |
+
cell2_text = get_clean_text(row.cells[1]).strip().lower()
|
| 1026 |
+
|
| 1027 |
+
# Check if this is the header row
|
| 1028 |
+
if "print name" in cell1_text and "position" in cell2_text:
|
| 1029 |
+
print(f" π Found header row at {row_idx + 1}")
|
| 1030 |
+
|
| 1031 |
+
# Look for the data row (next row)
|
| 1032 |
+
if row_idx + 1 < len(table.rows):
|
| 1033 |
+
data_row = table.rows[row_idx + 1]
|
| 1034 |
+
if len(data_row.cells) >= 2:
|
| 1035 |
+
name_cell = data_row.cells[0]
|
| 1036 |
+
position_cell = data_row.cells[1]
|
| 1037 |
+
|
| 1038 |
+
# Check if cells are empty or have red text
|
| 1039 |
+
name_text = get_clean_text(name_cell).strip()
|
| 1040 |
+
position_text = get_clean_text(position_cell).strip()
|
| 1041 |
+
|
| 1042 |
+
print(f" π Current values: Name='{name_text}', Position='{position_text}'")
|
| 1043 |
+
|
| 1044 |
+
# Fix Print Name if empty or has red text
|
| 1045 |
+
if not name_text or has_red_text(name_cell):
|
| 1046 |
+
print(f" π§ Fixing empty/red Print Name")
|
| 1047 |
+
|
| 1048 |
+
# Try multiple sources for operator name
|
| 1049 |
+
name_sources = [
|
| 1050 |
+
"Operator Declaration.Print Name",
|
| 1051 |
+
"Print Name",
|
| 1052 |
+
"Operator name (Legal entity)",
|
| 1053 |
+
"(print accreditation name)"
|
| 1054 |
+
]
|
| 1055 |
+
|
| 1056 |
+
for source in name_sources:
|
| 1057 |
+
name_value = find_matching_json_value(source, flat_json)
|
| 1058 |
+
if name_value is not None:
|
| 1059 |
+
name_replacement = get_value_as_string(name_value)
|
| 1060 |
+
if name_replacement.strip():
|
| 1061 |
+
# Extract just the name if it's a company name
|
| 1062 |
+
if "Pty Ltd" in name_replacement or "Company" in name_replacement:
|
| 1063 |
+
# Try to get individual name instead
|
| 1064 |
+
continue
|
| 1065 |
+
|
| 1066 |
+
if has_red_text(name_cell):
|
| 1067 |
+
cell_replacements = replace_red_text_in_cell(name_cell, name_replacement)
|
| 1068 |
+
else:
|
| 1069 |
+
# Cell is empty, add text directly
|
| 1070 |
+
name_cell.text = name_replacement
|
| 1071 |
+
cell_replacements = 1
|
| 1072 |
+
|
| 1073 |
+
replacements_made += cell_replacements
|
| 1074 |
+
print(f" β
Fixed Print Name with: '{name_replacement}' (from {source})")
|
| 1075 |
+
break
|
| 1076 |
+
|
| 1077 |
+
# Fix Position Title if empty or has red text
|
| 1078 |
+
if not position_text or has_red_text(position_cell):
|
| 1079 |
+
print(f" π§ Fixing empty/red Position Title")
|
| 1080 |
+
|
| 1081 |
+
# Try multiple sources for position
|
| 1082 |
+
position_sources = [
|
| 1083 |
+
"Operator Declaration.Position Title",
|
| 1084 |
+
"Position Title"
|
| 1085 |
+
]
|
| 1086 |
+
|
| 1087 |
+
for source in position_sources:
|
| 1088 |
+
position_value = find_matching_json_value(source, flat_json)
|
| 1089 |
+
if position_value is not None:
|
| 1090 |
+
position_replacement = get_value_as_string(position_value)
|
| 1091 |
+
if position_replacement.strip():
|
| 1092 |
+
|
| 1093 |
+
if has_red_text(position_cell):
|
| 1094 |
+
cell_replacements = replace_red_text_in_cell(position_cell, position_replacement)
|
| 1095 |
+
else:
|
| 1096 |
+
# Cell is empty, add text directly
|
| 1097 |
+
position_cell.text = position_replacement
|
| 1098 |
+
cell_replacements = 1
|
| 1099 |
+
|
| 1100 |
+
replacements_made += cell_replacements
|
| 1101 |
+
print(f" β
Fixed Position Title with: '{position_replacement}' (from {source})")
|
| 1102 |
+
break
|
| 1103 |
+
|
| 1104 |
+
# Fallback: use "Manager" if nothing found
|
| 1105 |
+
if not position_text and not has_red_text(position_cell):
|
| 1106 |
+
position_cell.text = "Manager"
|
| 1107 |
+
replacements_made += 1
|
| 1108 |
+
print(f" β
Used fallback Position Title: 'Manager'")
|
| 1109 |
+
|
| 1110 |
+
break # Found the table, stop looking
|
| 1111 |
+
|
| 1112 |
+
return replacements_made
|
| 1113 |
+
|
| 1114 |
def process_tables(document, flat_json):
|
| 1115 |
"""Your original function with ALL surgical fixes added"""
|
| 1116 |
replacements_made = 0
|
|
|
|
| 1131 |
|
| 1132 |
if has_management and has_details:
|
| 1133 |
print(f" π Detected Management Summary table")
|
| 1134 |
+
summary_fixes = fix_management_summary_details_column(table, flat_json)
|
| 1135 |
+
replacements_made += summary_fixes
|
| 1136 |
# Process each cell in the table to find red text and apply the existing fix
|
| 1137 |
summary_replacements = 0
|
| 1138 |
for row_idx, row in enumerate(table.rows):
|
|
|
|
| 1279 |
if len(table.rows) <= 4: # Only small tables
|
| 1280 |
declaration_fix = handle_operator_declaration_fix(table, flat_json)
|
| 1281 |
replacements_made += declaration_fix
|
| 1282 |
+
# Check for declaration tables that need fixing
|
| 1283 |
+
if "print name" in table_text and "position" in table_text:
|
| 1284 |
+
declaration_fixes = fix_operator_declaration_empty_values(table, flat_json)
|
| 1285 |
+
replacements_made += declaration_fixes
|
| 1286 |
+
|
| 1287 |
+
return replacements_made
|
| 1288 |
|
| 1289 |
def process_paragraphs(document, flat_json):
|
| 1290 |
"""Your original function (unchanged)"""
|