My impression of Evernote API documentation is that it is not very friendly to Python beginners, and rather short on examples. So I wrote this document on how to write a simple script that will get notes from a notebook.
Here is the idea. To get a note, you need to use
noteStore.getNote() method. It requires the note's
Guid. How would you get it? You get it from the notes' metadata by calling
noteStore.findNotesMetadata() and passing it a filter that will select all the notes you need.
For example, maybe you need all the notes in a particular notebook. For that, you should create a
NoteFilter object that contains this notebook's
How would you get a notebook's
Guid? Good question. I admit I haven't looked very hard in Evernote API, since retrieving the notes is only a one-time setup step for a much bigger project, so I was satisfied with a quick-and-dirty script. The easiest thing for me seemed to print out the names of all the notebooks, then figure out that the notebook I needed had index 1 in that array. Thus, I passed to the
noteFilter = NoteStore.NoteFilter(notebookGuid=notebooks.guid)
Which means that before my note-getting script I ran this preliminary script:
from evernote.api.client import EvernoteClient import evernote.edam.type.ttypes as Types from evernote.edam.notestore import NoteStore dev_token = "xxxx" #this should be your dev_token client = EvernoteClient(token=dev_token) noteStore = client.get_note_store() notebooks = noteStore.listNotebooks() for n in notebooks: print "name = " + n.name + " GUID = " + n.guid
And now that you know the index of the notebook you want to retrieve -- let's call it
ind, here is the main script. It gets all the notes from a notebook, and writes them to files.
from evernote.api.client import EvernoteClient import evernote.edam.type.ttypes as Types from evernote.edam.notestore import NoteStore dev_token = "xxxx" #this should be your dev_token client = EvernoteClient(token=dev_token) noteStore = client.get_note_store() notebooks = noteStore.listNotebooks() # The ind here is the index of the notebook you need in the notebooks list. noteFilter = NoteStore.NoteFilter(notebookGuid=notebooks[ind].guid) spec = NoteStore.NotesMetadataResultSpec() # We are starting with the first note (offset=0) and getting 25 notes # (or rather, their metadata). # You should set these numbers appropriately for your needs. nmdList = noteStore.findNotesMetadata(noteFilter, 0, 25, spec) for n in nmdList.notes: # The first boolean argument is withContent=True. I am passing the rest as False, # because my notes don't have resources, such as images. You should, of course, # find out which of these arguments, if any, should be set to True. note = noteStore.getNote(n.guid, True, False, False, False) # The file name of the note will be its Guid with extension .txt fileName = 'C:\\path\\to\\your\\notes\\' + str(n.guid) + '.txt' f = open(fileName, 'w') f.write(note.content) f.close()