Solved

RUBY - COUNTING WORDS IN WORD.DOC

Posted on 2007-11-27
2
577 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
2 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
Comment Utility
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
Comment Utility
Thanks Geert
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now