http://qs1969.pair.com?node_id=719497

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

I have a Perl/Tk application to gather data. I want to produce a Word document with the results
of manipulating this data. The Word document is standard and much like a form that
can have graphics. I guess I will have to:
• create some sort of data file;
• start Word so that the contents of the data file can be used to populate a prepared Word document.
I really would appreciate some guidance about how to do this.
As ever some ‘starter’ Perl code would really be gratefully received.

Replies are listed 'Best First'.
Re: Automating Word with Perl
by polypompholyx (Chaplain) on Oct 25, 2008 at 15:47 UTC

    Win32::OLE allows you to communicate directly with Word.

    use Win32::OLE; use Win32::OLE::Const "Microsoft Word 12.0 Object Library"; use Win32::OLE::Const "Microsoft Office 12.0 Object Library"; my $word = Win32::OLE->new( 'Word.Application', 'Quit' ) or die "Can't create Word OLE: " . Win32::OLE->LastError . "\n"; $word->{'Visible'} = 1; my $doc = $word->Documents->Add or die "Can't create new Word document: " . Win32::OLE->LastError +. "\n"; my $selection = $word->Selection; # select current position $selection->{'Style'} = 'Title'; $selection->TypeText( 'Document title' ); $selection->TypeParagraph; $selection->{'Style'} = 'Normal'; $selection->TypeText( 'Lorum ipsum' ); $selection->TypeParagraph; $doc->SaveAs( 'foo.docx' ); $word->Quit;

    The Visual Basic for Applications examples you'll be able to find in the MSDN library for Office can mostly be simply converted to Win32::OLE Perl code by changing . to -> and a := b to { a => b }.

Re: Automating Word with Perl
by jplindstrom (Monsignor) on Oct 26, 2008 at 01:07 UTC
    This is what Win32::Word::Writer is for, automating away the drudgery of those common operations described in the other post about Win32::OLE.

    As for the images, I guess you'll need to create images in a format Word understands and then use the appropriate Word API methods to include them.

    If you start off with text, it may be clearer how to do that, or how to figure out how to do it.

    /J

      This looked more than interesting so I have used your link to see the doumentation on CPAN.
      However I could not see any 'functions' to
      let me find a string and change found string.
      I also 'googled' the internet with no more success.
      Could you point me to a suitable 'address'?
        From your original question, I'm not sure why you would want to do that.

        Why not start with a template Word file (with nice headers, footers, logos etc), and then write the data from the Tk app into the Word document?

        /J

Re: Automating Word with Perl
by Anonymous Monk on Oct 25, 2008 at 14:09 UTC
    Would it be simpler if you pushed the text into say a csv then used a Word's mail merge?
      I should have asked how do I automatically run Word so
      that I can use mail merge to give a new document with
      merged data?
        Now that I better understand the scope of your problem, I wonder about generating the document in RTF via RTF::Writer.
      Possibly. However, will this allow me somehow to have as part of my data a graphics image?