Skip to content

Instantly share code, notes, and snippets.

@paultopia
Last active October 2, 2015 00:26
Show Gist options
  • Save paultopia/66daf88f394366ac45f1 to your computer and use it in GitHub Desktop.
Save paultopia/66daf88f394366ac45f1 to your computer and use it in GitHub Desktop.
# takes all HTML files in a directory, dumps it to evernote notebook.
# usage: designed for quick transfer of apple notes to evernote
# using the tool provided at https://github.com/pmatiello/notes-export
# first to get html files dumped in a notebook.
# then call from command line:
# python n2ev.py DEVTOKEN FULLPATHTODIR NOTEBOOKNAME
# DEVTOKEN is a developer token for your evernote acconut, which can
# be acquired from evernote: https://dev.evernote.com/#apikey
# FULLPATHTODIR is, obviously, the path to the directory in which your notes
# are stored, as html files. NOTEBOOKNAME is the new evernote notebook to
# be created. I have no idea what will happen if you give it a name that
# already exists, and strongly recommend you don't do so.
# also FULLPATHTODIR should not have anything other than the html files in it
# (and from which it follows that it should not be the same directory as
# this script is stored in)
#
# in addition to the standard library, this REQUIRES:
# evernote module, which can be installed via pip.
# beautifulsoup, ditto.
#
# I'm not responsible if you delete your shit using this. Only run it if
# you know how to code.
import sys, evernote, os
from bs4 import BeautifulSoup
from evernote.api.client import EvernoteClient
import evernote.edam.type.ttypes as Types
devtoken = sys.argv[1]
directory = sys.argv[2]
os.chdir(directory)
newbook = sys.argv[3]
client = EvernoteClient(token=devtoken)
noteStore = client.get_note_store()
userStore = client.get_user_store() # not sure if this is necessary
# create a notebook
notebook = Types.Notebook()
notebook.name = newbook
notebook = noteStore.createNotebook(notebook)
der_nbguid = notebook.guid
# this function takes a html file, extracts the first line, removes <div> and
# </div> from it, makes that note title; then extracts entire text from file
# (including first line, redundantly), converts it to text, and returns as
# evernote note object.
def html_to_evernote(htmlfile):
with open(htmlfile) as the_html:
firstline = the_html.readline().strip()
wholefile = the_html.read()
notebody = BeautifulSoup(wholefile).get_text()
notetitle = BeautifulSoup(firstline).get_text()
note = Types.Note()
notetitle = notetitle.replace('<div>', '').replace('</div>','')
print 'notetitle is: ' + notetitle
print 'notebody is: ' + notebody
note.title = firstline
note.content = '<?xml version="1.0" encoding="UTF-8"?>'
note.content += '<!DOCTYPE en-note SYSTEM ' \
'"http://xml.evernote.com/pub/enml2.dtd">'
note.content += '<en-note>'
note.content += notebody
note.content += '</en-note>'
note.content = note.content.encode('utf-8')
note.notebookGuid = der_nbguid
note = noteStore.createNote(note)
return note
notefiles = os.listdir(directory)
for notefile in notefiles:
anote = html_to_evernote(notefile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment