Shami96 commited on
Commit
f1c869a
·
verified ·
1 Parent(s): 6c1e37b

Update updated_word.py

Browse files
Files changed (1) hide show
  1. updated_word.py +14 -10
updated_word.py CHANGED
@@ -1125,24 +1125,27 @@ def process_red_text_in_paragraph(paragraph, context_text, flat_json):
1125
  return replacements_made
1126
 
1127
  def force_red_text_replacement(document, flat_json):
1128
- """Force replacement of any remaining red text by trying ALL JSON values"""
1129
  replacements_made = 0
1130
  print(f"\n🎯 FORCE FIX: Scanning for any remaining red text...")
1131
 
1132
- # Collect ALL possible replacement values from JSON
1133
  all_values = {}
1134
  for key, value in flat_json.items():
1135
- if value and str(value).strip():
1136
- # Store both the key and variations of the value
1137
  value_str = get_value_as_string(value, key)
1138
- if value_str and value_str.strip():
1139
- all_values[key] = value_str
 
 
1140
 
1141
- # Also store individual words/parts for partial matching
1142
  if isinstance(value, list):
1143
- for item in value:
1144
- if str(item).strip():
1145
- all_values[f"{key}_item"] = str(item).strip()
 
1146
 
1147
  print(f" Found {len(all_values)} potential replacement values")
1148
 
@@ -1273,6 +1276,7 @@ def force_red_text_replacement(document, flat_json):
1273
 
1274
  return replacements_made
1275
 
 
1276
  def process_hf(json_file, docx_file, output_file):
1277
  """Your original main function with force fix added at the end"""
1278
  try:
 
1125
  return replacements_made
1126
 
1127
  def force_red_text_replacement(document, flat_json):
1128
+ """Force replacement of any remaining red text by trying ALL JSON values - FIXED"""
1129
  replacements_made = 0
1130
  print(f"\n🎯 FORCE FIX: Scanning for any remaining red text...")
1131
 
1132
+ # Collect ALL possible replacement values from JSON - FIXED to handle lists properly
1133
  all_values = {}
1134
  for key, value in flat_json.items():
1135
+ if value:
1136
+ # Convert value to string properly
1137
  value_str = get_value_as_string(value, key)
1138
+
1139
+ # Only add if we have a valid string
1140
+ if value_str and isinstance(value_str, str) and value_str.strip():
1141
+ all_values[key] = value_str.strip()
1142
 
1143
+ # Also store individual items from lists for partial matching
1144
  if isinstance(value, list):
1145
+ for i, item in enumerate(value):
1146
+ item_str = str(item).strip() if item else ""
1147
+ if item_str:
1148
+ all_values[f"{key}_item_{i}"] = item_str
1149
 
1150
  print(f" Found {len(all_values)} potential replacement values")
1151
 
 
1276
 
1277
  return replacements_made
1278
 
1279
+
1280
  def process_hf(json_file, docx_file, output_file):
1281
  """Your original main function with force fix added at the end"""
1282
  try: