Setting a maximum line length

Thin Monkey

Local
Weaver
I have an MP4 player which can display e-books but only as text files (.TXT).

I have software to convert files to that format but, unfortunately, the converted files do not wrap properly on the screen. Instead of line breaking where a line is too long for the screen, words continue across the screen edge, so that the first few letters are on one line and the remainder on the next.

I want to format the files so that they suit the screen width i.e. line break every 30 characters.

I've tried numerous text editors to no avail. Any ideas?
 
Geek Warning!!!!!!

Python can do this easily in scripting. install python from Python 3.2.2 Release

create a folder on your C drive called ebooks ( i tested under Linux obviously but Python is Platform agnostic so should be OK)

save the following as ebook.py in it altering the text inside the quotes to the name of your ebook text file ( e.g. howtoprograminpython.txt to sausagesaretasty.txt & howtoprograminpython_fixed.txt to sauasgesaretasty_fixed.txt)

Code:
def word_wrap(string, width=80, ind1=0, ind2=0, prefix=''):
   
    string = prefix + ind1 * " " + string
    newstring = ""
    while len(string) > width:
        # find position of nearest whitespace char to the left of "width"
        marker = width - 1
        while not string[marker].isspace():
            marker = marker - 1

        # remove line from original string and add it to the new string
        newline = string[0:marker] + "\n"
        newstring = newstring + newline
        string = prefix + ind2 * " " + string[marker + 1:]

    return newstring + string
f=open('howtoprograminpython.txt', 'r')
o=open('howtoprograminpython_fixed.txt', 'w')

string=f.read()
o.write(word_wrap(string,30))

run it on a command line from the folder that you are in with

Code:
python ebook.py

repeat for all you want to fix
 
forgot to say, you need to place the original text files in the ebooks folder you created....
 
Thanks for the advice.

It's a bit daunting, I'm no programer, but I would have given it a try. However I've managed to find a more idiot proof solution using "Text Mechanic".

Add/Remove Line Breaks

A little long-winded perhaps, buy I understand much better what I'm doing.

Thanks again.
 
Back
Top