File size: 5,028 Bytes
32285b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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}