Import from Text Files

daedalus28

Import from Text Files

Postby daedalus28 » Sat Oct 29, 2011 4:28 am

I currently have a simple system - a 'Notes' directory on my hard drive containing a bunch of txt files. It works, but I liked the idea of a single UI. I've thought about writing my own, but CintaNotes looks pretty nice.

Though I've looked into CintaNotes a few times, I never took the plunge because it was incompatible with my current system (without resorting to excessive copy and paste). I've checked the roadmap, and it seems that it's on the list, but that doesn't help me now. I wanted to implement it myself, but despite being free, CintaNotes doesn't seem to be open source.

So I did what any good software developer would do, and I wrote myself a tool to do it for me. And, unlike CintaNotes, I'm releasing it as open source under the GPL :ugeek:

Right now, this just takes a directory of txt files and builds an XML file from them, using the files' created timestamps for the date. An export tool is planned, and I should have it for you guys soon. It was written in python, which for the record, I learned specifically to build this tool. For obvious reasons this will not work with a single text file created by CintaNotes, but it solved my problem and hopefully some others will find it helpful.

If you want to use it, install python and run this in the command line in the directory where you've got all your text files.

Code: Select all

#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

import os, time

xmlFile = open('notes.xml', 'w')
xmlFile.write("<?xml version=\"1.0\"?>\n<notebook>\n")

fileList = os.listdir('.')
fileList.sort()

for fileName in fileList:
     if(fileName.endswith(".txt")):
          fileText = open(fileName).read()
          fileStats = os.stat(fileName)
          fileDate = time.localtime(fileStats[8])
          fileDay = time.strftime("%Y-%m-%d", fileDate)
          fileTime = time.strftime("%H:%M:%S", fileDate)
         
          note = "<note title=\"%s\" date=\"%s\" time=\"%s\" source=\"\" link=\"\" tags=\"\">\n  <![CDATA[%s]]>\n</note>\n" % (fileName, fileDay, fileTime, fileText)
          xmlFile.write(note)
         
xmlFile.write("</notebook>\n");
User avatar
CintaNotes Developer
Site Admin
Posts: 5003
Joined: Fri Dec 12, 2008 4:45 pm
Contact:

Re: Import from Text Files

Postby CintaNotes Developer » Tue Nov 01, 2011 6:08 am

daedalus28, thanks a lot for sharing your work! Very, very nice.

CintaNotes would also probably be open-source if it was only a couple of man-days or even man-months work, - but alas, it is already more than two man-years ;)
Alex
iseemto
Posts: 5
Joined: Wed Jun 24, 2015 7:46 pm
Contact:

Re: Import from Text Files

Postby iseemto » Thu Jul 02, 2015 6:06 pm

Hello!

Thanks for this utility! Could you give me a piece of advice?..

How should the source code look to create an xml file with tags in it? Tags taken, say from *.TXT file names (or any other way).

Manually editing the xml file can work, but... an automated way would be great.

Thank you in advance!..
User avatar
CintaNotes Developer
Site Admin
Posts: 5003
Joined: Fri Dec 12, 2008 4:45 pm
Contact:

Re: Import from Text Files

Postby CintaNotes Developer » Mon Jul 06, 2015 9:29 am

Well, it will be something like this:

Code: Select all

#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

import os, time

def generateTagString(fileName):
     # TODO: implement some algorithm to generate a tag string in the form of "tag1 tag2 tag3 ..." from the file name
     # as an example, I'll just use the file name as the tag
     tags = fileName
     return tags

xmlFile = open('notes.xml', 'w')
xmlFile.write("<?xml version=\"1.0\"?>\n<notebook>\n")

fileList = os.listdir('.')
fileList.sort()

for fileName in fileList:
     if(fileName.endswith(".txt")):
          fileText = open(fileName).read()
          fileStats = os.stat(fileName)
          fileDate = time.localtime(fileStats[8])
          fileDay = time.strftime("%Y-%m-%d", fileDate)
          fileTime = time.strftime("%H:%M:%S", fileDate)
          tags = generateTagString(fileName)
         
          note = "<note title=\"%s\" date=\"%s\" time=\"%s\" source=\"\" link=\"\" tags=\"%s\">\n  <![CDATA[%s]]>\n</note>\n" % (fileName, fileDay, fileTime, tags, fileText)
          xmlFile.write(note)
         
xmlFile.write("</notebook>\n");


Note that you need to implement the function generateTagString yourself, since there can't be a unified algorithm on assigning tags.
Alex
kornac
Posts: 1
Joined: Tue Sep 26, 2017 7:02 am
Contact:

Re: Import from Text Files

Postby kornac » Wed Jun 05, 2019 10:24 am

I imply .txt by SimpleNote, in which there is an option to import .txt files.
User avatar
CintaNotes Developer
Site Admin
Posts: 5003
Joined: Fri Dec 12, 2008 4:45 pm
Contact:

Re: Import from Text Files

Postby CintaNotes Developer » Mon Jun 17, 2019 11:13 am

Very nice, thanks for the tip!
Indeed, with Simplenote you can now import not only plain text notes, but also notes from Evernote - this is great!
Here's more info on how to do it:
https://simplenote.com/2018/11/29/bring ... -importer/
Alex

Return to “CintaNotes Personal Notes Manager”