Page 1 of 1

Import from Text Files

Posted: Sat Oct 29, 2011 4:28 am
by daedalus28
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");

Re: Import from Text Files

Posted: Tue Nov 01, 2011 6:08 am
by CintaNotes Developer
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 ;)

Re: Import from Text Files

Posted: Thu Jul 02, 2015 6:06 pm
by iseemto
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!..

Re: Import from Text Files

Posted: Mon Jul 06, 2015 9:29 am
by CintaNotes Developer
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.