Chinaunix首页 | 论坛 | 博客
  • 博客访问: 225667
  • 博文数量: 36
  • 博客积分: 1188
  • 博客等级: 军士长
  • 技术积分: 802
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-08 21:45
文章分类

全部博文(36)

文章存档

2020年(1)

2017年(2)

2015年(1)

2014年(1)

2013年(1)

2012年(3)

2011年(27)

分类: PERL

2017-04-03 23:58:06

利用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


点击(此处)折叠或打开

  1. package Win32::PPT;
  2. use Moose;
  3. use Win32::OLE;
  4. use MooseX::NonMoose::InsideOut;
  5. use Data::Printer;

  6. extends 'Win32::PowerPoint';

  7. my $intro=<<'EOF';

  8. ########################################################
  9. # Win32::PPT模块
  10. # Author: TianYv
  11. # Blog: http://tianyv.github.io
  12. # Blog: http://blog.chinaunix.net/uid/22674875.html
  13. ########################################################
  14. EOF
  15. p $intro;

  16. has config => (
  17.     is => 'rw',
  18.     default => sub { {} }
  19. );

  20. has app => ( is => 'rw' );

  21. #通过PPT vba增加打开方法
  22. sub open_ppt {
  23.     my ( $self, $file ) = @_;
  24.     return unless defined $file;

  25.     $self->{app} = Win32::OLE->GetActiveObject('PowerPoint.Application');
  26.     $self->{app}->{Visible} = 1;
  27.     my $objppt = $self->{app}->Presentations->Open($file);
  28.     return $self;
  29. }

  30. #重写创建PPT方法
  31. sub new_ppt {
  32.     my $self = shift;
  33.     $self->new_presentation(
  34.         background_forecolor => [ 255, 255, 255 ],
  35.         background_backcolor => 'RGB(255,255,255)',
  36.     );
  37. }

  38. #新建幻灯片,并输入Title和Body文字
  39. sub slide2TB {
  40.     my ( $self, $texTitle, $texBody, $hT, $hB ) = @_;
  41.     $hT = ref($hT) eq 'HASH' ? $hT : $self->{config}->{title};
  42.     $hB = ref($hB) eq 'HASH' ? $hB : $self->{config}->{body};
  43.     $self->new_slide();
  44.     $self->add_text( $texTitle, $hT );
  45.     $self->add_text( $texBody, $hB );
  46. }

  47. #新建幻灯片,并输入Title和Picture文字
  48. sub slide2TP {
  49.     my ( $self, $texTitle, $img, $hT, $hP ) = @_;
  50.     $hT = ref($hT) eq 'HASH' ? $hT : $self->{config}->{title};
  51.     $hP = ref($hP) eq 'HASH' ? $hP : $self->{config}->{picture};
  52.     $self->new_slide();
  53.     $self->add_text( $texTitle, $hT );
  54.     $self->add_picture( $img, $hP );

  55. }

  56. #$hash={texT=>,texB=>,img=>,type=>}
  57. sub slide2TBBPP {
  58.     my ( $self, $hash, $hT, $hB, $hP ) = @_;
  59.     $hT = ref($hT) eq 'HASH' ? $hT : $self->{config}->{def_tbp}->{title};
  60.     $hB = ref($hB) eq 'HASH' ? $hB : $self->{config}->{def_tbp}->{body};
  61.     $hP = ref($hP) eq 'HASH' ? $hP : $self->{config}->{def_tbp}->{picture};
  62.     my ( $texTitle, $texBody, $img );
  63.     if ( ref($hash) eq 'HASH' ) {
  64.         $texTitle = defined( $hash->{texT} ) ? $hash->{texT} : '标题定义错误!';
  65.         $texBody = defined( $hash->{texB} ) ? $hash->{texB} : '正文定义错误!';
  66.         $img = defined( $hash->{img} ) ? $hash->{img} : '';
  67.     }
  68.     $self->new_slide();
  69.     $self->add_text( $texTitle, $hT );

  70.     my $type = $hash->{type};
  71.     if ( $type == 1 ) {
  72.         $self->add_text( $texBody, $hB );
  73.         $self->add_picture( $img, $hP );
  74.     }
  75.     else {
  76.         $self->add_picture( $img, $hP );
  77.         $self->add_text( $texBody, $hB );
  78.     }
  79. }

  80. #重写创建PPT保存并关闭方法,保存在当前文件夹
  81. sub save_ppt {
  82.     my ( $self, $name ) = @_;
  83.     $self->save_presentation($name);
  84.     $self->close_presentation();
  85.     $self->quit();
  86. }

  87. package main;
  88. use Cwd;
  89. use File::Spec;
  90. use YAML::Syck;
  91. use Data::Printer;

  92. my $ppt = Win32::PPT->new();
  93. my $dir = Cwd::getcwd();
  94. my $file = $dir . '/模板.ppt';

  95. #读取配置文件config.yaml信息:设置(title body picture)位置;
  96. #设置字体名称[name]、字体大小[size]、字体是否加粗[bold]、图片宽高[width | hight]
  97. #如$ppt->{config}->{body}->{size} 为正文字体大小
  98. my $confile = File::Spec->rel2abs('config.yaml');
  99. $ppt->{config} = LoadFile($confile);

  100. #p $ppt->{config};
  101. #$ppt->new_ppt();

  102. #打开模版PPT
  103. $ppt->open_ppt($file);

  104. #1.1使用config.yaml内设的默认属性
  105. $ppt->slide2TB( '一、测试标题', '测试正文' );

  106. #1.2可以改变局部属性,如$ht ,也可像$hb一样更改所有属性
  107. my $ht = {
  108.     top => 40,
  109.     size => 24,
  110.     name => '楷体'
  111. };
  112. my $hb = {
  113.     left => 35,
  114.     top => 85,
  115.     width => 625,
  116.     height => 400,
  117.     size => 24,
  118.     bold => 1,
  119.     name => '宋体'
  120. };
  121. $ppt->slide2TB( '一、测试标题', '测试正文', $ht, $hb );

  122. #2.1使用config.yaml内设的默认属性;插入图片
  123. $ppt->slide2TP( '二、测试标题图片', 'img/jx.png' );

  124. #2.2更改图片局部属性
  125. my $hp = {
  126.     width => 200,
  127.     height => 130,
  128. };
  129. $ppt->slide2TP( '二、测试标题图片', 'img/jx.png', $ht, $hp );

  130. #3.1左右2列的幻灯片,type=1,左正文+右图片
  131. my $h1 = {
  132.     texT => '三、左正文+右图片',
  133.     texB => '1234567890正文',
  134.     img => 'img/3D.png',
  135.     type => 1
  136. };
  137. $ppt->slide2TBBPP($h1);

  138. #3.2上下2列的幻灯片,type=0,上图片+下正文
  139. my $h2 = {
  140.     texT => '三、上图片+下正文',
  141.     texB => '1234567890正文',
  142.     img => 'img/3D.png',
  143.     type => 0
  144. };
  145. my $hb2 = {
  146.     top => 450,
  147.     height => 200,
  148. };
  149. my $hp2 = {
  150.     top => 85,
  151.     width => 400,
  152.     height => 360,
  153. };
  154. $ppt->slide2TBBPP( $h2, $ht, $hb2, $hp2 );
  155. $ppt->save_ppt("结果.ppt");

4.2 !yjconfig.pl


点击(此处)折叠或打开

  1. package MyConfig;
  2. use Moo;
  3. use YAML::Tiny;

  4. has file => ( is => 'rw' );

  5. sub SetConf {
  6.     my $self = shift;
  7.     my %h = (
  8.         'title' => {
  9.             left => 35,
  10.             top => 40,
  11.             width => 625,
  12.             height => 50,
  13.             size => 24,
  14.             bold => 1,
  15.             name => '宋体'
  16.         },
  17.         'body' => {
  18.             left => 35,
  19.             top => 85,
  20.             width => 625,
  21.             height => 400,
  22.             size => 24,
  23.             bold => 0,
  24.             name => '宋体'
  25.         },
  26.         'picture' => {
  27.             left => 35,
  28.             top => 85,
  29.             width => 625,
  30.             height => 400,
  31.         },
  32.         #定义左右2列的幻灯片,左边为正文,右边为图片
  33.         'def_tbp' => {
  34.             'title' => {
  35.                 left => 35,
  36.                 top => 40,
  37.                 width => 625,
  38.                 height => 50,
  39.                 size => 24,
  40.                 bold => 1,
  41.                 name => '宋体'
  42.             },
  43.             'body' => {
  44.                 left => 35,
  45.                 top => 85,
  46.                 width => 300,
  47.                 height => 400,
  48.                 size => 24,
  49.                 bold => 0,
  50.                 name => '宋体'
  51.             },
  52.             'picture' => {
  53.                 left => 350,
  54.                 top => 85,
  55.                 width =>300,
  56.                 height => 400,
  57.             }
  58.         }
  59.     );
  60.     unlink $self->{file};
  61.     YAML::Tiny::DumpFile( "$self->{file}", \%h );
  62. }

  63. package main;
  64. use Cwd;
  65. use YAML::Syck;
  66. use Data::Printer;

  67. my $dir = Cwd::getcwd();
  68. my $file_yml = $dir . '/config.yaml';
  69. my $con = MyConfig->new( file => $file_yml );
  70. $con->SetConf();

  71. #读yaml
  72. my $yaml = LoadFile($file_yml);
  73. #p $yaml->{body}->{left};
  74. p $yaml;


阅读(2639) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~