二、API 準備工作

2.1 注冊并獲取開發者 Token

登錄 Evernote Developer 網站:

2.2 安裝 Python SDK

Evernote 官方提供了 Python SDK:

pip install evernote

安裝成功后,可以通過如下代碼快速連接 NoteStore:

from evernote.api.client import EvernoteClient

client = EvernoteClient(token="your_token", sandbox=False)
note_store = client.get_note_store()

三、ENML:Evernote 的筆記結構語言

Evernote 使用 ENML(Evernote Markup Language)描述筆記內容,其語法類似 HTML:

< ?xml version="1.0" encoding="UTF-8"? >
< !DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd" >
< en-note >
    Hello, this is a simple text note.
< /en-note >

ENML 支持的標簽:


四、創建基本文本筆記

from evernote.edam.type import ttypes as Types

note = Types.Note()
note.title = "Hello Evernote"
note.content = """ < ?xml version="1.0" encoding="UTF-8"? >
< !DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd" >
< en-note > This is a text note. < /en-note > """

created_note = note_store.createNote(note)
print("Note GUID:", created_note.guid)

五、添加多媒體內容:圖片、音頻、PDF

5.1 創建資源(Resource)

每個圖片、音頻或 PDF 文件都作為 Resource 上傳,并通過 < en-media > 標簽嵌入 ENML 中。

import hashlib, binascii
from evernote.edam.type import ttypes as Types

def create_resource(file_path, mime_type):
    with open(file_path, "rb") as f:
        data_bytes = f.read()
    md5 = hashlib.md5(data_bytes).digest()
    data = Types.Data(bodyHash=md5, size=len(data_bytes), body=data_bytes)
    resource = Types.Resource(mime=mime_type, data=data)
    return resource, binascii.hexlify(md5).decode()

5.2 插入圖片示例

img_res, img_hash = create_resource("image.jpg", "image/jpeg")

note = Types.Note()
note.title = "包含圖片的筆記"
note.resources = [img_res]
note.content = f""" < ?xml version="1.0" encoding="UTF-8"? >
< !DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd" >
< en-note >
    這是插入的圖片:< br/ >
    < en-media type="image/jpeg" hash="{img_hash}"/  >
< /en-note  > """

note_store.createNote(note)

5.3 插入 PDF 示例

pdf_res, pdf_hash = create_resource("doc.pdf", "application/pdf")

note = Types.Note()
note.title = "PDF 示例筆記"
note.resources = [pdf_res]
note.content = f""" < ?xml version="1.0" encoding="UTF-8"?  >
< !DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd"  >
< en-note  >
    請查收 PDF 文檔:< br/  >
    < en-media type="application/pdf" hash="{pdf_hash}"/  >
< /en-note  > """

note_store.createNote(note)

六、音頻筆記的實現

將語音文件(如 MP3)嵌入筆記,非常適合制作語音日志或聽課記錄。

audio_res, audio_hash = create_resource("voice.mp3", "audio/mpeg")

note = Types.Note()
note.title = "語音筆記"
note.resources = [audio_res]
note.content = f""" < ?xml version="1.0" encoding="UTF-8"?  >
< !DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd"  >
< en-note  >
    聽課錄音如下:< br/  >
    < en-media type="audio/mpeg" hash="{audio_hash}"/  >
< /en-note  > """

note_store.createNote(note)

七、筆記標簽與筆記本管理

為筆記指定標簽與筆記本:

note.tagNames = ["多媒體", "項目資料"]
note.notebookGuid = "your_notebook_guid"

創建新的筆記本:

notebook = Types.Notebook()
notebook.name = "語音與圖像筆記"
created_nb = note_store.createNotebook(notebook)
print("Notebook GUID:", created_nb.guid)

八、讀取筆記中的多媒體資源

要下載筆記中的附件,可按如下方式獲取:

note = note_store.getNote(guid, True, False, False, False)
for res in note.resources:
    resource_data = note_store.getResource(res.guid, True, False, False, False)
    with open(res.guid + ".bin", "wb") as f:
        f.write(resource_data.data.body)

九、常見錯誤與排查

問題類型 錯誤信息 解決方案
ENML 錯誤 "ENML Validation Error" 檢查 XML 結構、標簽閉合、必須使用 en-note 根標簽
資源上傳失敗 "hash mismatch" 保證 < en-media > 中 hash 與 Resource 中一致
Token 認證失敗 "EDAMUserException: authenticationFailed" 檢查 token 是否有效,或切換 OAuth 授權
頻繁訪問被限流 "EDAMSystemException: rateLimit" 避免重復創建,添加重試邏輯與限速

十、實戰案例:構建圖文語音日報系統

使用 Evernote API,我們可以輕松構建一套圖文語音日報系統:


十一、總結與延伸

本文帶你完成:

未來可以進一步:

上一篇:

如何使用rest api發送電子郵件

下一篇:

Nexus API 的入門教程與使用指南
#你可能也喜歡這些API文章!

我們有何不同?

API服務商零注冊

多API并行試用

數據驅動選型,提升決策效率

查看全部API→
??

熱門場景實測,選對API

#AI文本生成大模型API

對比大模型API的內容創意新穎性、情感共鳴力、商業轉化潛力

25個渠道
一鍵對比試用API 限時免費

#AI深度推理大模型API

對比大模型API的邏輯推理準確性、分析深度、可視化建議合理性

10個渠道
一鍵對比試用API 限時免費