Package: trilium-py
🐍 trilium-pyEnglish |简体中文Python client for ETAPI of Trilium Note.🦮 Table of Contents🐍 trilium-py🦮 Table of Contents🔧 Installation📖 (Basic) Usage🚀 Initialization📊 Application Information🔍 Search note🏭 Create Note🖼️ Create Image note👀 Get note🔄 Update note🗑️ Delete note📅 Day note📤 Export note💾 Create data backup🏷 Create AttributeGet attachment infoUpdate attachment infoGet attachment contentUpdate attachment contentCreate attachment(Advanced Usage) ✅ TODO ListAdd TODO itemCheck/Uncheck a TODO itemUpdate a TODO itemDelete a TODO itemMove yesterday's unfinished todo to today(Advanced Usage) 🚚 Upload Markdown filesUpload single Markdown file with imagesBulk upload Markdown files in a folderImport from VNoteImport from JoplinImport from LogseqImport from ObsidianImport from Youdao Note/有道云笔记Import from TurtlImport from other markdown software(Advanced Usage) 🎨 Beautify notesBeautify a noteBeautify a note and its child notes(Advanced Usage) Sort note content🛠️ Develop🔗 Original OpenAPI Documentation🔧 Installationpython3-mpipinstalltrilium-py--user📖 (Basic) UsageThese are basic function that Trilium's ETAPI provides. Down below are some simple example code to use this package.🚀 InitializationIf you have a ETAPI token, change theserver_urlandtokento yours.fromtrilium_py.clientimportETAPIserver_url='http://localhost:8080'token='YOUR_TOKEN'ea=ETAPI(server_url,token)If you haven't created ETAPI token, you can create one with your password. Please note, you can only see this token
once, please save it if you want to reuse the token.fromtrilium_py.clientimportETAPIserver_url='http://localhost:8080'password='1234'ea=ETAPI(server_url)token=ea.login(password)print(token)After initialization, you can use Trilium ETAPI with python now. The following are some examples.📊 Application InformationTo start with, you can get the application information like this.print(ea.app_info())It should give you the version of your server application and some extra information.🔍 Search noteSearch note with keyword.res=ea.search_note(search="python",)forxinres['results']:print(x['noteId'],x['title'])Search with regular expression. For example, search and get all child notes under certain note:res = ea.search_note(
    # regular expression search for note title
    search="note.title %= '.*'",
    ancestorNoteId="Parent Note ID",
    fastSearch=False,
    limit=1000,
)🏭 Create NoteYou can create a simple note like this.res=ea.create_note(parentNoteId="root",title="Simple note 1",type="text",content="Simple note example",noteId="note1")ThenoteIdis not mandatory, if not provided, Trilium will generate a random one. You can retrieve it in the return.noteId=res['note']['noteId']🖼️ Create Image noteImage note is a special kind of note. You can create an image note with minimal information like this. Theimage_filerefers to the path of image.res=ea.create_image_note(parentNoteId="root",title="Image note 1",image_file="shield.png",)👀 Get noteTo retrieve the note's content.ea.get_note_content("noteid")You can get a note metadata by its id.ea.get_note(note_id)🔄 Update noteUpdate note contentea.update_note_content("noteid","updated by python")Modify note titleea.patch_note(noteId="noteid",title="Python client moded",)🗑️ Delete noteSimply delete a note by id.ea.delete_note("noteid")📅 Day noteYou can get the content of a certain date withget_day_note. The date string should be in format of "%Y-%m-%d", e.g. "
2022-02-25".ea.get_day_note("2022-02-25")Then set/update a day note withset_day_note. The content should be a (html) string.ea.set_day_note(date,new_content)📤 Export noteExport note comes in two formatshtmlormarkdown/md.res=ea.export_note(noteId='sK5fn4T6yZRI',format='md',savePath='/home/nate/data/1/test.zip',)💾 Create data backupThis example will create a database backup file like thistrilium-data/backup/backup-test.db.res=ea.backup("test")You can use the cron utility in Linux to schedule regular automatic backups. For example, to set up a daily backup at 3:
00 AM, you would use the following cron expression:0 3 * * * python /path/to/backup-script.py🏷 Create attributeYou can create a tag for a noteres=ea.create_attribute(noteId='noteid',type='label',name='name_of_the_tag',value='value_of_the_tag',isInheritable=True)ThenoteIdis not mandatory, if not provided, Trilium will generate a random one. You can retrieve it in the return.noteId=res['note']['noteId']Get attachment infoGet image title and etc.res = ea.get_attachment('Y5V6pYq6nwXo')Update attachment infoChange image title and etc.res = ea.update_attachment(
    attachmentId='2b7pPzqocS1s', title='hello etapi', role='image', mime='image/png'
)Get attachment contentGet the real image fileres = ea.get_attachment_content('icpDE4orQxlI')
with open('1.png', 'wb') as f:
    f.write(res)Update attachment contentReplace the image with new oneres = ea.update_attachment_content('icWqV6zFtE0V', '/home/nate/data/1.png')Create attachmentUpload a image file as attachment of a note.res = ea.create_attachment(
    ownerId='8m8luXym5LxT',
    file_path='/home/nate/data/ksnip_20230630-103509.png',
)(Advanced Usage) ✅ TODO ListWith the power of Python, I have expanded the basic usage of ETAPI. You can do something with todo list now.Add TODO itemYou can useadd_todoto add a TODO item, param is the TODO descriptionea.add_todo("买暖宝宝")Check/Uncheck a TODO itemparam is the index of the TODO itemea.todo_check(0)ea.todo_uncheck(1)Update a TODO itemUseupdate_todoto update a TODO item description at certain index.ea.update_todo(0,"去码头整点薯条")Delete a TODO itemRemove a TODO item by its index.ea.delete_todo(1)Move yesterday's unfinished todo to todayAs the title suggests, you can move yesterday's unfinished things to today. Unfinished todo's will be deleted from
yesterday's note.ea.move_yesterday_unfinished_todo_to_today()(Advanced Usage) 🚚 Upload Markdown filesUpload single Markdown file with imagesYou can import Markdown file with images into Trilium now! Trilium-py will help you to upload the images and fix the
links for you!res=ea.upload_md_file(parentNoteId="root",file="./md-demo/manjaro 修改caps lock.md",)Bulk upload Markdown files in a folderYou can upload a folder with lots of Markdown files to Trilium and preserve the folder structure!Import from VNoteSay, upload all the notes fromVNote, simply do this:res=ea.upload_md_folder(parentNoteId="root",mdFolder="~/data/vnotebook/",ignoreFolder=['vx_notebook','vx_recycle_bin','vx_images','_v_images'],)Import from JoplinJoplin can be imported effortlessly.res=ea.upload_md_folder(parentNoteId="root",mdFolder="/home/nate/data/joplin_data/",ignoreFolder=['_resources',],)Import from Logseqres=ea.upload_md_folder(parentNoteId="root",mdFolder="/home/nate/data/logseq_data/",ignoreFolder=['assets','logseq'],)Import from ObsidianObsidian has a very unique linking system for files. You should useobsidian-exportto convert a Obsidian vault to regular Markdown files. Then you should be
able to import the note into Trilium with trilium-py.Convert it first.obsidian-export/path/to/your/vault/outThen import just like a normal markdown, trilium-py will handle the images for you.res=ea.upload_md_folder(parentNoteId="root",mdFolder="E:/data/out",)Import from Youdao Note/有道云笔记Youdao does not provide an export feature anymore. Luckily, you can usehttps://github.com/DeppWang/youdaonote-pullto
download your notes and convert them into markdown files. After that, trilium-py should be able to help you import them.res=ea.upload_md_folder(parentNoteId="root",mdFolder="/home/nate/gitRepo/youdaonote-pull/out/",)Import from TurtlYou need to convert Turtl from json to markdown first.
Seeturtl-to-markdownfor details.Then you can import with trilium-py like this:res=ea.upload_md_folder(parentNoteId="root",mdFolder="/home/nate/gitRepo/turtl-to-markdown/out/",ignoreFolder=['_resources'],)Import from other markdown softwareIn general, markdown files have variety of standards. You can always try import them withres=ea.upload_md_folder(parentNoteId="root",mdFolder="/home/nate/data/your_markdown_files/",)If there is any problem, please feel free to create anissue.(Advanced Usage) 🎨 Beautify notesBecause of the constraints imposed by the library utilized by Trilium, imported notes may experience minor formatting
problems. These issues include an additional line appearing at the end of code blocks, images becoming integrated with
the note content, and the absence of line breaks between headings, resulting in a cramped appearance of the note
content.Here is what you can do to beautify your note.Beautify a noteSpecify a note id to beautify note content.ea.beautify_note('krm8B9JthNfi')Beautify a note and its child notesea.beautify_sub_notes('tlPuzU2szLJh')(Advanced Usage) 🧹 Sort note contentSort a note by the heading names. This feature could prove invaluable for notes containing extensive lists, such as book
titles sorted into various genres. It's equally useful for managing browser bookmarks or collecting links.Additionally, you have the option to specify a language code for sorting based on your local language. This enhances the
sorting process and tailors it to your linguistic preferences.res = ea.sort_note_content('lPxtkknjR2bJ')
res = ea.sort_note_content('y6hROhWjNmHQ', 'zh_CN.UTF-8')🛠️ DevelopInstall with pip egg link to make package change without reinstall.python-mpipinstall--user-e.🔗 Original OpenAPI DocumentationThe original OpenAPI document ishere. You
can open it withswagger editor.