Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Microsoft Word (.docx) documents in Ruby

Is there an easy way to create Word documents (.docx) in a Ruby application? Actually, in my case it's a Rails application served from a Linux server.

A gem similar to Prawn but for DOCX instead of PDF would be great!

like image 731
Javier Avatar asked Mar 30 '09 14:03

Javier


People also ask

How do I make a DOCX file without word?

You just have to use your browser. Install LibreOffice, a free and open-source office suite. This is an alternative to Microsoft Office. LibreOffice Writer, which is included, can open and edit Microsoft Word documents in DOC and DOCX format.

How do I make a DOCX?

Select the folder where you want to save your document. The dialog box will open > Select "Save as" > In the "Save as type" menu > Select the option "Word document (. docx)" > Click on the "Save as" button and a copy of your file will be saved in Docx format.

Can Notepad ++ read DOCX?

Notepad is a simple text editor it is not designed to open . docx files, only . txt files.


2 Answers

As has been noted, there don't appear to be any libraries to manipulate Open XML documents in Ruby, but OpenXML Developer has complete documentation on the format of Open XML documents.

If what you want is to send a copy of a standard document (like a form letter) customized for each user, it should be fairly simple given that a DOCX is a ZIP file that contains various parts in a directory hierarchy. Have a DOCX "template" that contains all the parts and tree structure that you want to send to all users (with no real content), then simply create new (or modify existing) pieces that contain the user-specific content you want and inject it into the ZIP (DOCX file) before sending it to the user.

For example: You could have document-template.xml that contains Dear [USER-PLACEHOLDER]:. When a user requests the document, you replace [USER-PLACEHOLDER] with the user's name, then add the resulting document.xml to the your-template.docx ZIP file (which would contain all the images and other parts you want in the Word document) and send that resulting document to the user.

Note that if you rename a .docx file to .zip it is trivial to explore the structure and format of the parts inside. You can remove or replace images or other parts very easily with any ZIP manipulation tools or programmatically with code.

Generating a brand new Word document with completely custom content from raw XML would be very difficult without access to an API to make the job easier. If you really need to do that, you might consider installing Mono, then use VB.NET, C# or IronRuby to create your Open XML documents using the Open XML Format SDK 1.0. Since you would just be using the Microsoft.Office.DocumentFormat.OpenXml.Packaging Namespace to manipulate Open XML documents, it should work okay in Mono, which seems to support everything the SDK requires.

like image 70
Grant Wagner Avatar answered Sep 19 '22 15:09

Grant Wagner


Maybe this gem is interesting for you.

https://github.com/trade-informatics/caracal/

It like prawn but with docx.

like image 38
Théo Capdet Avatar answered Sep 20 '22 15:09

Théo Capdet