014 Ruby PowerPoint写入Word
Hey,How are you doing?
到6月份,又要写半年总结了,
很多时候,我们一般都是在Word里写总结,
但有时,我们需要汇总其他人在PPT汇报时嵌在PPT里的总结。
这期,一起来看看,如何把内嵌在PPT里的总结写入Word。
001 需求分析
先准备一份含有内嵌文字的PPT,接着准备一个空白文档。
002 思路解析
第一步,初始化。我们生成了Word应用、PPT应用。这是一个大前提,非常重要,同时,对自增量s,幻灯片总页数进行赋值。
第二步,批量写入Word。
通过双嵌套循环,循环读取PPT中每一页的Shape,利用slides(i).shapes(j).textframe.textrange.text将其中的文字部分写入Word段落对象。
当然,你需要先通过Wdc.paragraphs.add,创建待写入的段落对象。
你要明白,我们利用Pst.slides.count得到总页数,但无法通过Pst.slides.shapes.count得到文本框总数量。
我们退而求其次,在最外层(1..m).each{|i|}循环里,动态获取文本框数量即可。
第三步,收尾。查看生成的文件是否存在问题,没问题就保存关闭即可。
003 代码展示
require 'win32ole'Pth=File.dirname(__FILE__)Wap=WIN32OLE::new('word.application');Wap.visible=trueWdc=Wap.documents.open(Pth+'/汇总.doc')Pap=WIN32OLE::new('powerpoint.application');Pap.visible=truePst=Pap.presentations.open(Pth+'/个人总结.pptx')s=1m=Pst.slides.count(1..m).each{|i| n=Pst.slides(i).shapes.count (1..n).each{|j| Wdc.paragraphs.add Wdc.paragraphs(s).range.text=Pst.slides(i).shapes(j).textframe.textrange.text s+=1 }}