如何使用Ruby计算Microsoft Word文件中的页数? [英] How do I count the number of pages in a Microsoft Word file using Ruby?

查看:80
本文介绍了如何使用Ruby计算Microsoft Word文件中的页数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Ruby Web应用程序中,我希望用户能够上传文档.如果用户上载Microsoft Word文件(.doc),我希望Ruby计算文件中的页面数.获取单词的数量甚至会比较花哨,但是页面的数量会做到这一点.

In a Ruby web application I want users to be able to upload documents. If the user uploads a Microsoft Word file (.doc) I want Ruby to count the number of pages in the file. It would be even slicker to get the number of words, but the number of pages will do.

我该怎么做?是否有可以为我做的Ruby库/gem?给定DOC格式,甚至有可能吗?

How would I do that? Is there a Ruby library/gem that can do that for me? Is it even possible, given the DOC-format?

推荐答案

在ruby中,打开单词文件需要使用:

In ruby, to open a 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)

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

请参阅: http://www.perlmonks.org/?node_id=614609 获得正确/预期字数的算法(注意:算法在perl中)

See: http://www.perlmonks.org/?node_id=614609 for an algorithm on getting the proper/expected word count (note: the algo is in perl)

然后:

word.activedocument.close( false )
word.quit

这篇关于如何使用Ruby计算Microsoft Word文件中的页数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆