from typing import Dict, Any import json from docx import Document from openpyxl import load_workbook async def fill_form(template_path: str, data: Dict[str, Any]) -> dict: """ Auto-fill form template with extracted data Args: template_path: Path to form template (DOCX or XLSX) data: Dictionary of field values Returns: Dict with filled form path and fields filled """ try: file_ext = Path(template_path).suffix.lower() output_path = template_path.replace(file_ext, f'_filled{file_ext}') if file_ext == '.docx': return await fill_docx_form(template_path, data, output_path) elif file_ext in ['.xlsx', '.xls']: return await fill_excel_form(template_path, data, output_path) else: # Generic text replacement return await fill_text_form(template_path, data, output_path) except Exception as e: return {'error': str(e), 'success': False} async def fill_docx_form(template_path: str, data: Dict[str, Any], output_path: str) -> dict: """Fill DOCX form template""" try: doc = Document(template_path) fields_filled = [] # Replace in paragraphs for paragraph in doc.paragraphs: for key, value in data.items(): placeholder = f"{{{{{key}}}}}" # {{field_name}} if placeholder in paragraph.text: paragraph.text = paragraph.text.replace(placeholder, str(value)) fields_filled.append(key) # Replace in tables for table in doc.tables: for row in table.rows: for cell in row.cells: for key, value in data.items(): placeholder = f"{{{{{key}}}}}" if placeholder in cell.text: cell.text = cell.text.replace(placeholder, str(value)) if key not in fields_filled: fields_filled.append(key) doc.save(output_path) return { 'success': True, 'output_path': output_path, 'fields_filled': fields_filled, 'total_fields': len(fields_filled) } except Exception as e: return {'error': str(e), 'success': False} async def fill_excel_form(template_path: str, data: Dict[str, Any], output_path: str) -> dict: """Fill Excel form template""" try: wb = load_workbook(template_path) ws = wb.active fields_filled = [] # Search for placeholders and replace for row in ws.iter_rows(): for cell in row: if cell.value and isinstance(cell.value, str): for key, value in data.items(): placeholder = f"{{{{{key}}}}}" if placeholder in cell.value: cell.value = cell.value.replace(placeholder, str(value)) fields_filled.append(key) wb.save(output_path) return { 'success': True, 'output_path': output_path, 'fields_filled': list(set(fields_filled)), 'total_fields': len(set(fields_filled)) } except Exception as e: return {'error': str(e), 'success': False} async def fill_text_form(template_path: str, data: Dict[str, Any], output_path: str) -> dict: """Fill text-based form""" try: with open(template_path, 'r', encoding='utf-8') as f: content = f.read() fields_filled = [] for key, value in data.items(): placeholder = f"{{{{{key}}}}}" if placeholder in content: content = content.replace(placeholder, str(value)) fields_filled.append(key) with open(output_path, 'w', encoding='utf-8') as f: f.write(content) return { 'success': True, 'output_path': output_path, 'fields_filled': fields_filled, 'total_fields': len(fields_filled) } except Exception as e: return {'error': str(e), 'success': False} async def generate_form_template(fields: list) -> dict: """Generate a blank form template with specified fields""" try: doc = Document() doc.add_heading('Form Template', 0) for field in fields: p = doc.add_paragraph() p.add_run(f"{field}: ").bold = True p.add_run(f"{{{{{field}}}}}") output_path = "data/outputs/form_template.docx" doc.save(output_path) return { 'success': True, 'output_path': output_path, 'fields': fields } except Exception as e: return {'error': str(e), 'success': False}