narinder1231 commited on
Commit
d76fac3
·
1 Parent(s): 6a3bea4

Refactor proposal generation to use DOCX template

Browse files
Files changed (2) hide show
  1. Template.docx +0 -0
  2. src/app.py +30 -28
Template.docx ADDED
Binary file (385 kB). View file
 
src/app.py CHANGED
@@ -5,6 +5,8 @@ from enum import Enum
5
  import pypandoc
6
  from fastapi import FastAPI, HTTPException
7
  from pydantic import BaseModel
 
 
8
 
9
  from src.utils import EmailClient
10
 
@@ -12,13 +14,8 @@ app = FastAPI()
12
 
13
  pypandoc.download_pandoc()
14
 
15
- TEMPLATE = ""
16
- with open("src/proposal_template.md", "r") as file:
17
- TEMPLATE = file.read()
18
-
19
- TEMPLATE = TEMPLATE.replace("`", "")
20
- VARIABLES = re.findall(r"\{(.*?)\}", TEMPLATE)
21
-
22
 
23
  class Email(str, Enum):
24
  sukhwinder = "sukhwinder@sifars.com"
@@ -45,28 +42,33 @@ class ProposalRequest(BaseModel):
45
 
46
 
47
  async def generate_proposal(data: dict):
48
- proposal = TEMPLATE
49
- for variable in VARIABLES:
50
- proposal = proposal.replace(
51
- f"{{{variable}}}",
52
- data.get(variable, f"DATA NOT FOUND FOR {variable}. FIXME."),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  )
54
- with tempfile.NamedTemporaryFile(delete=False, suffix=".md") as temp_file:
55
- temp_file.write(proposal.encode())
56
- temp_file.flush()
57
- docx_file_path = temp_file.name.replace(".md", ".docx")
58
- pypandoc.convert_file(temp_file.name, "docx", outputfile=docx_file_path)
59
- attachments = [docx_file_path]
60
- recipient_email = data.get("recipient_email", "sukhwinder@sifars.com")
61
- subject = f"Proposal for {data.get('project_name')}"
62
- body = f"Hello,\n\nPlease find the proposal attached.\n\nBest,\nProposal Bot"
63
- async with EmailClient() as email_client:
64
- await email_client.send_email(
65
- subject=subject,
66
- body=body,
67
- to_emails=[recipient_email],
68
- attachments=attachments,
69
- )
70
  return {"message": "Proposal sent successfully"}
71
 
72
 
 
5
  import pypandoc
6
  from fastapi import FastAPI, HTTPException
7
  from pydantic import BaseModel
8
+ from docx import Document
9
+ from docx.shared import Pt
10
 
11
  from src.utils import EmailClient
12
 
 
14
 
15
  pypandoc.download_pandoc()
16
 
17
+ template_path = "Template.docx"
18
+ DOC = Document(template_path)
 
 
 
 
 
19
 
20
  class Email(str, Enum):
21
  sukhwinder = "sukhwinder@sifars.com"
 
42
 
43
 
44
  async def generate_proposal(data: dict):
45
+
46
+ for para in DOC.paragraphs:
47
+ for key, value in data.items():
48
+ if f"{{{key}}}" in para.text:
49
+ para.text = para.text.replace(f"{{{key}}}", value)
50
+ for run in para.runs:
51
+ run.font.size = Pt(14)
52
+ run.font.name = "Arial"
53
+
54
+
55
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".docx") as temp_file:
56
+ DOC.save(temp_file.name)
57
+ docx_file_path = temp_file.name
58
+
59
+ attachments = [docx_file_path]
60
+ recipient_email = data.get("recipient_email", "sukhwinder@sifars.com")
61
+ subject = f"Proposal for {data.get('project_name')}"
62
+ body = f"Hello,\n\nPlease find the proposal attached.\n\nBest,\nProposal Bot"
63
+
64
+ async with EmailClient() as email_client:
65
+ await email_client.send_email(
66
+ subject=subject,
67
+ body=body,
68
+ to_emails=[recipient_email],
69
+ attachments=attachments,
70
  )
71
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  return {"message": "Proposal sent successfully"}
73
 
74