Skip to content

Instantly share code, notes, and snippets.

@xiaoganghan
Created July 27, 2012 07:26
Show Gist options
  • Save xiaoganghan/3186646 to your computer and use it in GitHub Desktop.
Save xiaoganghan/3186646 to your computer and use it in GitHub Desktop.
Parsing Evernote export file (.enex) using Python
from lxml import etree
from StringIO import StringIO
p = etree.XMLParser(remove_blank_text=True, resolve_entities=False)
def parseNoteXML(xmlFile):
context = etree.iterparse(xmlFile, encoding='utf-8', strip_cdata=False)
note_dict = {}
notes = []
for ind, (action, elem) in enumerate(context):
text = elem.text
if elem.tag == 'content':
text = []
r = etree.parse(StringIO(elem.text.encode('utf-8')), p)
for e in r.iter():
try:
text.append(e.text)
except:
print 'cannot print'
note_dict[elem.tag] = text
if elem.tag == "note":
notes.append(note_dict)
note_dict = {}
return notes
if __name__ == '__main__':
notes = parseNoteXML('mynote.enex')
@samba
Copy link

samba commented May 3, 2014

Just wanted to say Thanks for posting this, @chrishan; it proved very useful as an XML formatting reference when writing a migration tool for Tomboy to Evernote. (https://github.com/samba/trombone)

@bbirand
Copy link

bbirand commented Jun 16, 2015

👍

@wespe
Copy link

wespe commented Jul 21, 2016

Good gemacht!

@BeWe11
Copy link

BeWe11 commented Jan 13, 2021

Python 3 version:

from lxml import etree
from io import BytesIO
#http://www.hanxiaogang.com/writing/parsing-evernote-export-file-enex-using-python/
p = etree.XMLParser(remove_blank_text=True, resolve_entities=False)
def parseNoteXML(xmlFile):
    context = etree.iterparse(xmlFile, encoding='utf-8', strip_cdata=False)
    note_dict = {}
    notes = []
    for ind, (action, elem) in enumerate(context):
        text = elem.text
        if elem.tag == 'content':
            text = []
            r = etree.parse(BytesIO(elem.text.encode('utf-8')), p)
            for e in r.iter():
                try:
                    text.append(e.text)
                except:
                    print('cannot print')
        note_dict[elem.tag] = text
        if elem.tag == "note":
            notes.append(note_dict)
            note_dict = {}
    return notes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment