-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheml_converter.py
More file actions
72 lines (62 loc) · 2.5 KB
/
Copy patheml_converter.py
File metadata and controls
72 lines (62 loc) · 2.5 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
68
69
70
71
72
# Conversione di email .eml in PDF con dettagli del messaggio.
import os
import sys
import pdfkit
from email import message_from_file
# Funzione per convertire il contenuto di un file .eml in HTML
def eml_to_html(eml_file):
with open(eml_file, 'r', encoding='utf-8') as f:
msg = message_from_file(f)
subject = msg["Subject"] or "(No Subject)"
sender = msg["From"] or "(Unknown Sender)"
to = msg["To"] or "(Unknown Recipient)"
body = ""
if msg.is_multipart():
for part in msg.walk():
if part.get_content_type() == "text/plain":
body = part.get_payload(decode=True).decode('utf-8', errors='ignore')
break
else:
body = msg.get_payload(decode=True).decode('utf-8', errors='ignore')
# Creazione dell'HTML
html_content = f"""
<html>
<head><meta charset='utf-8'><title>{subject}</title></head>
<body>
<h1>Subject: {subject}</h1>
<p><strong>From:</strong> {sender}</p>
<p><strong>To:</strong> {to}</p>
<pre>{body}</pre>
</body>
</html>
"""
return html_content
# Funzione per convertire file .eml in PDF
def convert_eml_to_pdf(input_folder):
output_folder = os.path.join(input_folder, "converted_pdfs")
os.makedirs(output_folder, exist_ok=True)
for filename in os.listdir(input_folder):
if filename.lower().endswith('.eml'):
eml_path = os.path.join(input_folder, filename)
html_content = eml_to_html(eml_path)
pdf_filename = os.path.splitext(filename)[0] + ".pdf"
pdf_path = os.path.join(output_folder, pdf_filename)
pdfkit.from_string(html_content, pdf_path)
print(f"Converted: {filename} -> {pdf_path}")
def main():
input_folder = input("Inserisci il percorso della cartella contenente i file .eml: ").strip()
if not os.path.isdir(input_folder):
print("Errore: Il percorso specificato non è una cartella valida.")
return
convert_eml_to_pdf(input_folder)
print("Tutti i file .eml sono stati convertiti in PDF nella cartella 'converted_pdfs'.")
if __name__ == "__main__":
while True:
main()
scelta = input("\nUtilizza di nuovo lo script digitando 1 o premi 0 per ritornare a main.py: ").strip()
if scelta == '1':
continue
elif scelta == '0':
break
else:
print("Scelta non valida. Inserire 1 o 0.")