本文实例讲述了Python输出Powerpoint(ppt)文件中全部文字信息的方法。分享给大家供大家参考。具体分析如下:
下面的代码依赖于windows com,所以必须在机器上安装PowerPoint才能用,可以将PPT文件中的所有纯文本信息进行输出到指定的文件,非常实用。
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | Stream Vera Sans Mono', 'Courier New', Courier, monospace !important; float: none !important; border-top-width: 0px !important; border-bottom-width: 0px !important; height: auto !important; color: rgb(0, 102, 153) !important; vertical-align: baseline !important; overflow: visible !important; top: auto !important; right: auto !important; font-weight: bold !important; left: auto !important; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;" class="py keyword">import win32com from win32com.client import Dispatch, constants ppt = win32com.client.Dispatch( 'PowerPoint.Application' ) ppt.Visible = 1 pptSel = ppt.Presentations. Open ( "c:\1.ppt" ) f = file ( "c:\1.txt" , "w" ) slide_count = pptSel.Slides.Count for i in range ( 1 ,slide_count + 1 ): shape_count = pptSel.Slides(i).Shapes.Count print shape_count for j in range ( 1 ,shape_count + 1 ): if pptSel.Slides(i).Shapes(j).HasTextFrame: s = pptSel.Slides(i).Shapes(j).TextFrame.TextRange.Text f.write(s.encode( 'utf-8' ) + "
" ) f.close() ppt.Quit() |