Skip to content

Instantly share code, notes, and snippets.

@williamgurzoni
Created November 21, 2022 08:24
Show Gist options
  • Save williamgurzoni/c16a88d15858fee95a0905fc6a4c6dcc to your computer and use it in GitHub Desktop.
Save williamgurzoni/c16a88d15858fee95a0905fc6a4c6dcc to your computer and use it in GitHub Desktop.
Convert Evernote html to markdown file

Convert Evernote html to markdown file

Wrote this simple (messy) python script to convert Evernote html into markdown files. Using the new .md files on https://obsidian.md

from bs4 import BeautifulSoup
from mdutils import MdUtils
import glob


def convert(filename):
    print('Converting: ' + filename)
    output_name = filename.replace(
        '.html', '').replace('origin/', 'converted/')
    file = open(filename, "r").read()
    soup = BeautifulSoup(file, 'html.parser')

    # Title
    title_tag = soup.find('meta', itemprop='title')
    title = title_tag['content']

    # Date
    created_tag = soup.find('meta', itemprop='created')
    created_string = created_tag['content']
    year = created_string[0:4]
    month = created_string[4:6]
    day = created_string[6:8]
    created = year + "-" + month + "-" + day

    # Tags
    tags_tag = soup.find_all(attrs={"itemprop": "tag"})
    tags = ""
    for tag_content in tags_tag:
        tags = tags + " #" + tag_content['content']

    # Body
    body = soup.find(attrs={"class": "peso"})

    mdFile = MdUtils(file_name=output_name)
    mdFile.write('----')
    mdFile.new_line("Created at: " + created)
    mdFile.new_line("Tags:" + tags)
    mdFile.new_paragraph(body.get_text("\n"))
    mdFile.create_md_file()


for filename in glob.iglob("origin/" + '**/*.html', recursive=True):
    convert(filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment