• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 595
  • Last Modified:

RUBY - COUNTING WORDS IN WORD.DOC

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
philsivyer
Asked:
philsivyer
1 Solution
 
Geert BormansCommented:
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
 
philsivyerAuthor Commented:
Thanks Geert
0

Featured Post

Independent Software Vendors: 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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now