Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Help generating MS Word .doc file from perl

by schick79 (Initiate)
on May 21, 2010 at 05:51 UTC ( #841025=perlquestion: print w/replies, xml ) Need Help??
schick79 has asked for the wisdom of the Perl Monks concerning the following question:

Hello all! I thought this was going to be an easy solution to come up with, but after hours searching the web I can't seem to figure it out. I have a perl script I wrote that takes input from a web-based form and generates simple text output. I started by having the perl script just generate HTML that of course displayed on screen. Now I would like to have this perl script generate a Microsoft Word .doc document and prompt me to download it. I don't have any special formatting going into the file. It's just plain old text. How do I go about doing this? I've seen about using content-type and content-disposition, but those don't seem to be doing anything for me. Thanks much in advance for any help!
  • Comment on Help generating MS Word .doc file from perl

Replies are listed 'Best First'.
Re: Help generating MS Word .doc file from perl
by Krambambuli (Curate) on May 21, 2010 at 07:27 UTC
    What I found here - Creating Microsoft Word Document with Perl - sort of confirms what I wanted to suggest:

    maybe it's easier to generate a .rtf file, and the Microsoft world will still live happily ever after with your offering.

    Just an idea.

Re: Help generating MS Word .doc file from perl
by jethro (Monsignor) on May 21, 2010 at 08:45 UTC
    There is also RTF::Writer, if the suggestion in Krambambulis link doesn't appeal to you.

    "just generate HTML that of course displayed on screen"
    What do you mean, did you see the raw html instead or did you see a correct web page as expected? In the latter case that is all you need, just create a link to the doc-file and the browser will give you the option to download it when you click on the link

    You do know that you need a browser to look at your html-page, right? And if you want others to access that html page from other computers you need a web server like apache.

    If this doesn't help, please tell us what your problem is exactly.
Re: Help generating MS Word .doc file from perl
by Marshall (Abbot) on May 21, 2010 at 10:43 UTC
    I don't have any special formatting going into the file. It's just plain old text. How do I go about doing this?

    There are some ways to use Win32:OLE and other techniques to generate a fancy MS .doc file. But from the above, you say that this is just plain old text. To me that means no fancy formatting, font selections, etc.

    If you really just have "plain old text", then just make file: plain_old_text.doc with some plain old text in it. I just renamed one of my .pl programs to .doc as a test case. When WinWord opens plain_old_text.doc, it will say that it is plain text and user just has to click "yes" to read it (does a conversion). default fonts wind up on my machine as Courier New, 10 pt. So if it is indeed just text, Winword will read that "sort of .doc file" and convert it to a "real" .doc file just like as an analogy, Excel can import CSV files.

    The WinWord.doc format is complex and I don't think that there are any options for a "real" .doc file that can be read by multiple years of WinWord other than using a version WinWord to make it (WinWord can be controlled by Perl but I've never actually done it myself).

    Update: I did get a transient cannot open mswrd632.wpc error when I did the above experiment, but WinWord 2000 asked for my CD and became happier after that. Anyway my .pl file renamed to a .doc file did open in WinWord 2000.

      Krambambuli - I also agree, an rtf would be just fine in my case. I just want to make sure that this process spawns something that will open in Word by default. I'm sure my users can go through the Save As and change it to an actual Word file after they are all done with it. Unfortunately, the code referenced in that page is no longer available. :(

      jethro - What I meant was that it would just dynamically generate html which was displayed on screen just like a regular web page. No file was bring created or anything, it just shot back the text I wanted as html. The formatting that I wanted (only referring to extra line breaks) was via html tags. There are no font changes, or anything else. The web page output is as expected. This was only a temporary measure as I knew what I wanted the end result to look like but didn't know how to generate the Save As prompt for the download, and I figured that getting to that point wouldn't require much difference in the code (hoping I'm correct on that!) Although, I do think I get your point. Instead of generating a doc/rtf file to be downloaded, I could just write to the server and provide a link on the html output screen, and the user could just right-click and Save Target As, or click and open it directly, correct? This will be hosted on a web server and all users will have access. (Note: I would actually much prefer to create some sort of souped-up Word document that does all of it internally, but I don't know how to program like that. I do have a good grasp on perl which is why I went this route instead.)

      Marshall - I'm assuming you are referring to the same thing as above in writing the file to the web site and providing a link. I might go that route. Just out of curiosity, where would be a good place to go to learn about that Win 32 OLE thing? Just in case I do want to get fancy with it.

      Thanks much, all!

        Do a google on Win32:OLE, one site Win32_Perl_Modules. Would also try the Super Search on Monks and would also pay attention to the Active State site. Basically you will find lots of "stuff", which of that "stuff" is the "best stuff", I can't say! I am confident that it is possible for Perl to control WinWord, but I would not underestimate the complexity of the details involved (see below)

        What I've described is similar to what will happen with say an RTF file that WordPad generated. WinWord can read those things.

        I'm not sure that I understand exactly what you mean by "dynamic". Generation of a .doc file upon a new release of a text file every week is quite a different thing than generating a .doc file that continually changes every second based say upon current weather conditions.

        I have generated fancy reports in WinWord from text files before, but not completely automated. The way I did it was create a Word Macro such that I just had to open the text file in Word, then hit say CTL-R (for report) to run the Word macro and then save the resulting file...3 manual steps...took a minute for me to do. This didn't matter as I had to also prepare a management presentation about what the data meant which took hours!

        Now that I think about it, this worked amazingly well! I had control of the format of the text file and that enabled me to avoid writing any WinWord BASIC macro code at all! I did it all with just keyboard macro recorder and use of a Word template file that I made...Search for X, change font to Y, assign heading numbering, let's change landscape instead of portrait mode, etc.

        If you are with my thinking so far, the idea is to NOT to make a Winword doc "from scratch" using Perl to control Word. Make a template and macros that you need within Word so that you can with just a few key strokes make a fancy looking report from the text input. THEN use Perl to automate those very few commands that you can do easily by hand - it may come down to just opening Word and running a single macro. Avoid all of this detailed WinWord Visual Basic stuff to the maximum extent possible.

        Anyway, that is what my thinking would be as a first approach to a fancy document. But it doesn't sound like you need a "fancy document". WinWord can open a plain text or RTF file and import if the user just clicks that it is "ok" to do that.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://841025]
Approved by Marshall
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2018-05-22 20:48 GMT
Find Nodes?
    Voting Booth?