Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DevGW/c4c6c5f64590e9dbcac20207ca5df45f to your computer and use it in GitHub Desktop.
Save DevGW/c4c6c5f64590e9dbcac20207ca5df45f to your computer and use it in GitHub Desktop.
Convert Sticky Notes (outlook) .eml files to markdown files.md
import re

def sanitize_filename(filename):
    """
    Sanitize the filename to ensure it's valid and does not contain prohibited characters.
    """
    filename = re.sub(r'[<>:"/\\|?*\']+', '', filename)  # Remove invalid characters
    filename = filename.replace('\n', ' ').replace('\r', '')  # Replace newlines
    return filename.strip()

# Folder to store the markdown files
markdown_folder_path = '/mnt/data/notes_markdown'
os.makedirs(markdown_folder_path, exist_ok=True)

# Process each EML file
for eml_file in sticky_notes_files:
    eml_file_path = os.path.join(sticky_notes_folder_path, eml_file)
    with open(eml_file_path, 'rb') as file:
        msg = BytesParser(policy=policy.default).parse(file)
    
    # Extract subject and body
    subject = msg['Subject']
    body = msg.get_body(preferencelist=('plain')).get_content()

    # Sanitize the subject to use as a filename
    filename = sanitize_filename(subject) + '.md'

    # Write to markdown file
    with open(os.path.join(markdown_folder_path, filename), 'w', encoding='utf-8') as md_file:
        md_file.write(body)

# Return the path to the folder containing the markdown files
markdown_folder_path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment