Home / GitHub Page

Import txt files

I realize you can rename files to .md and then import Markdown but it would be nice if you could just Import plain .txt files. Putting the path and file name in the Metadata would be an added bonus.

You may consider using the webclipper API to create notes through a python script. This gives you very powerful possibilities to work on the tile, content etc… Requires the web clipper feature to be enabled and joplin to be running :slight_smile:
Here is mine, adapted from https://github.com/foxmask/jong by @foxmask
It takes one argument - a filename with full path, like a PDF - and creates a note out of it in Joplin. The title of the note is the filename

Disclaimer : this is the first python I’m writing, please excuse the basicness of some constructs !

#!/usr/bin/env python
# coding: utf-8
"""
    News generator for creating joplin notes

    The script use the webclipper service port - Enable the service from the menu "Tools > Webclipper option"
    Launch

    python manage.py run

    Have Fun
"""
# std lib
from __future__ import unicode_literals
import datetime
import time
#import asks
import requests
import sys
import os.path
from logging import getLogger
logger = getLogger('import.nc')

JOPLIN_WEBCLIPPER = 41184
JOPLIN_NOTEBOOK="Import_NC"

fileName=os.path.abspath(sys.argv[1])
print("Adding {} to Joplin".format(fileName))

# Create the content of the note
NCpath=fileName.replace("/home/user/Nextcloud/user1/","")
content=""
content+="Nextcloud : https://doma.in/remote.php/webdav/user1/{}\n".format(NCpath)
content+="Local : [{}](/{})\n".format(os.path.basename(fileName),fileName)
content+='\n\n# Bookmarks\n'
content+="Page  | Description\n"
content+="------ | ------------\n"
content+="x      | x\n"
content+="x      | x\n"

# Find the ID of the destination folder
res = requests.get("http://127.0.0.1:{}/folders".format(JOPLIN_WEBCLIPPER))
folders = res.json()

notebook_id = 0
for folder in folders:
    if folder.get('title') == JOPLIN_NOTEBOOK:
        notebook_id = folder.get('id')
if notebook_id == 0:
    for folder in folders:
        if 'children' in folder:
            for child in folder.get('children'):
                if child.get('title') == JOPLIN_NOTEBOOOK:
                    notebook_id = child.get('id')
data = {'title': os.path.basename(fileName).replace('.pdf','').replace('_',' '),
        'body': content,
        'parent_id': notebook_id,
        'author': sys.argv[0]}
res = requests.post("http://127.0.0.1:{}/notes".format(JOPLIN_WEBCLIPPER), json=data)
if res.status_code == 200:
    logger.info("fileName %s added" % (fileName))
    print("Added {} to Joplin".format(fileName))
else:
    print("ERROR while adding {} to Joplin\n".format(fileName))

ha, nice! saw this just after I built my own python module.
https://discourse.joplin.cozic.net/t/image-and-text-file-uploader/719
Primary focus of mine is to handle notes with images…

cool, thanks ! will have a look at it.

Cute.
I made some similar tools a short while ago.
One of my tools bypasses Joplin clients alltogether and manipulates the notes on the cloud server (local mirror of ownCloud/nextCloud).

I spent some time to polish them up and now they are on GitHub: https://github.com/sciurius/Joplin-Tools . Feel free to play and feedback.