How to count the number of pages in a Microsoft Word file using Ruby?

In a Ruby web application, I want users to be able to upload documents. If the user downloads a Microsoft Word file (.doc), I want Ruby to count the number of pages in the file. It would be even easier to use the word count, but the page count will do.

How can I do it? Is there a Ruby library / gem that can do this for me? Is this even possible with the DOC format?

+3
source share
2 answers

In ruby, to open the word file you need to use:

require 'win32ole'
word = WIN32OLE.new('word.application')
word.visible = true
word.documents.count

# open/create new document
word.documents.add

# or open file
word.documents.open(path_to_file)

(source: http://www.ruby-forum.com/topic/99742#214485 )

: http://www.perlmonks.org/?node_id=614609 / (: algo perl)

:

word.activedocument.close( false )
word.quit
+1

ComputeStatistics() . Range:

require 'win32ole'

WdStatisticWords = 0
WdStatisticPages = 2

word = WIN32OLE.connect('Word.Application')
doc = word.ActiveDocument

word_count = doc.Range.ComputeStatistics(WdStatisticWords)
page_count = doc.Range.ComputeStatistics(WdStatisticPages)

Word Ruby .

+4

Source: https://habr.com/ru/post/1709901/


All Articles