View Single Post
Old 11-15-2010, 09:27 AM   #1
benluo
Junior Member
benluo began at the beginning.
 
Posts: 1
Karma: 10
Join Date: Oct 2010
Device: Kindle 3
Smile txt2html function in python

I converted txt files which include Chinese charactors in utf8 to kindle format. it makes a lot of strange signals which can not read in kindle 3. But I found that html files can be converted with no problem.

So I wrote the code to convert tx2 to html. If it can be integrate to calibre, it will be much convinced.
Quote:
import os
import os.path
import glob
import sys

def txt2html (txtfile, basefilename):
html = "<title>" + basefilename + "</title><html><body>"
outfilename = basefilename + ".html"
fin = open (txtfile, 'r')
for textline in fin:
line = "<p>" + textline + "</p>"
html = html + line
html = html + "</body> </html>"
fout = open(outfilename, "w")
fout.write(html)
fout.close()


if __name__ == "__main__":
sys.argv[1:] = [item for arg in sys.argv[1:] for item in glob.glob(arg)]
for infile in sys.argv[1:]:
basefilename = os.path.splitext(infile)[0]
txt2html(infile, basefilename)
the code is very ugly. just show some possibilities.

Best regards,

Ben Luo

Last edited by benluo; 11-15-2010 at 09:29 AM. Reason: modify codes
benluo is offline   Reply With Quote