如何使用perl中的Win32 :: OLE从word文档中读取图片/图像

时间:2013-05-02 07:16:00

标签: perl

使用Win32 :: OLE我能够从word文档中读取表格,段落。但我想从word文档中读取图片/图片,是否有任何获取图片的功能?

以下是表格和段落阅读的代码。

$Word = Win32::OLE->GetActiveObject('Word.Application') || Win32::OLE->new('Word.Application');
$Word->{'Visible'}     = 0;
$Word->{DisplayAlerts} = 0;

my $document = $Word->Documents->Open($Req_doc_path);

### for tables reading
my $tables = $document->{'Tables'};
for my $table (in $tables){
   my $tableText = $table->ConvertToText({ Separator => wdSeparateByTabs });
   #print "Table: ", $tableText->Text(), "\n";
}

### for paragraphs reading
$paragraphs = $document->paragraphs();
$enumerate = new Win32::OLE::Enum($paragraphs);
while(defined($paragraph = $enumerate->Next()))
{

}

请帮我看看图片/图片。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

您需要InlineShapes()Shapes

#!/usr/bin/perl

use Modern::Perl;
use Win32::OLE;

my $Word = Win32::OLE->GetActiveObject('Word.Application')
  || Win32::OLE->new('Word.Application');
$Word->{'Visible'} = 0;
$Word->{DisplayAlerts} = 0;

my $document = $Word->Documents->Open("test.docx");

my $shapes = $document->InlineShapes(); #you need to process $document->Shapes() also


my $enumerate = new Win32::OLE::Enum($shapes);
while ( defined( my $shape = $enumerate->Next() ) ) {

    say "Width: ", $shape->Width();
}