Opening .Doc Files in Ruby

Read MS Word .doc file with ruby and win32ole

Don't know the difference between rails c and rails, so I'll give some random advise.

First, it is a bad idea to run this in a webserver, each time Word is run on the server, so what happens if multiple users start using this at the same time ?

You'd better convert your .doc files to another format first like .rtf or .docx (a batch conversion ?) and then use other gems that don't require Word itself.

If you keep it like this, consider to not close word (remove the word.quit) buit only close the document itself, the instance will be picked up the next time by the WIN32OLE.connect

While testing you'de better keep word visible so that you can better see what is happening (errors ?).
I notice your path uses forward slashes while in this case backslashes are needed but since your code runs a few times before the error i suppose that is not the problem.

Hope this helps.

Parse doc and xls files in ruby

You can parse different types of spreadsheets using the Roo gem. It supports:

  • OpenOffice
  • Excel
  • Google spreadsheets
  • Excelx
  • LibreOffice
  • CSV

From my experience it has some issues with parsing .xls files, however parsing .xlsx files is good.

As for .doc files, you may try using msworddoc-extractor gem or try one of the solutions proposed here.

Update: working with *.docx files - docx and docx-html

Display .doc .docx in browser

Thanks for answering but after many research, I finally found :

https://view.officeapps.live.com/op/view.aspx?src=

which work as well as the pdf reader from browser.

Be sure to have a public url for the file you want to display

Creating Microsoft Word (.docx) documents in Ruby

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.

How to open file in default application. Ruby

This should work (untested, as I'm not on a Windows machine now):

file_to_open = "c:\path\to\file.txt"
system %{cmd /c "start #{file_to_open}"}

For reference, this can also be done on OS X:

file_to_open = "/path/to/file.txt"
system %{open "#{file_to_open}"}


Related Topics



Leave a reply



Submit