mbox-to-markdown/mbox_to_markdown.py

32 lines
932 B
Python
Raw Normal View History

2024-07-19 10:44:15 +00:00
import mailbox
import os
from markdownify import markdownify
# Configuration
mbox_file = 'path/to/your/file.mbox'
output_dir = 'path/to/output/directory'
# Ensure output directory exists
os.makedirs(output_dir, exist_ok=True)
def save_email_as_markdown(email, index):
subject = email.get('subject', 'No Subject')
date = email.get('date', 'No Date')
body = email.get_payload(decode=True).decode(errors='ignore')
body_markdown = markdownify(body)
# Create a Markdown file for each email
filename = os.path.join(output_dir, f'email_{index}.md')
with open(filename, 'w', encoding='utf-8') as file:
file.write(f'# {subject}\n')
file.write(f'*Date: {date}*\n\n')
file.write(body_markdown)
def convert_mbox_to_markdown(mbox_file):
mbox = mailbox.mbox(mbox_file)
for i, email in enumerate(mbox):
save_email_as_markdown(email, i)
convert_mbox_to_markdown(mbox_file)