Register Guidelines E-Books Search Today's Posts Mark Forums Read

Go Back   MobileRead Forums > E-Book Software > Calibre


Thread Tools Search this Thread
Old 11-15-2010, 10:27 AM   #1
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.
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")

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 10:29 AM. Reason: modify codes
benluo is offline   Reply With Quote

chinese, html, python, txt

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Calibre produces Python function terminated unexpectedly roelvdh Calibre 6 09-09-2013 08:50 PM
Python error! didiyy Calibre 6 09-24-2010 03:05 AM
Python 2.6 for iLiad ericshliao iRex 14 01-28-2009 02:08 AM
Python 2.5 or 2.6? itimpi Calibre 5 01-19-2009 01:48 PM
python coding... alexxxm Sony Reader 8 01-31-2008 01:25 PM

All times are GMT -4. The time now is 01:42 AM. is a privately owned, operated and funded community.