Solved

RUBY - COUNTING WORDS IN WORD.DOC

Posted on 2007-11-27
2
589 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

Article by: narshlob
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

687 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