且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用OLE从Powerpoint中获取文本

更新时间:2023-01-18 17:20:32

另请参阅我对 PowerPoint幻灯片没有特定的Title属性.它们具有Name属性,但这不是一回事.形状的占位符类型属性可以告诉您它是否是标题:

PowerPoint slides do not have a specific Title property. They have a Name property but that is not the same thing. A shape's placeholder type property can tell you if it is a title:

#!/usr/bin/perl

use strict; use warnings;
use Try::Tiny;
use Win32::OLE;
use Win32::OLE::Const qw( Microsoft.PowerPoint );
use Win32::OLE::Enum;

$Win32::OLE::Warn = 3;

my $ppt = get_ppt();

my $presentation = $ppt->Presentations->Open('test.ppt', 1);
my $slides = Win32::OLE::Enum->new( $presentation->Slides );

SLIDE:
while ( my $slide = $slides->Next ) {
    printf "%s:\t", $slide->Name;
    my $shapes = Win32::OLE::Enum->new( $slide->Shapes );
    SHAPE:
    while ( my $shape = $shapes->Next ) {
        my $type = $shape->PlaceholderFormat->Type;
        if ( $type == ppPlaceholderTitle
                or $type == ppPlaceholderCenterTitle
                or $type == ppPlaceholderVerticalTitle
        ) {
            print $shape->TextFrame->TextRange->text;
            last SHAPE;
        }
    }
    print "\n";
}

$presentation->Close;

sub get_ppt {
    my $ppt;

    try {
        $ppt = Win32::OLE->GetActiveObject('PowerPoint.Application');
    }
    catch {
        die $_;
    };

    unless ( $ppt ) {
        $ppt = Win32::OLE->new(
            'PowerPoint.Application', sub { $_[0]->Quit }
        ) or die sprintf(
            'Cannot start PowerPoint: %s', Win32::OLE->LastError
        );
    }

    return $ppt;
}

输出:

Slide1: Title Page Title
Slide2: Page with bullets
Slide3: Page with chart
Slide4:

很显然,Slide4上没有标题.

Obviously, there was no title on Slide4.