【问题标题】:How to Extract text from MS Word?如何从 MS Word 中提取文本?
【发布时间】:2011-10-06 05:07:00
【问题描述】:

我正在尝试打开一个 word 文档,然后提取文档中的所有文本并使用 Win32::OLE 将其显示给用户

#usr/bin/perl
#OLEWord.pl

#Use string and print warnings
use strict;use warnings;
#Using OLE + OLE constants for Variants and OLE enumeration for Enumerations
use Win32::OLE;
use Win32::OLE::Const 'Microsoft Word';

$Win32::OLE::Warn = 3;

#set the file to be opened
my $file = '/work/Test.docx';

#Create a new instance of Win32::OLE for the Word application, die if could not open the application
my $MSWord = Win32::OLE->new('Word.Application','Quit') and "Opened Word" or die     "Unable to open document ", Win32::OLE->LastError(); 
#Set the screen to Visible, so that you can see what is going on
$MSWord->{'Visible'} = 1;
#open the request file or die and print warning message
my $Doc = $MSWord->Documents->Open('C:\work\Test.docx') or die "Could not open ", $file, " Error:", Win32::OLE->LastError();

#$MSWord->ActiveDocument->SaveAs({Filename => 'AlteredTest.docx', 
                            #FileFormat => wdFormatDocument});
                            
                            
sub ShowObjs {
my $obj = shift;
foreach (sort keys %$obj) {
print "Keys: $_ - $obj->{$_}\n"; }
 }

 my $paragraphs = $Doc->Paragraphs;
 ShowObjs($paragraphs);

 #  Get and print the Text inside the opened file
 my $paragraphs = $Doc->Paragraphs;
 my $txt = $Doc->Range->Text;
 print $txt;
                            
 $MSWord->ActiveDocument->Close;
 $MSWord->Quit;

我收到此错误代码:

“Microsoft Word”的 OLE 异常:

命令失败

Win32::OLE(0.1709) 错误 ox800a1066 在 OLEWord.pl 第 20 行的 METHOD/PROPERTYGET "Open" 中

更新:我可以正常打开 Word 应用程序,只是当我尝试打开文件时出现问题

【问题讨论】:

  • 您确定文件扩展名是“doc”吗?打开失败让我觉得文件扩展名是docx,而不是doc。
  • 我把文件改成docx还是不行
  • 我可以使用上面的脚本打开 Word 文件(doc 和 docx)(但在 $txt = $Word->Paragraphs... 上会失败)。您是否有权打开该文件并且该文件尚未在 Word 中的其他位置打开?
  • 是的,我创建了用于测试的文件,我也不确定如何从单词 docuemtn 中获取文本
  • 确保文件可访问。尝试使用绝对路径。

标签: perl ole win32ole


【解决方案1】:

我有几个脚本可以使用Win32::OLE 将 DOC 转换为各种输出格式。他们通常是这样开始的:

use Win32::OLE;
use Win32::OLE::Const 'Microsoft Word';
my $wr = Win32::OLE->new('Word.Application')
    or die "Failure - word. \n";

$wr->{DisplayAlerts} = wdAlertsNone;
$wr->{Visible} = 0;

my $Doc = $wr->Documents->Open({
    FileName           => $input_file_path,
    ConfirmConversions => 0,
    AddToRecentFiles   => 0,
    Revert             => 0,
    ReadOnly           => 1,
    OpenAndRepair      => 0,
}) or exit;

...

请注意$input_file_path 必须包含文件的绝对路径。您还可以启用 VisibleDisplayAlerts 以查看 Word 可能给您带来的任何错误。

编辑:您可以使用in枚举器遍历段落:

use Win32::OLE qw(in);

...

my $paragraphs = $Doc->Paragraphs;
for my $par (in $paragraphs) {
    print $par->Range->Text();
}

或者您可以使用 Word 自己的导出方法并将文档另存为支持的格式之一:

$Doc->SaveAs({
    FileName   => 'c:\\work\\Test.txt',
    FileFormat => wdFormatEncodedText,
});

后一种方法的优点是尽可能保留格式,这样可以为项目符号、编号等产生更好的结果。

【讨论】:

  • 好的,所以它在打开文件时工作,但现在我收到此错误:尝试释放未引用标量:SV 0xb8b3c4,Perl 解释器:0x3f3d6c.?
  • @Shahab - 你能显示你正在使用的代码吗?您可以更新您的问题。
  • @Shahab - 请参阅我更新的段落遍历和导出选项的答案。使用ShowObjs 函数——请记住Win32::OLE 只是OLE 调用的薄包装,因此您不能将其视为常规哈希。奇怪的错误是常见的结果。
  • 嗯,我喜欢将其保存为 txt 文件的想法
【解决方案2】:

Win32::OLE 在交互方面可能有点搞笑。如果有任何触发提示,您可能会收到这样的消息。例如,通常它可能希望以只读方式打开文件并打开一个对话框,但这些对话框可能会在默认初始化Win32::OLE 下中断。

如果是这种情况,调用

Win32::OLE->Initialize(Win32::OLE::COINIT_OLEINITIALIZE);

在您执行任何实例化任何对象之类的操作之前(即,在Win32::OLE->new 之前)可能会成功。

【讨论】:

  • 您使用的是什么版本的 Word、Windows 和 Perl?
  • Word 2010、Windows XP 和 Perl 5.12
  • 问题是我可以通过脚本很好地打开Word。现在的问题是在 Word 打开后尝试打开文件。
  • 嗯,不容易。同时,我在 CPAN 上维护 Text::Extract::Word,它还从 Word(经典 .doc,但不是 .docx)文件中提取文本,但不运行 Word 或 OLE 来完成
  • 嗯这是一个奇怪的问题,因为我可以打开 Excel 并修改和打印文本,它只是一个字
猜你喜欢
  • 1970-01-01
  • 2010-09-12
  • 1970-01-01
  • 2012-01-05
  • 2020-04-10
  • 1970-01-01
  • 2010-11-12
  • 1970-01-01
  • 2013-12-22
相关资源
最近更新 更多