如何使用 Ruby 计算 Microsoft Word 文件的页数?

发布于 2024-07-23 11:50:39 字数 177 浏览 2 评论 0 原文

在 Ruby Web 应用程序中,我希望用户能够上传文档。 如果用户上传 Microsoft Word 文件 (.doc),我希望 Ruby 计算文件中的页数。 获取字数会更灵活,但获取页数就可以了。

我该怎么做呢? 有没有 Ruby 库/gem 可以帮我做到这一点? 考虑到 DOC 格式,这是否可能?

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

是你 2024-07-30 11:50:39

对文档的 Range 对象调用 ComputeStatistics() 方法:

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)

您将找到各种有关使用 Ruby 自动化 Word 的文章 此处

Call the ComputeStatistics() method on the document's Range object:

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)

You'll find various articles on automating Word with Ruby here.

云淡风轻 2024-07-30 11:50:39

在 ruby​​ 中,要打开 Word 文件,您需要使用:(

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 编写的)

然后:

word.activedocument.close( false )
word.quit

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)

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

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

Then:

word.activedocument.close( false )
word.quit
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文