How to read Microsoft document file in ruby on rails ?

Hello Everyone,
     I m looking for parsing doc/docx file in ruby on rails.
     I have use'filename','r'), but it shows special character instead of the content of file .

If all you want is the text content of the files, you can try the ancient Unix utility catdoc to do that. Just back-tick to that command (and make sure it's installed in your Web server's path). The result will not be pretty, but it will have all of the words in it.


The docx format is actually pretty simple: it is a zipped set of
files. If you upload it to the server and unzip it, you'll see a set
of xml files. You can poke around and figure out the format, or you
can find a spec on line.

You are really cruel to toy with him like that :wink:

Walter Lee Davis , Paul

Please can u give some code snipet or give some more clarification about parsing doc file.

Did you try googling? This was the third link I found.

Dheeraj Kumar

Use of PDFTron may useful. google for “PDFTron Ruby Intigration”

For a start, here's the man page for catdoc, which you will need to install.

Then, read up on using the system() or backtick operators in a Ruby script to engage it. You'll need to have a path to the file you want to process, which is highly dependent on the system you're using to store the files. In Paperclip, I made this processor to extract text from PDF files (pdftotext is part of the same collection of utilities as catdoc, I believe):


module Paperclip
  # Handles extracting plain text from PDF file attachments
  class Text < Processor

    attr_accessor :whiny

    # Creates a Text extract from PDF
    def make
      src = @file
      dst =[@basename, 'txt'].compact.join("."))
      command = <<-end_command
        "#{ File.expand_path(src.path) }"
        "#{ File.expand_path(dst.path) }"

        success ="/usr/bin/pdftotext -nopgbrk", command.gsub(/\s+/, " ")) "Processing #{src.path} to #{dst.path} in the text processor."
      rescue PaperclipCommandLineError
        raise PaperclipError, "There was an error processing the text for #{@basename}" if @whiny

Depending on how you are uploading your files, your mileage may vary. At the very simplest, the command would be

text_contents = system('/usr/bin/catdoc /root/relative/path/to/file.doc')

But that's hopelessly naive and will blow up on any error.


Hello Everyone,

Thanks everyone.Finally got a solution while searching things that you all had explained.

There is a docx gem for parsing docx file and docx-html for convert it into HTML.

require ‘docx’

d ='example.docx')
d.each_paragraph do |p|
  puts d

and for the docx file stored on s3 amazon.‘http://S3-URL/original.docx’,:ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE))

A big Thanks to All.