免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2204 | 回复: 0
打印 上一主题 下一主题

perl操作ppt自动化报告 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2017-04-04 00:23 |只看该作者 |倒序浏览
win32ppt.zip (292.18 KB, 下载次数: 4)

直接上代码:
  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");
复制代码


您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP