Export to Word
February 25, 2009 6:09 AM   RSS feed for this thread Subscribe

I need to take form data from an web page and export it to a Microsoft Word File. I have a bunch of form data in HTML which I can convert to XML/JSON. The users will click 'Export to Word Template 1, 2, 3, etc.' and the website will create a .doc file for them to download depending on the template / look-and-feel they select. Can I do this with non-Microsoft technologies like javascript and Python? The key requirements here is the ability to export the same data to different layouts. I have a feeling I can do this with XML / WordML, but the formatting piece looks daunting. What is the easiest way to do this without hooking into COM?
posted by kaizen to computers & internet (6 comments total)
I think the easiest way is to set up the template in Word as you wish it to look, then save the document as a web page. You can then open the file and observe what styles and HTML Word is using to recreate the template. Then, using whatever language you wish, send the .doc file down to the client containing similar styles and HTML, and you should be all set.
posted by SNACKeR at 6:31 AM on February 25


I've done a little experimenting with Python and Word docs, and the dirt-easiest thing to do (involving Python) is to use a wrapper class that simplifies the COM inputs. This one lets you do a search/replace with plain text, which might be just enough utility to let you populate a copied Word doc. Getting the variables down into python and serving up the new .doc would involve using python as a server-side script – which doesn't actually look that hard either from peeking into the googles.
posted by cowbellemoo at 6:47 AM on February 25


If you want to be slightly maverick-ey about the HTTP standard, you can actually get away with serving an HTML document as though it were a Word doc. If the client has Word installed, it will open in that application, and Word will try to render the HTML formatting properly in the document. It's not a perfect solution, but in many cases it's good enough. And what's nice is that the "Word document" you're exporting is actually usable even to people who don't have Word installed.
posted by letourneau at 7:02 AM on February 25


We used to dish out RTF files named as .doc. Word (used to) silently convert them to .DOCs and the client was never any wiser.

RTFs in Python are easier to create than .DOCs.
posted by unixrat at 7:32 AM on February 25


Well - it would be dramatically easy to do if you limitted your target result to Word 2007 as it fully supports XML and there are plenty of samples out there - no COM required.

letourneau is talking about modifying the "mime-type" of the HTTP response request to trick the browser into launching Word. I've used this trick for Excel and table-HTML output, but never Word.

But - I would agree with unixrat - RTF is your best bet.
posted by jkaczor at 9:54 AM on February 25


Yes, you can name a html file or an rtf file with the extension .doc and word will serve it up as a word doc, and the user will be none the wiser. RTF gives you more control of the layout, but HTML is easier to work with.
posted by singingfish at 2:52 PM on February 25


« Older Should a 30-something small mo...   |   Is swallowing (just a tiny bit... Newer »

You are not logged in, either login or create an account to post comments