使用 OLE 从 Powerpoint 中获取文本

发布于 09-18 07:10 字数 488 浏览 7 评论 0原文

我正在尝试使用 Win32::OLE 获取幻灯片及其列表当前演示文稿的标题。

到目前为止,我可以获得

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

But $slides only has properties Application Count Parent 谁能指出我进一步采取这一行动。

我意识到一个明显的答案是不要使用 Powerpoint。公司指令等等。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

锦爱2024-09-25 07:10:09

另请参阅我对 自动化的回答工作中的工作:将 Powerpoint 项目符号文本导入 Excel 工作表

PowerPoint 幻灯片没有特定的 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:

显然,Slide4 上没有标题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文