Maheen001 commited on
Commit
143de80
·
verified ·
1 Parent(s): f1869ef

Update tools/form_filler_server.py

Browse files
Files changed (1) hide show
  1. tools/form_filler_server.py +74 -52
tools/form_filler_server.py CHANGED
@@ -29,21 +29,32 @@ async def fill_form(template_path: str, data: Dict[str, Any]) -> dict:
29
  return await fill_text_form(template_path, data, output_path)
30
 
31
  except Exception as e:
32
- return {'error': str(e), 'success': False}
 
33
 
34
 
35
  async def fill_docx_form(template_path: str, data: Dict[str, Any], output_path: str) -> dict:
36
- """Fill DOCX form template - Enhanced to handle table-based forms"""
37
  try:
38
  doc = Document(template_path)
39
  fields_filled = []
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
  # First, try to replace placeholders in paragraphs
42
  for paragraph in doc.paragraphs:
43
- original_text = paragraph.text
44
-
45
- for key, value in data.items():
46
- # Try multiple placeholder formats
47
  placeholders = [
48
  f"{{{{{key}}}}}", # {{field_name}}
49
  f"{{{key}}}", # {field_name}
@@ -55,60 +66,68 @@ async def fill_docx_form(template_path: str, data: Dict[str, Any], output_path:
55
  if key not in fields_filled:
56
  fields_filled.append(key)
57
 
58
- # Enhanced table processing - match field labels to values
59
- for table in doc.tables:
60
- for row in table.rows:
61
- cells = row.cells
62
 
63
  # Process each cell
64
- for cell_idx, cell in enumerate(cells):
65
- cell_text = cell.text.strip()
66
 
67
- # Check if this cell contains a field label
68
- for key, value in data.items():
69
- # Create variations of the key to match
70
- key_variations = [
71
- key.lower(),
72
- key.replace("_", " ").lower(),
73
- key.replace("_", " ").title(),
74
- key.title(),
75
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
 
77
- # If cell text matches a field label
78
- for key_var in key_variations:
79
- if key_var in cell_text.lower():
80
- # Try to fill the next cell (common form pattern)
81
- if cell_idx + 1 < len(cells):
82
- next_cell = cells[cell_idx + 1]
83
- # Only fill if the next cell is empty or has placeholder
84
- if not next_cell.text.strip() or next_cell.text.strip() in ["", "_____", "..."]:
85
- next_cell.text = str(value)
86
- if key not in fields_filled:
87
- fields_filled.append(key)
88
- break
89
-
90
- # Also check if there's text after the label in same cell
91
- # Pattern: "First Name: _____" or "First Name:"
92
- if ":" in cell_text:
93
- # Replace everything after the colon
94
- label_part = cell_text.split(":")[0] + ":"
95
- cell.text = f"{label_part} {value}"
96
- if key not in fields_filled:
97
- fields_filled.append(key)
98
- break
99
 
100
- # Also check for placeholder patterns in the cell
101
- for key, value in data.items():
102
  placeholders = [
103
  f"{{{{{key}}}}}",
104
  f"{{{key}}}",
105
  ]
106
 
107
  for placeholder in placeholders:
108
- if placeholder in cell.text:
109
  cell.text = cell.text.replace(placeholder, str(value))
110
  if key not in fields_filled:
111
  fields_filled.append(key)
 
112
 
113
  # Ensure output directory exists
114
  Path(output_path).parent.mkdir(parents=True, exist_ok=True)
@@ -117,8 +136,9 @@ async def fill_docx_form(template_path: str, data: Dict[str, Any], output_path:
117
  return {
118
  'success': True,
119
  'output_path': output_path,
120
- 'fields_filled': fields_filled,
121
- 'total_fields': len(fields_filled)
 
122
  }
123
 
124
  except Exception as e:
@@ -141,7 +161,6 @@ async def fill_excel_form(template_path: str, data: Dict[str, Any], output_path:
141
  placeholders = [
142
  f"{{{{{key}}}}}",
143
  f"{{{key}}}",
144
- key
145
  ]
146
 
147
  for placeholder in placeholders:
@@ -162,7 +181,8 @@ async def fill_excel_form(template_path: str, data: Dict[str, Any], output_path:
162
  }
163
 
164
  except Exception as e:
165
- return {'error': str(e), 'success': False}
 
166
 
167
 
168
  async def fill_text_form(template_path: str, data: Dict[str, Any], output_path: str) -> dict:
@@ -198,7 +218,8 @@ async def fill_text_form(template_path: str, data: Dict[str, Any], output_path:
198
  }
199
 
200
  except Exception as e:
201
- return {'error': str(e), 'success': False}
 
202
 
203
 
204
  async def generate_form_template(fields: list) -> dict:
@@ -224,4 +245,5 @@ async def generate_form_template(fields: list) -> dict:
224
  }
225
 
226
  except Exception as e:
227
- return {'error': str(e), 'success': False}
 
 
29
  return await fill_text_form(template_path, data, output_path)
30
 
31
  except Exception as e:
32
+ import traceback
33
+ return {'error': f"{str(e)}\n\n{traceback.format_exc()}", 'success': False}
34
 
35
 
36
  async def fill_docx_form(template_path: str, data: Dict[str, Any], output_path: str) -> dict:
37
+ """Fill DOCX form template - Enhanced with comprehensive matching"""
38
  try:
39
  doc = Document(template_path)
40
  fields_filled = []
41
+ debug_info = []
42
+
43
+ # Normalize data keys for better matching
44
+ normalized_data = {}
45
+ for key, value in data.items():
46
+ # Store original
47
+ normalized_data[key] = value
48
+ # Store lowercase
49
+ normalized_data[key.lower()] = value
50
+ # Store with underscores
51
+ normalized_data[key.lower().replace(" ", "_")] = value
52
+ # Store without spaces
53
+ normalized_data[key.lower().replace(" ", "")] = value
54
 
55
  # First, try to replace placeholders in paragraphs
56
  for paragraph in doc.paragraphs:
57
+ for key, value in normalized_data.items():
 
 
 
58
  placeholders = [
59
  f"{{{{{key}}}}}", # {{field_name}}
60
  f"{{{key}}}", # {field_name}
 
66
  if key not in fields_filled:
67
  fields_filled.append(key)
68
 
69
+ # Enhanced table processing
70
+ for table_idx, table in enumerate(doc.tables):
71
+ for row_idx, row in enumerate(table.rows):
72
+ cells_text = [cell.text.strip() for cell in row.cells]
73
 
74
  # Process each cell
75
+ for cell_idx, cell in enumerate(row.cells):
76
+ cell_text = cell.text.strip().lower()
77
 
78
+ # Skip empty cells
79
+ if not cell_text:
80
+ continue
81
+
82
+ # Check each field
83
+ for original_key, value in data.items():
84
+ key_lower = original_key.lower()
85
+ key_normalized = key_lower.replace(" ", "").replace("_", "")
86
+ cell_normalized = cell_text.replace(" ", "").replace(":", "").replace("_", "")
87
+
88
+ # Method 1: Exact match (case-insensitive)
89
+ if key_lower in cell_text or cell_text in key_lower:
90
+ # Fill next cell if exists and is empty
91
+ if cell_idx + 1 < len(row.cells):
92
+ next_cell = row.cells[cell_idx + 1]
93
+ if not next_cell.text.strip() or len(next_cell.text.strip()) < 3:
94
+ next_cell.text = str(value)
95
+ if original_key not in fields_filled:
96
+ fields_filled.append(original_key)
97
+ debug_info.append(f"Filled '{original_key}' in cell after '{cell.text.strip()}'")
98
+
99
+ # Method 2: Fill after colon in same cell
100
+ if ":" in cell.text:
101
+ parts = cell.text.split(":", 1)
102
+ label = parts[0].strip()
103
+ cell.text = f"{label}: {value}"
104
+ if original_key not in fields_filled:
105
+ fields_filled.append(original_key)
106
+ debug_info.append(f"Filled '{original_key}' after colon in '{label}'")
107
 
108
+ # Method 3: Fuzzy match (normalized)
109
+ elif key_normalized in cell_normalized or cell_normalized in key_normalized:
110
+ if cell_idx + 1 < len(row.cells):
111
+ next_cell = row.cells[cell_idx + 1]
112
+ if not next_cell.text.strip() or len(next_cell.text.strip()) < 3:
113
+ next_cell.text = str(value)
114
+ if original_key not in fields_filled:
115
+ fields_filled.append(original_key)
116
+ debug_info.append(f"Filled '{original_key}' (fuzzy) after '{cell.text.strip()}'")
 
 
 
 
 
 
 
 
 
 
 
 
 
117
 
118
+ # Check for placeholders
119
+ for key, value in normalized_data.items():
120
  placeholders = [
121
  f"{{{{{key}}}}}",
122
  f"{{{key}}}",
123
  ]
124
 
125
  for placeholder in placeholders:
126
+ if placeholder.lower() in cell.text.lower():
127
  cell.text = cell.text.replace(placeholder, str(value))
128
  if key not in fields_filled:
129
  fields_filled.append(key)
130
+ debug_info.append(f"Filled placeholder '{placeholder}' with '{key}'")
131
 
132
  # Ensure output directory exists
133
  Path(output_path).parent.mkdir(parents=True, exist_ok=True)
 
136
  return {
137
  'success': True,
138
  'output_path': output_path,
139
+ 'fields_filled': list(set(fields_filled)),
140
+ 'total_fields': len(set(fields_filled)),
141
+ 'debug_info': debug_info
142
  }
143
 
144
  except Exception as e:
 
161
  placeholders = [
162
  f"{{{{{key}}}}}",
163
  f"{{{key}}}",
 
164
  ]
165
 
166
  for placeholder in placeholders:
 
181
  }
182
 
183
  except Exception as e:
184
+ import traceback
185
+ return {'error': f"{str(e)}\n\n{traceback.format_exc()}", 'success': False}
186
 
187
 
188
  async def fill_text_form(template_path: str, data: Dict[str, Any], output_path: str) -> dict:
 
218
  }
219
 
220
  except Exception as e:
221
+ import traceback
222
+ return {'error': f"{str(e)}\n\n{traceback.format_exc()}", 'success': False}
223
 
224
 
225
  async def generate_form_template(fields: list) -> dict:
 
245
  }
246
 
247
  except Exception as e:
248
+ import traceback
249
+ return {'error': f"{str(e)}\n\n{traceback.format_exc()}", 'success': False}