Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / operating-systems / Windows

An Autoit Script to Generate a PowerPoint Presentation from MSWord Document

5.00/5 (2 votes)
19 Jul 2016CPOL3 min read 12.8K   159  
A program for generating a PowerPoint presentation from a structured MSWord document

Introduction

This program is an AutoIt[1] script for generating a PowerPoint presentation from a structured MSWord document, it is also an example of how we can work with COM (Component Object Model) objects in particular, in this case, the use of COM objects for MSWord and PowerPoint.

For those interested in the topic, I have already addressed AutoIt and COM technology in an earlier article: An Autoit script to extract data from a MSWord document and send them to an Internet application.

The article carries the following topics:

  • How to access the MSWord documents
  • How to extract the data
  • How to create, and possibly start, a presentation

Every document which can be read by MS Word can generate a presentation, provided that it is structured in chapters and the styles are Heading 1, Heading 2, Heading 3 and possibly Title.

MSWord Document

Word exposes the object for manage a document; this object that can contain the document if it is loaded, is a set of methods and collections like paragraphs, tables, comments, styles, etc.

The application deals with paragraphs that have text styles and heading styles, those names are in the language of installation: for example the heading style in Italian is Titolo; in the fragment below, a method for getting the local name[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

The entire Microsoft PowerPoint application is the object Application essentially a collection of Presentations and slides.

Application
    Presentations (collection of presentations)
        Presentation
            Slides (collection of slides)
                Slide

A slide is generated by adding a new slide to a presentation, with a specific layout:

Global $oPPT = ObjCreate("PowerPoint.Application")
$PPT = $oPPT.Presentations.Add(True) ; add a Presentation
...
$oSlide = $PPT.Slides.Add($index, $ppLayoutText)	; add a slide
...

Every layout has a set of shapes which can contain data, charts, tables, etc.; in the program I used a layout with title and subtitle and a layout with title and text.

The aesthetic of presentation can be done by setting some properties of SlideMaster object or directly on a particular slide:

$PPT.SlideMaster.Background.Fill.foreColor.RGB = 51200   ; green
$PPT.SlideMaster.Background.Fill.oneColorGradient(3, 1, 1)

Executing the Program

The program needs AutoIt v3.3.14.2; it starts with a form[3] that asks the name of the document, the possibly styles involved in the build of the slides, the action at the end and a very simple personalisation.

Building Rules

Below are the simple rules for creating a presentation:

  • Every title and heading 1 generates a slide with a list of his heading 1 or heading 2 respectively
  • Every title, heading 1 and heading 2 generates a slide with content, where the content is the first paragraph after title, heading 1 or heading 2, having style Normal or Standard
  • For title and heading 1 if the above text can’t be found, the slide is suppressed; for heading 2 are inserted the heading 3 pertaining

Final Actions

The program in addition to saving the presentation (file name body plus .ppt) and possibly save a .pdf copy, can start the presentation and wait for his term using the event SlideShowEnd:

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

An Enhancement

There is no need to adapt the Word document if it is correctly structured and one follows the good practice to specify in the first paragraph of each chapter its contents, however the program offers the opportunity to intervene on the presentation with simple operation on document:

  • Bookmarks a set of contiguous paragraphs
  • Add comments

In the first case, the bookmark name must be the name of the heading concerned, in the second the comment must be associated at the heading. Below is the fragment of program that creates a dictionary from bookmarks and comments, and how this is used.

...
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
...

Notes

  1. ^AutoIt is a free-ware BASIC-like scripting language, an alternative to PowerShell, designed in origin for automating the interaction with Windows GUI. AutoIt can run on Windows interpreted or compiled.
    It comes with many libraries that enable, among other things, access COM objects and create graphical interfaces.
  2. ^Thanks to Lene Fredborg, DocTools, www.thedoctools.com.
  3. ^The form is created by my utility formGen that you can find in this site.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)