View Single Post
Old 28-03-2012, 09:22   #2
entwisi
God Member
 
entwisi's Avatar
 

Re: Setting a maximum line length

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
__________________
Ian

Technical God, No 1 Geek And Linux Guru

Have you seen my Flickr pictures?

entwisi is offline   Reply With Quote