ruby-on-rails – 在Ruby中打开.doc文件

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 在Ruby中打开.doc文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我可以打开一个.doc文件并使用 Ruby获取文件内容吗?

解决方法

宝石docx使用起来非常简单
require 'docx'

puts Docx::Document.open('test.docx')@H_404_7@ 
 

要么

d = Docx::Document.open('test.docx')
d.each_paragraph do |p|
  puts p
end@H_404_7@ 
 

您可以在https://github.com/chrahunt/docx找到它,并通过gem install docx进行安装

然而,docx不支持.doc文件(word 2007及更早版本),那么可以使用WIN32OLE:

require 'win32ole'

begin
  word = WIN32OLE.connect('Word.Application')
  doc = word.ActiveDocument
rescue
  word = WIN32OLE.new('word.application')
  path_open = 'C:\Users\...\test.doc' #yes: backslashes in windows
  doc = word.Documents.Open(path_open)
end

word.visible = true
doc.Sentences.each { |x| puts x.text }@H_404_7@
原文链接:https://www.f2er.com/ruby/272106.html

猜你在找的Ruby相关文章