-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdf_tools.py
46 lines (40 loc) · 1.39 KB
/
pdf_tools.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
import os
from eml_processing import extract_text_from_eml
import html2pdf
from PyPDF2 import PdfMerger
def text_to_pdf(text, pdf_file):
try:
html2pdf.HTMLToPDF(text.decode('utf-8'), pdf_file)
except Exception as e:
print(f"An error occurred while converting {pdf_file} to pdf: {e}")
def combine_pdfs(pdf_files, output_file):
pdf_merger = PdfMerger()
try:
for pdf_file in pdf_files:
with open(pdf_file, 'rb') as file:
pdf_merger.append(file)
except Exception as e:
print("Error opening file:", e)
return
try:
with open(output_file, 'wb') as file:
pdf_merger.write(file)
except Exception as e:
print("Error writing file:", e)
return
def create_pdf_folder(pdf_folder):
if not os.path.exists(pdf_folder):
os.makedirs(pdf_folder)
def convert_eml_to_pdf(eml_folder, pdf_folder, eml_file):
pdf_file = os.path.join(pdf_folder, os.path.splitext(eml_file)[0] + ".pdf")
try:
text = extract_text_from_eml(os.path.join(eml_folder, eml_file))
text_to_pdf(text.encode("utf-8"), pdf_file)
except Exception as e:
print(f"Failed to convert {eml_file} to pdf: {e}")
return pdf_file
def combine_pdfs_func(pdf_files, output_file):
try:
combine_pdfs(pdf_files, output_file)
except Exception as e:
print(f"Failed to combine pdfs: {e}")