Controlling MS Powerpoint via Powershell


The creation of PowerPoint files in particular can cost you a lot of nerves. Especially frequently recurring tasks like status or quarterly reports can be automated with Powershell. It is important to create a good PowerPoint master template, which can then be filled using Powershell. It makes sense to run through the document twice, inserting text modules in the first run and then adapting them to the current situation in the second run.

Creating a Powerpoint Presentation

The script will create a Powerpoint presentation which can be used as an container.

$application = New-Object -ComObject powerpoint.application
$application.visible = $True
$presentation = $application.Presentations.add()
$presentation.SaveAs("C:\YOURPATH.pptx")
$application.Quit()

Create a Powerpoint slide

The following snippet will create a Powerpoint Slide within a presentation.

$Presentationpath="C:\YOURPATH.pptx"
$Powerpoint = New-object -ComObject powerpoint.application
$presentation=$Powerpoint.Presentations.Open($Presentationpath)

$slide01 = $presentation.Slides.addSlide($presentation.Slides.Count + 1, $presentation.SlideMaster.CustomLayouts(2))

$slide01.Shapes(1).TextFrame.TextRange.Text =  "Dies ist mein Beispieltitel"

$slide01.Shapes(2).TextFrame.TextRange.Text =  "Dies ist mein Beispieltext!"

Create a shape within a presentation

Shapes can be created using the following snippet:

$Presentationpath="C:\YOURPATH.pptx"
	$Powerpoint = New-object -ComObject powerpoint.application
	$presentation=$Powerpoint.Presentations.Open($Presentationpath)
$slide01 = $presentation.Slides.addSlide($presentation.Slides.Count + 1, $presentation.SlideMaster.CustomLayouts(2))
$shape01= $slide01.Shapes.AddShape( 1,100,100, 200, 200)
	$shape01.TextFrame.TextRange.Text =  "Lalalallalaa"

Create an image within a presentation

Images can be created using the following snippet:

$Presentationpath="C:\YOURPATH.pptx"
$Powerpoint = New-object -ComObject powerpoint.application
$presentation=$Powerpoint.Presentations.Open($Presentationpath)
$slide01 = $presentation.Slides.addSlide($presentation.Slides.Count + 1, $presentation.SlideMaster.CustomLayouts(2))
$picture= $slide01.Shapes.AddPicture("c:\YOURPATH.jpg", $True, $True, 100,100, 70, 70)

Create a table within a presentation

Tables can be created using the following snippet:

$Presentationpath="C:\YOURPATH.pptx"
$Powerpoint = New-object -ComObject powerpoint.application
$presentation=$Powerpoint.Presentations.Open($Presentationpath)

$slide01 = $presentation.Slides.addSlide($presentation.Slides.Count + 1, $presentation.SlideMaster.CustomLayouts(2))

$TimeTable = $slide01.Shapes.AddTable(1,13,30,300)
$TimeTable.table.Columns.Item(1).Width = 180
for($i=2; $i -lt $14;$i++)
 {
        $TimeTable.table.Columns.Item($i).Width = 60
 }  	 

for($j=1; $j -lt $14;$j++)
 {          $TimeTable.table.Cell(1,$j).Shape.TextFrame.TextRange.Font.size=10
 }   

	$TimeTable.table.Cell(1,1).shape.TextFrame.TextRange.Text="Task"	$TimeTable.table.Cell(1,2).Shape.TextFrame.TextRange.Text="Jan"
$TimeTable.table.Cell(1,3).Shape.TextFrame.TextRange.Text="Feb"
$TimeTable.table.Cell(1,4).Shape.TextFrame.TextRange.Text="Mar"	$TimeTable.table.Cell(1,5).shape.TextFrame.TextRange.Text="Apr"	$TimeTable.table.Cell(1,6).Shape.TextFrame.TextRange.Text="May"	$TimeTable.table.Cell(1,7).Shape.TextFrame.TextRange.Text="Jun"	$TimeTable.table.Cell(1,8).Shape.TextFrame.TextRange.Text="Jul"	$TimeTable.table.Cell(1,9).shape.TextFrame.TextRange.Text="Aug"  $TimeTable.table.Cell(1,10).Shape.TextFrame.TextRange.Text="Sep"  $TimeTable.table.Cell(1,11).Shape.TextFrame.TextRange.Text="Oct"  $TimeTable.table.Cell(1,12).Shape.TextFrame.TextRange.Text="Nov"  	$TimeTable.table.Cell(1,13).shape.TextFrame.TextRange.Text="Dec"   
$Timetablelength=1

#Add row
$TimeTable.table.Rows.Add()
$Timetablelength++    
$TimeTable.table.Cell($Timetablelength,1).Shape.TextFrame.TextRange.Font.size=10        $TimeTable.table.Cell($Timetablelength,1).shape.TextFrame.TextRange.Text="Erste Zeile"

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre, wie deine Kommentardaten verarbeitet werden.

WordPress Cookie Plugin von Real Cookie Banner