Spaces:
Running
Running
Update tools/form_filler_server.py
Browse files- tools/form_filler_server.py +62 -20
tools/form_filler_server.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
from typing import Dict, Any
|
| 2 |
import json
|
|
|
|
| 3 |
from docx import Document
|
| 4 |
from openpyxl import load_workbook
|
| 5 |
|
|
@@ -39,23 +40,44 @@ async def fill_docx_form(template_path: str, data: Dict[str, Any], output_path:
|
|
| 39 |
|
| 40 |
# Replace in paragraphs
|
| 41 |
for paragraph in doc.paragraphs:
|
|
|
|
|
|
|
|
|
|
| 42 |
for key, value in data.items():
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
# Replace in tables
|
| 49 |
for table in doc.tables:
|
| 50 |
for row in table.rows:
|
| 51 |
for cell in row.cells:
|
| 52 |
-
for
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
doc.save(output_path)
|
| 60 |
|
| 61 |
return {
|
|
@@ -81,11 +103,20 @@ async def fill_excel_form(template_path: str, data: Dict[str, Any], output_path:
|
|
| 81 |
for cell in row:
|
| 82 |
if cell.value and isinstance(cell.value, str):
|
| 83 |
for key, value in data.items():
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
wb.save(output_path)
|
| 90 |
|
| 91 |
return {
|
|
@@ -107,10 +138,19 @@ async def fill_text_form(template_path: str, data: Dict[str, Any], output_path:
|
|
| 107 |
|
| 108 |
fields_filled = []
|
| 109 |
for key, value in data.items():
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
with open(output_path, 'w', encoding='utf-8') as f:
|
| 116 |
f.write(content)
|
|
@@ -137,7 +177,9 @@ async def generate_form_template(fields: list) -> dict:
|
|
| 137 |
p.add_run(f"{field}: ").bold = True
|
| 138 |
p.add_run(f"{{{{{field}}}}}")
|
| 139 |
|
|
|
|
| 140 |
output_path = "data/outputs/form_template.docx"
|
|
|
|
| 141 |
doc.save(output_path)
|
| 142 |
|
| 143 |
return {
|
|
|
|
| 1 |
from typing import Dict, Any
|
| 2 |
import json
|
| 3 |
+
from pathlib import Path
|
| 4 |
from docx import Document
|
| 5 |
from openpyxl import load_workbook
|
| 6 |
|
|
|
|
| 40 |
|
| 41 |
# Replace in paragraphs
|
| 42 |
for paragraph in doc.paragraphs:
|
| 43 |
+
original_text = paragraph.text
|
| 44 |
+
modified = False
|
| 45 |
+
|
| 46 |
for key, value in data.items():
|
| 47 |
+
# Try multiple placeholder formats
|
| 48 |
+
placeholders = [
|
| 49 |
+
f"{{{{{key}}}}}", # {{field_name}}
|
| 50 |
+
f"{{{key}}}", # {field_name}
|
| 51 |
+
key # field_name
|
| 52 |
+
]
|
| 53 |
+
|
| 54 |
+
for placeholder in placeholders:
|
| 55 |
+
if placeholder in paragraph.text:
|
| 56 |
+
paragraph.text = paragraph.text.replace(placeholder, str(value))
|
| 57 |
+
modified = True
|
| 58 |
+
if key not in fields_filled:
|
| 59 |
+
fields_filled.append(key)
|
| 60 |
|
| 61 |
# Replace in tables
|
| 62 |
for table in doc.tables:
|
| 63 |
for row in table.rows:
|
| 64 |
for cell in row.cells:
|
| 65 |
+
for paragraph in cell.paragraphs:
|
| 66 |
+
for key, value in data.items():
|
| 67 |
+
placeholders = [
|
| 68 |
+
f"{{{{{key}}}}}",
|
| 69 |
+
f"{{{key}}}",
|
| 70 |
+
key
|
| 71 |
+
]
|
| 72 |
+
|
| 73 |
+
for placeholder in placeholders:
|
| 74 |
+
if placeholder in paragraph.text:
|
| 75 |
+
paragraph.text = paragraph.text.replace(placeholder, str(value))
|
| 76 |
+
if key not in fields_filled:
|
| 77 |
+
fields_filled.append(key)
|
| 78 |
+
|
| 79 |
+
# Ensure output directory exists
|
| 80 |
+
Path(output_path).parent.mkdir(parents=True, exist_ok=True)
|
| 81 |
doc.save(output_path)
|
| 82 |
|
| 83 |
return {
|
|
|
|
| 103 |
for cell in row:
|
| 104 |
if cell.value and isinstance(cell.value, str):
|
| 105 |
for key, value in data.items():
|
| 106 |
+
placeholders = [
|
| 107 |
+
f"{{{{{key}}}}}",
|
| 108 |
+
f"{{{key}}}",
|
| 109 |
+
key
|
| 110 |
+
]
|
| 111 |
+
|
| 112 |
+
for placeholder in placeholders:
|
| 113 |
+
if placeholder in cell.value:
|
| 114 |
+
cell.value = cell.value.replace(placeholder, str(value))
|
| 115 |
+
if key not in fields_filled:
|
| 116 |
+
fields_filled.append(key)
|
| 117 |
+
|
| 118 |
+
# Ensure output directory exists
|
| 119 |
+
Path(output_path).parent.mkdir(parents=True, exist_ok=True)
|
| 120 |
wb.save(output_path)
|
| 121 |
|
| 122 |
return {
|
|
|
|
| 138 |
|
| 139 |
fields_filled = []
|
| 140 |
for key, value in data.items():
|
| 141 |
+
placeholders = [
|
| 142 |
+
f"{{{{{key}}}}}",
|
| 143 |
+
f"{{{key}}}",
|
| 144 |
+
]
|
| 145 |
+
|
| 146 |
+
for placeholder in placeholders:
|
| 147 |
+
if placeholder in content:
|
| 148 |
+
content = content.replace(placeholder, str(value))
|
| 149 |
+
if key not in fields_filled:
|
| 150 |
+
fields_filled.append(key)
|
| 151 |
+
|
| 152 |
+
# Ensure output directory exists
|
| 153 |
+
Path(output_path).parent.mkdir(parents=True, exist_ok=True)
|
| 154 |
|
| 155 |
with open(output_path, 'w', encoding='utf-8') as f:
|
| 156 |
f.write(content)
|
|
|
|
| 177 |
p.add_run(f"{field}: ").bold = True
|
| 178 |
p.add_run(f"{{{{{field}}}}}")
|
| 179 |
|
| 180 |
+
# Ensure output directory exists
|
| 181 |
output_path = "data/outputs/form_template.docx"
|
| 182 |
+
Path(output_path).parent.mkdir(parents=True, exist_ok=True)
|
| 183 |
doc.save(output_path)
|
| 184 |
|
| 185 |
return {
|