- 论坛徽章:
- 0
|
win32ppt.zip
(292.18 KB, 下载次数: 4)
直接上代码:
- 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");
复制代码
|
|