-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
68 lines (55 loc) · 1.63 KB
/
utils.py
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
import csv
import pdfkit
import os
from datetime import datetime
import shutil
import zipfile
from constants import *
def clean_out_dir():
"""
Clean the output folder
"""
shutil.rmtree(OUTPUT_FOLDER_NAME)
def csv_to_dict(file: bytes) -> list:
"""
Convert a csv file to a list of dictionaries
"""
data = file.decode("utf-8")
reader = csv.DictReader(data.splitlines())
return list(reader)
def replace_message_context_with_html(
html_content: str, message_context: dict
) -> bytes:
"""
Replace the message context with html content
"""
msg = html_content
for key in message_context:
msg = msg.replace("{{" + key + "}}", message_context[key])
return msg
def html_str_to_pdf(html_content: list):
"""
Convert html content to pdf
"""
os.makedirs(OUTPUT_FOLDER_NAME, exist_ok=True)
for content in html_content:
for key in content:
_content = content[key]
curr_datetime = datetime.now().strftime("%b-%d-%Y-%H:%M:%S")
pdfkit.from_string(
_content,
f"{OUTPUT_FOLDER_NAME}/{curr_datetime}-{list(content.keys())[0]}.pdf",
)
return True
def get_output_pdfs():
"""
Get all the pdfs in the output folder
"""
return [file for file in os.listdir(OUTPUT_FOLDER_NAME) if file.endswith(".pdf")]
def zip_output_pdf_files():
files = get_output_pdfs()
output_file_name = f"{OUTPUT_FOLDER_NAME}/output"
myZipFile = zipfile.ZipFile(output_file_name, "w")
for file in files:
myZipFile.write(f"{OUTPUT_FOLDER_NAME}/{file}", file)
return output_file_name