这是使用Win32::OLE的示例脚本。
顺便说一句,一旦您将幻灯片转换为您可以处理的格式,您就可以在非 MS 系统上使用Spreadsheet::WriteExcel来编写输出。因此,我会推荐两个程序:一个用于转换 PowerPoint 文档,另一个用于生成 Excel 文件。
请注意,Microsoft Office 应用程序的一个极好的信息来源是对象浏览器。您可以通过工具 → 宏 → Visual Basic 编辑器访问它。进入编辑器后,点击F2浏览 Microsoft Office 应用程序提供的接口、方法和属性。
#!/usr/bin/perl
use strict;
use warnings;
use FindBin qw( $Bin );
use File::Spec::Functions qw( catfile );
use Win32::OLE;
use Win32::OLE::Const 'Microsoft PowerPoint';
$Win32::OLE::Warn = 3;
my $ppt = get_ppt();
$ppt->{Visible} = 1;
my $ppt_file = catfile $Bin, 'test.ppt';
my $doc = $ppt->Presentations->open( $ppt_file );
my $slides = $doc->Slides;
my $num_slides = $slides->Count;
for my $slide_idx (1 .. $num_slides) {
print "=== Begin Slide $slide_idx ===\n";
my $slide = $doc->Slides->Item( $slide_idx );
my $shapes = $slide->Shapes;
my $num_shapes = $shapes->Count;
for my $shape_idx (1 .. $num_shapes) {
my $shape = $shapes->Item($shape_idx);
next unless $shape->HasTextFrame;
my $pars = $shape->TextFrame->TextRange->Paragraphs;
my $num_pars = $pars->Count;
for my $par_idx (1 .. $num_pars) {
my $par = $pars->Paragraphs($par_idx,1);
print_par( $par );
}
}
print "=== End Slide $slide_idx ===\n\n";
}
sub print_par {
my ($par) = @_;
my @bullets = qw( - * > + = @ );
my $bullet_format = $par->ParagraphFormat->Bullet;
my $bullet_type = $bullet_format->Type;
my $bullet_char = '';
if ($bullet_type == ppBulletNumbered) {
$bullet_char = $bullet_format->Number . "\t";
}
elsif( $bullet_type == ppBulletUnnumbered ) {
# Need a Unicode => ASCII mapping if you want to use
# $bullet_format->Character
my $indent = $par->IndentLevel % scalar @bullets;
$bullet_char = $bullets[$indent] . "\t";
}
my $text = $par->Text;
$text =~ s/\s+$//;
print $bullet_char, $text,"\n";
}
sub get_ppt {
my $app;
eval {
$app = Win32::OLE->GetActiveObject('PowerPoint.Application');
};
die "$@\n" if $@;
unless($app) {
$app = Win32::OLE->new(
'PowerPoint.Application', sub { $_[0]->Quit }
) or die "Oops, cannot start PowerPoint: ",
Win32::OLE->LastError, "\n";
}
return $app;
}