How to create table in PowerPoint slide using powershell?

This topic contains 1 reply, has 1 voice, and was last updated by Profile photo of Mohammed Elsharkawy Mohammed Elsharkawy 5 months, 2 weeks ago.

  • Author
    Posts
  • #43891
    Profile photo of Mohammed Elsharkawy
    Mohammed Elsharkawy
    Participant

    Dear all
    How can i create powerpoint slides containing table using powershell?
    i can create table in word using this code, but i need same for powerpoint

    $app=New-Object -ComObject 'word.Application'
    $app.Visible=$False
    $doc=$app.Documents.Add()
    $Range=$doc.Range()
    $Number_Of_Rows=3
    $Number_Of_Columns=5
    $doc.Tables.Add($Range,$Number_Of_Rows,$Number_Of_Columns)|Out-Null
    $Table=$doc.Tables.item(1)
    $Table.Cell(1,1).Range.Text="Header 1"
    $Table.Cell(1,2).Range.Text="Header 2"
    $Table.Cell(1,3).Range.Text="Header 3"
    $Table.Cell(2,1).Range.Text="Row content 1"
    $Table.Cell(2,2).Range.Text="Row content 2"
    $Table.Cell(2,3).Range.Text="Row content 3"

    $Table.AutoFormat(9)

    $file='C:\tempFiles\doc1.docx'
    $doc.SaveAs([ref]$file)
    $doc.close()
    $app.quit()
    [System.Runtime.interopservices.Marshal]::ReleaseComObject($doc)|Out-Null
    [System.Runtime.interopservices.Marshal]::ReleaseComObject($app)|Out-Null
    [System.Runtime.interopservices.Marshal]::ReleaseComObject($Range)|Out-Null
    [System.Runtime.interopservices.Marshal]::ReleaseComObject($Table)|Out-Null

    Remove-Variable doc,app,Range,Table
    [gc]::collect()
    [gc]::WaitForPendingFinalizers()

    Regards
    Mohammed Sharkawy

  • #44294
    Profile photo of Mohammed Elsharkawy
    Mohammed Elsharkawy
    Participant

    Hello
    i found the required code for this :

    Add-type -AssemblyName office
    Add-Type -AssemblyName microsoft.office.interop.powerpoint
    [object] $powerPoint = New-Object -ComObject PowerPoint.Application
    $powerPoint.Visible = [Microsoft.Office.Core.MsoTriState]::msoTrue
    [object] $presentation = $powerPoint.Presentations.Add($true)
    
    $presentation.ApplyTheme("C:\tempFiles\Theme1.thmx")
        $Number_Of_Rows=2
        $Number_Of_Columns=8
    	[object] $Table = $Slide.Shapes.AddTable($Number_Of_Rows,$Number_Of_Columns,0,30)
    	
    	$Table.Table.Columns.Item(1).Width = 100
       $Table.Table.Columns.Item(2).Width = 90
       $Table.Table.Columns.Item(3).Width = 90
       $Table.Table.Columns.Item(4).Width = 80
       $Table.Table.Columns.Item(5).Width = 80
       $Table.Table.Columns.Item(6).Width = 90
       $Table.Table.Columns.Item(7).Width = 100
       $Table.Table.Columns.Item(8).Width = 90
       
       $Table.table.Cell(1,1).Shape.TextFrame.TextRange.Font.size=14
       $Table.table.Cell(1,2).Shape.TextFrame.TextRange.Font.size=14
       $Table.table.Cell(1,3).Shape.TextFrame.TextRange.Font.size=14
       $Table.table.Cell(1,4).Shape.TextFrame.TextRange.Font.size=14
       $Table.table.Cell(1,5).Shape.TextFrame.TextRange.Font.size=14
       $Table.table.Cell(1,6).Shape.TextFrame.TextRange.Font.size=14
       $Table.table.Cell(1,7).Shape.TextFrame.TextRange.Font.size=14
       $Table.table.Cell(1,8).Shape.TextFrame.TextRange.Font.size=14
       
       $Table.table.Cell(2,1).Shape.TextFrame.TextRange.Font.size=12
       $Table.table.Cell(2,2).Shape.TextFrame.TextRange.Font.size=12
       $Table.table.Cell(2,3).Shape.TextFrame.TextRange.Font.size=12
       $Table.table.Cell(2,4).Shape.TextFrame.TextRange.Font.size=12
       $Table.table.Cell(2,5).Shape.TextFrame.TextRange.Font.size=12
       $Table.table.Cell(2,6).Shape.TextFrame.TextRange.Font.size=12
       $Table.table.Cell(2,7).Shape.TextFrame.TextRange.Font.size=12
       $Table.table.Cell(2,8).Shape.TextFrame.TextRange.Font.size=12
       
       #$Table.table.Cell(1,1).Shape.TextFrame.TextRange.Font.Bold = 'msoTrue'
       $Table.table.Cell(1,1).shape.TextFrame.TextRange.Text="CRQ"
       $Table.table.Cell(1,2).Shape.TextFrame.TextRange.Text="Change-Deployment Summary"
       $Table.table.Cell(1,3).Shape.TextFrame.TextRange.Text="Change Description"
       $Table.table.Cell(1,4).Shape.TextFrame.TextRange.Text="Benefits"
       $Table.table.Cell(1,5).Shape.TextFrame.TextRange.Text="Business Impact"
       $Table.table.Cell(1,6).Shape.TextFrame.TextRange.Text="Risk"
       $Table.table.Cell(1,7).Shape.TextFrame.TextRange.Text="Business/Service Owner Approvals"
       $Table.table.Cell(1,8).Shape.TextFrame.TextRange.Text="Hypercare Approval"
       
       $file='C:\tempFiles\ccc3.pptx'
       $presentation.SaveAs($file)
    

You must be logged in to reply to this topic.