解决方法
宝石docx使用起来非常简单
require 'docx' puts Docx::Document.open('test.docx')
要么
d = Docx::Document.open('test.docx') d.each_paragraph do |p| puts p end
您可以在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 }