LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   dynamically format text and send to another programme... (https://www.linuxquestions.org/questions/linux-software-2/dynamically-format-text-and-send-to-another-programme-796918/)

comiconomenclaturist 03-21-2010 03:36 PM

dynamically format text and send to another programme...
 
I have a Word document that I want to read from and format the text dynamically to send to another programme. The document is about 12 pages long, and I need to preserve CR or EOL's, but the rest of the text will need be formatted according to variables such as characters per line and lines per page. I will need to read and print characters like %, £, ;, and : too.

I'm not sure where to start with this! Can anyone recommend an application or programming language that would be suitable? Could I do this with python, latex, or even just awk or sed, in a bash script? What would be best?

James

David the H. 03-21-2010 08:50 PM

Too vague. We'd need to see a representative sample of the input, and an example of the intended output, at the very least. The right tools to use depend a lot on the formatting of the input you have to deal with.

Also, do you need the script itself to get the text out of the word document and into plain text, or can you handle that separately? You say it's a 12-page document. Will the new formatting be on a per-page basis, or will it restructure the whole document? It's things like this that we need to know.

comiconomenclaturist 03-22-2010 04:55 AM

2 Attachment(s)
Sorry for being vague and thanks for the reply. I've attached a sample of some text from the Word document. What I want to do is input two variables into the script (characters per line and lines per page) and read as many lines as necessary to output one page. Then, when requested, I want to send the next page with the same formatting of characters per line and lines per page. (The whole text is similar to the example and will be formatted the same). Ultimately I want to display this text in fullscreen. I've also attached an example of how the output might look with line wrap at 78 characters. And finally yes, it would be possible for me to save the Word document to plain text first if necessary.

thanks for your help

David the H. 03-22-2010 07:10 AM

So really there are two operations to perform. First re-wrap the lines to the width you want, then paginate the text to the desired length, outputting one page at a time. Correct?

A couple more clarification questions here. You want to rewrap the text, but not the dashed lines separating blocks, right? Also, do the separators always separate single lines of text, or could there be more than one in each section (i.e. newlines or even blank lines). Are there any other possible formatting gotchas to worry about?


Just monkeying about for few minutes though, I came up with this for the first operation:

Code:

linewidth=78

sed 's/^_\+$/<linebreak>/' sample_text.txt | fold -sw "$linewidth" | \
sed 's/<linebreak>/__________________________________________________________________________________________/'

Formatting the bulk of the text while excluding the separators is a little challenging, so I just applied a brute-force method. I used sed to replace the separators with a substitute string, used fold to wrap the text, then converted the separator back to full length. Hey, it works. :)

Not to sure how to paginate it though, especially while controlling the timing of the output. You could use sed to break it up, and perhaps use a loop of some kind to control the timing of the output based on user input. I'd have to think about it a bit more.

It might be better to go about this with something like perl, which I personally don't know much about.

comiconomenclaturist 03-22-2010 09:28 AM

Great, thanks for this starting point. You are right about the separators - they should not be wrapped. And yes, they may separate single lines or multiple lines of text including blank lines. Also, some lines are indented within Word and it would be good to preserve this... I'm going to look at the sed commands more closely and perhaps Perl, like you suggest.

many thanks

James

chrism01 03-22-2010 07:47 PM

A couple of Perl modules

http://search.cpan.org/~gabor/Text-F...Text/Format.pm
http://search.cpan.org/~muir/Text-Ta...b/Text/Wrap.pm (simpler)

You may need to write your own code:
http://perldoc.perl.org/
http://www.perlmonks.org/?node=Tutorials


All times are GMT -5. The time now is 07:09 PM.