This repository was archived by the owner on Apr 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.py
More file actions
67 lines (47 loc) · 1.31 KB
/
create.py
File metadata and controls
67 lines (47 loc) · 1.31 KB
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
import re
from docx import Document
import os
from urllib.parse import urlparse
def mails(csv, templ):
all_mails = []
data = csv['data']
for row in data:
m = {}
# Subject
m['subject'] = row['oggetto']
# Recipient
m['recipient'] = row['pec']
# Body
doc = mail_text(row, templ)
text_doc = get_text(doc)
m['body'] = text_doc
# Attachments
m['attachments'] = []
for k,v in row.items():
if 'allegato' in k and v != '':
m['attachments'].append(v)
all_mails.append(m)
return all_mails
def mail_text(row, templ):
doc = Document()
pattern = '\$\{(.*?)\}'
for p in templ.paragraphs:
par_vars = re.findall(pattern, p.text)
# Empty line
if p.text == '':
continue
# No variables
if len(par_vars) == 0:
doc.add_paragraph(p.text)
continue
# Replace vars
upd_p = p.text
for v in par_vars:
upd_p = upd_p.replace('${' + v + '}', row[v])
doc.add_paragraph(upd_p)
return doc
def get_text(doc):
fullText = []
for para in doc.paragraphs:
fullText.append(para.text)
return '\n\n'.join(fullText)