尝试从Word文档中提取内容时出现“Ole :: Storage :: FormatError:OLE2签名无效”的错误

6

我正在使用Rails 5。我想要从一个Word文档(.doc)中获取文本,因此我使用了这段代码:

  text = nil
  MSWordDoc::Extractor.load(file_location) do |ctl00_MainContent_List1_grdData|
    text = contents.whole_contents
  end

但我得到了下面的错误。 我在Gemfile中有这个gem。
gem 'msworddoc-extractor'

我需要做什么才能从Word文档中提取内容?如果我能够将同样的代码应用于.docx文件,就太好了。

/Users/davea/.rvm/gems/ruby-2.4.0/gems/ruby-ole-1.2.12/lib/ole/support.rb:201: warning: constant ::Fixnum is deprecated
Ole::Storage::FormatError: OLE2 signature is invalid
    from /Users/davea/.rvm/gems/ruby-2.4.0/gems/ruby-ole-1.2.12/lib/ole/storage/base.rb:378:in `validate!'
    from /Users/davea/.rvm/gems/ruby-2.4.0/gems/ruby-ole-1.2.12/lib/ole/storage/base.rb:370:in `initialize'
    from /Users/davea/.rvm/gems/ruby-2.4.0/gems/ruby-ole-1.2.12/lib/ole/storage/base.rb:112:in `new'
    from /Users/davea/.rvm/gems/ruby-2.4.0/gems/ruby-ole-1.2.12/lib/ole/storage/base.rb:112:in `load'
    from /Users/davea/.rvm/gems/ruby-2.4.0/gems/ruby-ole-1.2.12/lib/ole/storage/base.rb:79:in `initialize'
    from /Users/davea/.rvm/gems/ruby-2.4.0/gems/ruby-ole-1.2.12/lib/ole/storage/base.rb:85:in `new'
    from /Users/davea/.rvm/gems/ruby-2.4.0/gems/ruby-ole-1.2.12/lib/ole/storage/base.rb:85:in `open'
    from /Users/davea/.rvm/gems/ruby-2.4.0/gems/msworddoc-extractor-0.2.0/lib/msworddoc/extractor.rb:11:in `load'
    from /Users/davea/Documents/workspace/myproject/app/services/msword_processor_service.rb:12:in `pre_process_data'
    from /Users/davea/Documents/workspace/myproject/app/services/abstract_import_service.rb:88:in `process_race_data'
    from (irb):2
    from /Users/davea/.rvm/gems/ruby-2.4.0@global/gems/railties-5.0.1/lib/rails/commands/console.rb:65:in `start'
    from /Users/davea/.rvm/gems/ruby-2.4.0@global/gems/railties-5.0.1/lib/rails/commands/console_helper.rb:9:in `start'
    from /Users/davea/.rvm/gems/ruby-2.4.0@global/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:78:in `console'
    from /Users/davea/.rvm/gems/ruby-2.4.0@global/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
    from /Users/davea/.rvm/gems/ruby-2.4.0@global/gems/railties-5.0.1/lib/rails/commands.rb:18:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'
1个回答

6
您正在使用的宝石(gem)依赖于ruby-ole宝石。您可以在代码中看到它:
ole = Ole::Storage.open(file)

当您导入Word文档时,实际上是由ruby-ole宝石打开的。如果该宝石无法验证文件格式,则会引发异常
raise FormatError, "OLE2 signature is invalid" unless magic == MAGIC

"MAGIC 指的是 .doc 文件的头部,应该长得像这样 like this:"
# i have seen it pointed out that the first 4 bytes of hex,
# 0xd0cf11e0, is supposed to spell out docfile. hmmm :)
MAGIC = "\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1"  # expected value of Header#magic

这指的是Word文档的CFBF标题格式
BYTE _abSig[8];             // [00H,08] {0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1,
                            // 0x1a, 0xe1} for current version

你的.doc文件不是有效的Word文档,或者它是由不受ruby-ole gem支持的新版Word创建的。

我建议尝试使用几个不同的Word文档重试操作,以找到兼容的类型,然后将原始文档以该格式重新保存以再次尝试。


1
这是我从互联网上下载的Word文档。不幸的是,重新保存该文档为另一种格式并不是一个可行的选项。我需要找到一种Ruby解决方案,可以打开“.doc”文件。我能够使用Word 2010打开此文档。 - Dave
1
在UNIX/Linux系统中,您可以使用file命令,例如file your.doc,它会输出文件类型: Microsoft Word 2007+Composite Document File V2 Document, Little Endian, Os: Windows, Version 5.1, Code page: 1252。这有助于确定文件的类型。这使用相同类型的“魔术测试”来确定文件头是什么类型的文件。 - anothermh
1
嗨,那个Unix命令真的帮了我很大的忙——它让我发现我没有正确地下载文件。无论如何,如果你对MS Word解析感到非常自信,我还有另一个问题需要解决,可能会在一天左右进入悬赏状态——https://dev59.com/46Dia4cB1Zd3GeqPDWpu - Dave

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接