Perl + MS Word

ziggy on 2001-03-07T02:56:55

SJ writes "Does anybody know of any modules which allow a perl program to create an MS Word formatted file? Any help would be much appreciated. Thanks."

Good question. There's Win32::OLE for Windows users, but a brief spin around CPAN is finding little else...


try richtext?

cs on 2001-03-07T08:00:12

How about creating a richtext file with RTF::Document? If a .doc format is desired you could then use Win32::Ole to convert the .rtf into a .doc using word.

Christian

Re:try richtext?

borus on 2001-03-07T12:31:23

Well, I should go with Win32::Ole all the way. I have been using it full-blown with M$ Excel and it worked great.

BTW why converting a RTF file? Word opens the file fine, any receiver (e.g. collegue) can use the document straight away

Re:try richtext?

clinton on 2001-03-07T13:55:05

I'm having a lot of success with two methods:

First, MS-Word 98/2k will read HTML (UGH!) seamlessly. The trick is not to use XHTML or exercise too much of the HTML 4.0 spec. Just really basic (and non-conforming) HTML 3.2 converts nicely. (isn't that sad?) Headers become proper document headers (H1, H2, H3) and all of the text markup seems to be making it in okay, including tables to some extent.

The RTF suggestion is a good one. I'm submitting text to my publisher in RTF which was converted from an XML markup and they're having no problems at all with the text. They open it as if it were a word document... Of course, they send it back as a real Word document (because of the revision control marks they put in) but the world can't be perfect.

Try renaming the output file.

$Bob on 2001-03-07T20:29:20

A friend of mine showed me this trick, if you change your output file.txt to file.xls, even though it's in text format, Excel will see the xls extension, realize it's a text file (which it already knows how to convert) and open the file without any problems. This may work for your situation (change the extension to .doc instead of .xls) if you don't have an fancy markup, but if you do have some fancy markup you may be better off with the HTML that someone else suggested.

Re:try richtext?

pvhp on 2001-03-09T06:10:44

OK so the docs start out in XML. If you had a bit more flexibility about the input format I would have mentioned that there are at several RTF modules on CPAN including at least two separate pod2rtf converters:

Module that have "rtf"

Perhaps one of them could have helped a situation in which .pod was an input format choice.

Output in Visual Basic Format

mattias on 2001-03-09T10:28:12

It is entirely possible to create Word-documents by using Win32::OLE, but it requires Perl to run on a Win32-machine with Office installed and there is a bit of voodo involved in figuring out how to translate what's in the manuals for Visual Basic into valid syntax for Win32::OLE.

Another approach is therefore to generate Visual Basic code in text format from your Perl-script. Then fire up word and let word run the macro that you just generated. For example, your Perl-program can generate the output:

Selection.Style = ActiveDocument.Styles("Heading 1") Selection.TypeText Text:="Hello world" Selection.TypeParagraph

To find out what the Visual Basic code should look like, turn on the Macro Recording feature in Word, create the document as you want it to look and then look at the generated macro.

I haven't yet gotten around to automating the step of actually running the macro code, but there must be some way to paste visual basic code into a Word document macro from VB code. The viruses do this all the time... /Mattias I haven't yet gotten around to