Accrington Web

Accrington Web (https://www.accringtonweb.com/forum/index.php)
-   Tech Talk (https://www.accringtonweb.com/forum/f83/)
-   -   Setting a maximum line length (https://www.accringtonweb.com/forum/f83/setting-a-maximum-line-length-61095.html)

Thin Monkey 27-03-2012 16:53

Setting a maximum line length
 
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?

entwisi 28-03-2012 09:22

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

entwisi 28-03-2012 09:50

Re: Setting a maximum line length
 
forgot to say, you need to place the original text files in the ebooks folder you created....

Thin Monkey 28-03-2012 11:38

Re: Setting a maximum line length
 
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.


All times are GMT. The time now is 05:01.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Friendly URLs by vBSEO 3.6.1
© 2003-2013 AccringtonWeb.com