【问题标题】:How to read picture/image from word document using Win32::OLE in perl如何在 perl 中使用 Win32::OLE 从 word 文档中读取图片/图像
【发布时间】:2013-09-28 02:53:41
【问题描述】:

使用 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()))
{

}

#

请帮助我阅读图像/图片。 提前致谢。

【问题讨论】:

    标签: perl


    【解决方案1】:

    为此,您需要 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();
    }
    

    【讨论】:

    • 感谢您的回答。现在我能够弄清楚图片/图像。我可以读取该图片中的内容/数据吗?阅读表格中是否有类似“ConvertToText”的方法?
    猜你喜欢
    • 1970-01-01
    • 2012-10-22
    • 1970-01-01
    • 2017-05-07
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多