使用 Autoit 脚本从 MS Word 文档生成 PowerPoint 演示文稿





5.00/5 (2投票s)
一个用于从结构化的 MS Word 文档生成 PowerPoint 演示文稿的程序
引言
该程序是一个 AutoIt[1] 脚本,用于从结构化的 MSWord 文档生成 PowerPoint 演示文稿。它也是一个示例,展示了我们如何使用 COM(组件对象模型)对象,特别是使用 用于 MSWord 和 PowerPoint 的 COM 对象。
对于那些对此主题感兴趣的人,我已经在之前的文章中讨论过 AutoIt 和 COM 技术:一个 Autoit 脚本,用于从 MSWord 文档中提取数据并将其发送到 Internet 应用程序。
本文涵盖以下主题:
- 如何访问 MSWord 文档
- 如何提取数据
- 如何创建(并可能启动)演示文稿
任何可以被 MS Word 读取的文档都可以生成演示文稿,前提是它的结构化为章节,并且样式为 Heading 1
、Heading 2
、Heading 3
和可能的 Title
。
MSWord 文档
Word 公开了用于管理文档的对象;如果加载了文档,该对象可以包含文档,它是一组方法和集合,如段落、表格、注释、样式等。
该应用程序处理具有文本样式和标题样式的段落,这些名称采用安装语言:例如,意大利语中的 heading
样式是 Titolo
;在下面的片段中,是一种获取本地名称的方法[2]。
...
$fl = "C:\D\Condor\Attività\CourseInformatique\JavaScriptCourse.odt"
$oDoc = ObjGet($fl) ; open an existing MSWord document
...
; for to be independent from local language we must obtain the local name using a key of the style:
$title = $oDoc.Styles(-63).NameLocal
$heading1 = $oDoc.Styles(-2).NameLocal
$heading2 = $oDoc.Styles(-3).NameLocal
$heading3 = $oDoc.Styles(-4).NameLocal
$normal = $oDoc.Styles(-1).NameLocal
$BodyText = $oDoc.Styles(-67).NameLocal
PowerPoint
整个 Microsoft PowerPoint 应用程序本质上是对象 Application
,它是 Presentations
和 slides
的集合。
Application
Presentations (collection of presentations)
Presentation
Slides (collection of slides)
Slide
通过向演示文稿添加具有特定布局的新幻灯片来生成幻灯片 Global $oPPT = ObjCreate("PowerPoint.Application")
$PPT = $oPPT.Presentations.Add(True) ; add a Presentation
...
$oSlide = $PPT.Slides.Add($index, $ppLayoutText) ; add a slide
...
每个布局都有一组可以包含数据、图表、表格等的形状;在该程序中,我使用了具有标题和副标题的布局以及具有标题和文本的布局。 可以通过设置 $PPT.SlideMaster.Background.Fill.foreColor.RGB = 51200 ; green
$PPT.SlideMaster.Background.Fill.oneColorGradient(3, 1, 1)
执行程序该程序需要 AutoIt v3.3.14.2;它从一个表单[3]开始,该表单要求提供文档名称、可能参与幻灯片构建的样式、结束时的操作以及非常简单的个性化设置。 构建规则以下是创建演示文稿的简单规则
最终操作除了保存演示文稿(文件名主体加上 .ppt)和可能保存 .pdf 副本之外,该程序还可以启动演示文稿,并使用事件 Global $endPresentation = false
Global $oPPT = ObjCreate("PowerPoint.Application")
...
$EventObject = ObjEvent($oPPT, "PWTevent_") ; Start receiving Events
...
$PPT.SlideShowSettings.Run() ; start the slide show
While Not $endPresentation ; wait end of presentation
Sleep(10)
Wend
...
Func PWTevent_SlideShowEnd($pres)
$endPresentation = True
EndFunc ;==>PWTSlideShowEnd
增强功能如果 Word 文档的结构正确,并且遵循在每个章节的第一段中指定其内容的良好做法,则无需修改 Word 文档。但是,该程序提供了通过对文档进行简单操作来干预演示文稿的机会
在第一种情况下,书签名称必须是相关标题的名称,在第二种情况下,注释必须与标题相关联。 下面是从书签和注释创建字典的程序片段,以及如何使用它。 ...
Global $commentsDict = ObjCreate("Scripting.Dictionary") ; Create a Scripting Dictionary
For $comment In $oDoc.Comments
$commentsDict.Item($comment.scope.text) = $comment.range.text
Next
For $bookmark In $oDoc.bookmarks
$commentsDict.Item($bookmark.name) = $bookmark.range.text
Next
...
If $commentsDict.Exists($parag) Then
$oSlide.Shapes(2).TextFrame.TextRange.Text = $commentsDict.Item($parag)
EndIf
...
注释
|