Solved

RUBY - COUNTING WORDS IN WORD.DOC

Posted on 2007-11-27
2
586 Views
Last Modified: 2010-04-21
Hello
Below is my code to count lines & words in a txt file - which works. How can I modify this to count lines & words in Microsoft Word.doc file?
My attempt thus far:
lines = File.readlines("c:/Phil.txt")
lines_count = lines.size
text = lines.join
puts "#{lines_count} lines"

word_count = text.split.length
puts " there are #{word_count} words"
0
Comment
Question by:philsivyer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 20357202
you either need a library that reads word documents
or save the word files as RTF, text or XML

if you save as text, you could use your own technique,
if you save as RTF or XML, you can get the number of words and lines from the properties summary directly

If you still want to work with the binary file,
you need the win32ole module
Here is an example of its use
http://rubyonwindows.blogspot.com/2007/11/find-replace-with-ms-word.html
It must be possible to get the properties summary this way, which prevents you from having to get all words and lines yourself

cheers

Geert
0
 

Author Closing Comment

by:philsivyer
ID: 31411173
Thanks Geert
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Ruby, Call or invoke a API DLL library is easily via Win32API class, win32-api gem or other gems. For general DLL API call, there are quite a few references, some good tips list below: http://www.rubytips.org/2008/05/13/accessing-windows-api-fro…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question