利用MooseX::NonMoose::InsideOut继承非Moose模块Win32::PowerPoint。
源文件及示例:win32ppt.zip
一、依赖模块
1.1模块Win32::PPT
- Moose;
- Win32::OLE;
- MooseX::NonMoose::InsideOut;
1.2示例main
- Cwd;
- File::Spec;
- YAML::Syck;
- Data::Printer;
二、说明
- 文件名为“模板.ppt”的PPT与win32ppt.pl放在同一文件夹下
- 子文件夹img下放2个图片“3D.png”与“jx.png”
- config.yaml为配置文件,用于预设标题、正文和图片的相关属性;可由!yjconfig.pl生成
三、使用步骤
- Win32::PPT->new();
- $ppt->{config} = LoadFile($confile);
- new_ppt()or open_ppt();
- 新建幻灯片
- ==>标题与正文:slide2TB()
- ==> 标题与图片:slide2TP()
- ==> 标题、正文与图片(左右两列或上下两行格式):slide2TBBPP()
- save_ppt()
四、Code
4.1 win32ppt.pl
点击(此处)折叠或打开
- package Win32::PPT;
- use Moose;
- use Win32::OLE;
- use MooseX::NonMoose::InsideOut;
- use Data::Printer;
- extends 'Win32::PowerPoint';
- my $intro=<<'EOF';
- ########################################################
- # Win32::PPT模块
- # Author: TianYv
- # Blog: http://tianyv.github.io
- # Blog: http://blog.chinaunix.net/uid/22674875.html
- ########################################################
- EOF
- p $intro;
- has config => (
- is => 'rw',
- default => sub { {} }
- );
- has app => ( is => 'rw' );
- #通过PPT vba增加打开方法
- sub open_ppt {
- my ( $self, $file ) = @_;
- return unless defined $file;
- $self->{app} = Win32::OLE->GetActiveObject('PowerPoint.Application');
- $self->{app}->{Visible} = 1;
- my $objppt = $self->{app}->Presentations->Open($file);
- return $self;
- }
- #重写创建PPT方法
- sub new_ppt {
- my $self = shift;
- $self->new_presentation(
- background_forecolor => [ 255, 255, 255 ],
- background_backcolor => 'RGB(255,255,255)',
- );
- }
- #新建幻灯片,并输入Title和Body文字
- sub slide2TB {
- my ( $self, $texTitle, $texBody, $hT, $hB ) = @_;
- $hT = ref($hT) eq 'HASH' ? $hT : $self->{config}->{title};
- $hB = ref($hB) eq 'HASH' ? $hB : $self->{config}->{body};
- $self->new_slide();
- $self->add_text( $texTitle, $hT );
- $self->add_text( $texBody, $hB );
- }
- #新建幻灯片,并输入Title和Picture文字
- sub slide2TP {
- my ( $self, $texTitle, $img, $hT, $hP ) = @_;
- $hT = ref($hT) eq 'HASH' ? $hT : $self->{config}->{title};
- $hP = ref($hP) eq 'HASH' ? $hP : $self->{config}->{picture};
- $self->new_slide();
- $self->add_text( $texTitle, $hT );
- $self->add_picture( $img, $hP );
- }
- #$hash={texT=>,texB=>,img=>,type=>}
- sub slide2TBBPP {
- my ( $self, $hash, $hT, $hB, $hP ) = @_;
- $hT = ref($hT) eq 'HASH' ? $hT : $self->{config}->{def_tbp}->{title};
- $hB = ref($hB) eq 'HASH' ? $hB : $self->{config}->{def_tbp}->{body};
- $hP = ref($hP) eq 'HASH' ? $hP : $self->{config}->{def_tbp}->{picture};
- my ( $texTitle, $texBody, $img );
- if ( ref($hash) eq 'HASH' ) {
- $texTitle = defined( $hash->{texT} ) ? $hash->{texT} : '标题定义错误!';
- $texBody = defined( $hash->{texB} ) ? $hash->{texB} : '正文定义错误!';
- $img = defined( $hash->{img} ) ? $hash->{img} : '';
- }
- $self->new_slide();
- $self->add_text( $texTitle, $hT );
- my $type = $hash->{type};
- if ( $type == 1 ) {
- $self->add_text( $texBody, $hB );
- $self->add_picture( $img, $hP );
- }
- else {
- $self->add_picture( $img, $hP );
- $self->add_text( $texBody, $hB );
- }
- }
- #重写创建PPT保存并关闭方法,保存在当前文件夹
- sub save_ppt {
- my ( $self, $name ) = @_;
- $self->save_presentation($name);
- $self->close_presentation();
- $self->quit();
- }
- package main;
- use Cwd;
- use File::Spec;
- use YAML::Syck;
- use Data::Printer;
- my $ppt = Win32::PPT->new();
- my $dir = Cwd::getcwd();
- my $file = $dir . '/模板.ppt';
- #读取配置文件config.yaml信息:设置(title body picture)位置;
- #设置字体名称[name]、字体大小[size]、字体是否加粗[bold]、图片宽高[width | hight]
- #如$ppt->{config}->{body}->{size} 为正文字体大小
- my $confile = File::Spec->rel2abs('config.yaml');
- $ppt->{config} = LoadFile($confile);
- #p $ppt->{config};
- #$ppt->new_ppt();
- #打开模版PPT
- $ppt->open_ppt($file);
- #1.1使用config.yaml内设的默认属性
- $ppt->slide2TB( '一、测试标题', '测试正文' );
- #1.2可以改变局部属性,如$ht ,也可像$hb一样更改所有属性
- my $ht = {
- top => 40,
- size => 24,
- name => '楷体'
- };
- my $hb = {
- left => 35,
- top => 85,
- width => 625,
- height => 400,
- size => 24,
- bold => 1,
- name => '宋体'
- };
- $ppt->slide2TB( '一、测试标题', '测试正文', $ht, $hb );
- #2.1使用config.yaml内设的默认属性;插入图片
- $ppt->slide2TP( '二、测试标题图片', 'img/jx.png' );
- #2.2更改图片局部属性
- my $hp = {
- width => 200,
- height => 130,
- };
- $ppt->slide2TP( '二、测试标题图片', 'img/jx.png', $ht, $hp );
- #3.1左右2列的幻灯片,type=1,左正文+右图片
- my $h1 = {
- texT => '三、左正文+右图片',
- texB => '1234567890正文',
- img => 'img/3D.png',
- type => 1
- };
- $ppt->slide2TBBPP($h1);
- #3.2上下2列的幻灯片,type=0,上图片+下正文
- my $h2 = {
- texT => '三、上图片+下正文',
- texB => '1234567890正文',
- img => 'img/3D.png',
- type => 0
- };
- my $hb2 = {
- top => 450,
- height => 200,
- };
- my $hp2 = {
- top => 85,
- width => 400,
- height => 360,
- };
- $ppt->slide2TBBPP( $h2, $ht, $hb2, $hp2 );
- $ppt->save_ppt("结果.ppt");
4.2 !yjconfig.pl
点击(此处)折叠或打开
- package MyConfig;
- use Moo;
- use YAML::Tiny;
- has file => ( is => 'rw' );
- sub SetConf {
- my $self = shift;
- my %h = (
- 'title' => {
- left => 35,
- top => 40,
- width => 625,
- height => 50,
- size => 24,
- bold => 1,
- name => '宋体'
- },
- 'body' => {
- left => 35,
- top => 85,
- width => 625,
- height => 400,
- size => 24,
- bold => 0,
- name => '宋体'
- },
- 'picture' => {
- left => 35,
- top => 85,
- width => 625,
- height => 400,
- },
- #定义左右2列的幻灯片,左边为正文,右边为图片
- 'def_tbp' => {
- 'title' => {
- left => 35,
- top => 40,
- width => 625,
- height => 50,
- size => 24,
- bold => 1,
- name => '宋体'
- },
- 'body' => {
- left => 35,
- top => 85,
- width => 300,
- height => 400,
- size => 24,
- bold => 0,
- name => '宋体'
- },
- 'picture' => {
- left => 350,
- top => 85,
- width =>300,
- height => 400,
- }
- }
- );
- unlink $self->{file};
- YAML::Tiny::DumpFile( "$self->{file}", \%h );
- }
- package main;
- use Cwd;
- use YAML::Syck;
- use Data::Printer;
- my $dir = Cwd::getcwd();
- my $file_yml = $dir . '/config.yaml';
- my $con = MyConfig->new( file => $file_yml );
- $con->SetConf();
- #读yaml
- my $yaml = LoadFile($file_yml);
- #p $yaml->{body}->{left};
- p $yaml;