Using OLE to get text out of Powerpoint

笑着哭i 提交于 2019-12-12 09:41:31

问题


I am trying to use Win32::OLE to get a list of slides and their titles from the current presentation.

So far I can get

    my $powerpoint = Win32::OLE->GetActiveObject('Powerpoint.Application')
    my $ap = $$powerpoint { ActivePresentation } ;
    my $slides = $$ap { slides } ;

But $slides only has properties Application Count Parent Can anyone point me to take this futher.

I realise an obvious answer is don't use Powerpoint. Corporate dictat and all that.


回答1:


See also my answer to Automating a Job at Work: Importing Powerpoint Bullet Text into an Excel Sheet.

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;
}

Output:

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

Obviously, there was no title on Slide4.



来源:https://stackoverflow.com/questions/3678663/using-ole-to-get-text-out-of-powerpoint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!