Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Changing the STDOUT Font for word
by tachyon (Chancellor) on Apr 03, 2003 at 03:46 UTC

    Easy. Use Win32::OLE. Here is an example I posted that creates a Word doc, and writes some text in different colours in a selected font (Courier New 18pt as it happens). It also shows you how to print it FWIW. There is also a bit that shows you how to look at the properties and methods available.

    use Win32::OLE; use Win32::OLE::Const 'Microsoft Word'; # start Word program die if unable to $word = Win32::OLE->new('Word.Application', sub { $_[0]->Quit; } ) or die 'Cannot start Word'; # let's watch $word->{'Visible'} = 1; # Create new document my $d = $word->Documents->Add; # define selection my $s = $word->Selection; my @lines = ( "This is test line 1", "This is test line 2", "This is test line 3", ); # $c is the color # $start is the start of Range # $end is the end of Range # $r is the Range object my ($c, $start, $end, $r) = (2, 0, 0, ); for my $line (@lines) { $end += length($line) + 1; $s->TypeText($line); # define the Range $r = $d->Range($start, $end); # Set font properties $r->Font->{Size} = 18; $r->Font->{ColorIndex} = $c++; $r->Font->{Name} = 'Courier New'; $s->TypeText("\n"); $start = $end; } # TIMTOWTDI but this will overwrite the above #my $r = $doc->{Content}; #$r->{Text} = 'Hello World!'; #$r->InsertParagraphAfter(); #$r->InsertParagraphAfter(); #$r->InsertAfter('Bye!'); # here is how to print a document $word->ActiveDocument->PrintOut({ Background => 0, Append => 0, Range => wdPrintAllDocument, Item => wdPrintDocumentContent, Copies => 1, PageType => wdPrintAllPages, }); # save the file without a prompt $word->WordBasic->FileSaveAs("c:\\test.doc"); # have a quick look at the objects and properties # don't try to Dumper the whole thing unless you have time and memory+ ++ print "Range: $_ => $r->{$_}\n" for sort keys %$r; print "Doc: $_ => $d->{$_}\n" for sort keys %$d; print "Font: $_ $r->{Font}->{$_}\n" for sort keys %{$r->{Font}}; # house keeping, clean up our instances $d->Close(); $word->Quit(); undef $word;

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      tachyon,
      Do you know if it's possible to use Perl's format in conjunction with Word?

      Thanks for the code...it is a big help!

        Yes and no. Format depends on fixed width fonts (ie Courier New and friends) to line up the stuff. Here is a table in fixed width font

        fixed | width | format ------+-------+------- foo | bar | baz

        And here is the same thing in variable width font (completely naff):

        fixed | width | format
        ------+-------+-------
        foo | bar | baz

        If you can tolerate that you would just stick the output of format into a scalar and then insert it with a fixed width font type into Word and it will all look fine. format is a legacy item and painful to use. To capture its output you need a filehandle. I would just use sprintf which can do anything format can.

        my $tmpfile = 'c:/tmp/tmp.txt'; $str = 'widget'; $cost = 10; format Something = Item: @<<<<<<<<@>>>>> $str, '$' . sprintf("%.2f",$cost) . # format is a pain in the ass and won't write to an IO::Handle so we # need to go through these contortions open TMP, "+>$tmpfile" or die $!; select(TMP); # need to select our file as the output handle for w +rite $~ = 'Something'; # now select our format write TMP; # now write it seek TMP, 0, 0; # up to the top of the file to read it select(STDOUT); # reinstate STDOUT as our ouput handle @data = <TMP>; # read in our formatted data close TMP; # clean up unlink $tmpfile; print @data; # wohoo

        To 'format' variable width fonts you need tables or perhaps columns at a pinch. Either use Word ones or Excel. Making a formatted table in Excel and then inserting this as an object into Word is much better documented than manipulating tables in Word so will be easier to do.

        cheers

        tachyon

        s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Changing the STDOUT Font for word
by belg4mit (Prior) on Apr 02, 2003 at 20:59 UTC
    This is not a Perl question, and you aren't very clear on what you mean by changing the font. If you mean changing the font face, it depends upon your terminal. I believe it is possible for certain ancient terminals, and emulators of those terminals. If you mean changing the weight... if you send your output through less a sequence like
    c^Hca^Hat^Ht
    Will show as cat (cat in bold). You can output backspace ^H using \b
    print "c\bca\bat\bt\n"

    --
    I'm not belgian but I play one on TV.

Re: Changing the STDOUT Font for word
by Abigail-II (Bishop) on Apr 02, 2003 at 22:12 UTC
    Your question is meaningless. STDOUT doesn't have "fonts". It's just a byte stream.

    Abigail

      I'm sorry, I should have made my question more clear. I'm writing to a file with a ".doc. extension, and I was wondering if it was possible to set the font in word to "Arial".

      I can write to the file but if I change the font after the fact, then my formatting is lost.

      Any ideas?

        Anonymous Monk,
        There have been many nodes here on creating Word documents and modifying existing Word documents. The consensus, as best I can tell, is that you can't easily/effectively create Word documents in Perl because the standards are terrible and vary from version to version. You can check Super Search to see if I missed something. Unless you have found a module I do not know about, it sounds like what you are doing is just opening a file with a .doc extension and writing regular text to it. There is no such thing as changing the font in this regard.

        The alternative is to create a Rich Text Formatted file that can be opened by Word and then automatically converted. This will certainly allow you to change your font. See RTF::Document for details.

        Cheers - L~R

Re: Changing the STDOUT Font for word
by crenz (Priest) on Apr 02, 2003 at 22:46 UTC

    You will need to send escape sequences that depend on your terminal to interpret them. Check out Term::ANSIColor, a module that helps with setting color and the "bold" (=lighter color) attribute on unix terminals. Might even work with DOS/Windows' ANSI.SYS.