Solved

RUBY - COUNTING WORDS IN WORD.DOC

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Implementing a Cancel button for a modal dialog in Ruby on Rails 4 1,516
Get Latest files 4 306
Ruby on Rails - IIS Windows Server 2008 2 1,019
Ruby and Rakefile 1 142
I recently rediscovered rails when I needed a holiday project and decided to build a management dashboard for the company where I work.  With it being a project done in my free time, I could focus my time on learning the basics rather than trying to…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

932 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

15 Experts available now in Live!

Get 1:1 Help Now