代码之家  ›  专栏  ›  技术社区  ›  justintime

使用OLE从Powerpoint中获取文本

  •  4
  • justintime  · 技术社区  · 15 年前

    Win32::OLE 从当前演示文稿中获取幻灯片及其标题的列表。

    到目前为止我可以

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

    但是 $slides Application Count Parent 谁能指点我把这个拿远一点吗。

    我意识到一个明显的答案是不要使用Powerpoint。公司宣言之类的。

    1 回复  |  直到 15 年前
        1
  •  5
  •   Community Mohan Dere    9 年前

    另见我对 Automating a Job at Work: Importing Powerpoint Bullet Text into an Excel Sheet .

    Title Name

    #!/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: