- 論壇徽章:
- 0
|
win32ppt.zip
(292.18 KB, 下載次數(shù): 4)
2017-04-04 00:19 上傳
點擊文件名下載附件
源文件
直接上代碼:
- 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;
- }
- #重寫創(chuàng)建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 );
- }
- }
- #重寫創(chuàng)建PPT保存并關(guān)閉方法,保存在當(dāng)前文件夾
- 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信息:設(shè)置(title body picture)位置;
- #設(shè)置字體名稱[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內(nèi)設(shè)的默認屬性
- $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內(nèi)設(shè)的默認屬性;插入圖片
- $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("結(jié)果.ppt");
復(fù)制代碼
|
|