How to create a powerpoint table using powershell - powershell

I'm currently trying to create a calendar in powerpoint using powershell. All I want to do is insert a table into a powerpoint slide. This table is representing the month of January, it contains the days of the week etc.
I did some research and came across this.
This is VB script, so i tried to "create its equivalent" in powershell:
EDIT3: I was finally able to copy my table from Excel and paste it into my powerpoint slide using this code:
#Create an instance of Excel.
$xl=New-Object -ComObject "Excel.Application"
$xl.Visible = $True
#Open the Excel file containing the table.
$wb = $xl.workbooks.open("C:\January.xls")
$ws = $wb.ActiveSheet
#Select the table.
$range = $ws.range("A1:G7")
$range.select()
#Copy the table to the clipboard.
$range.copyPicture()
#Create an instance of Powerpoint.
$objPPT = New-Object -ComObject "Powerpoint.Application"
$objPPT.Visible ='Msotrue'
#Add a slide to the presentation.
$project = $objPPT.Presentations.Add()
$slide = $project.Slides.Add(1, 1)
#Paste the table into the slide.
$shape = $slide.Shapes.Paste()
#Position the table.
$shape.Left = 50
$shape.Top = 150
$shape.Width = 300
$shape.Height = 168
Thanks to those who have helped me here and on #powershell

I saw your question in the channel.
This worked for me:
$presentation = $ppt.Presentations.Open($ifile)
$sl = $presentation.Slides.Add(1, $ppLayoutBlank)
$shape = $sl.shapes.paste()

I think you can use this:
# Optionally, make sure you're on the last slide:
$ppt.ActiveWindow.View.GotoSlide( $ppt.ActivePresentation.Slides.Count )
# Specify the
$ppt.ActiveWindow.View.PasteSpecial( "ppPasteOLEObject", "msoFalse", "", 0, "", "msoFalse")
See the
MSDN Interop Docs
and thanks to this example:
Paste Excel Chart into Powerpoint using VBA

Related

PowerShell - How can I select the last row of the last table in a Word document, copy it, and insert as a new row at the bottom of the table?

I am trying to increase the level of automation within my day-to-day work, part of which involves adding a line to the end of a table within a report that contains largely the same information, with a few cells changed (new dates).
I have a little experience with VB and C++, but I am very much an amateur when it comes to PowerShell, which seems to be the go-to for task automation.
I have a couple of PowerShell scripts that search through the body of the report and change text, but the last part of the report is a record, and needs appending as opposed to amending.
How would I go about this?
I have tried mangling a few bits of PowerShell code I've found online, to no avail. I have gotten as far as selecting a row in the correct table, but I have no idea how I might then select the last row, copy this and insert it beneath as a new row at the bottom of the table:
$objWord = New-Object -ComObject word.application
$objWord.Visible = $True
$objWord.Documents.Open("FILEPATH")
$FindText = "KEYTEXT"
$objWord.Selection.Find.Execute($FindText)
$objWord.Selection.SelectRow()
Here's a lengthy example with explanations that does what you're looking for:
# open document
$objWord = New-Object -ComObject word.application
$objWord.Visible = $True
$doc = $objWord.Documents.Open("C:\temp\temp.docx")
# search for row
$FindText = "Value2"
$result = $objWord.Selection.Find.Execute($FindText)
$objWord.Selection.SelectRow()
# copy the ranges from searched row
$table = $objWord.Selection.Tables[1]
$copiedCells = $objWord.Selection.Cells | select columnindex,rowindex
# add row at the end of table
$table.Rows[$table.Rows.Count].Select()
$objWord.Selection.InsertRowsBelow(1)
# insert copied text into each column of last row
foreach ($cell in $copiedCells) {
# copy value from cell in copied row
$copiedText = $table.Cell($cell.rowindex, $cell.columnindex).Range.Text
# remove last 2 characters (paragraph and end-of-cell)
$TrimmedText = $copiedText.Remove($copiedText.Length - 2)
# set value for cell in last row
$table.Cell($table.Rows.Count,$cell.columnindex).Range.Text = $TrimmedText
}

Powershell GUI Imported text file missing newline/carriage return

I have a powershell GUI which imports a text file and displays it in a textbox when a button is clicked
But even though the text file contains one entry per line when it gets displayed in the textbox it is all on one line...
The text file looks like this-
But when I import it it looks like this-
This is the code I am using-
$button_hosts = New-Object system.windows.Forms.Button
$button_hosts.Text = "Hosts"
$button_hosts.Width = 60
$button_hosts.Height = 25
$button_hosts.location = new-object system.drawing.point(20,55)
$button_hosts.Font = "Microsoft Sans Serif,10"
$mydocs = [Environment]::GetFolderPath('MyDocuments')
$button_hosts.Add_Click({
$textBox_hosts.Text = Get-Filename "$mydocs" txt
$textBox_hostlist.Text = Get-Content $textBox_hosts.Text
})
$GUI.controls.Add($button_hosts)
Any idea how to get it to display the same? I cant add any extra data to the txt file as it is an output from another program
Set the lines property, not the text property.
$textBox_hostlist.Lines = Get-Content $textBox_hosts.Text
Get-Content reads the content one line at a time and returns a collection of objects, each of which represents a line of content.
The means that you have to join the collection with carriage returns and linefeeds:
(Get-Content $textBox_hosts) -Join "`r`n"
For your WinForms TextBox, do you have the multiline property set to true?
https://msdn.microsoft.com/en-us/library/12w624ff(v=vs.110).aspx
If not, it defaults to single line.

Aligning Column Text in MS Word Table Set generated with Powershell

I am a beginner in Powershell scripting and facing this problem:
I generated a set of tables in a Word document.
At the end I like to align for each table the texts in all cells of the second column vertically centered. I tried to use a code structure like:
$document.Tables | ForEach-Object {...}
However I find no way to get the hands on the individual column of each table to align the texts.
What would be a possible way?
Found no answer so far. So I ended up with the following:
$word = New-Object -ComObject word.application
$document = $word.documents.open($wordTemplatePath)
...
For ($i = 1; $i -le $document.Tables.Count; $i++) {
$a = $document.Tables.Item($i).Columns.Item(2).Select()
$objSelection = $word.Selection
$objSelection.ParagraphFormat.Alignment = "wdAlignParagraphCenter"
$objSelection.Cells.VerticalAlignment = 1
}
Works fine.
To justify the whole table you can use:
$table.Range.ParagraphFormat.Alignment=1
To do just a cell:
$table.Cell(1,1).Range.ParagraphFormat.Alignment=1
Other values are 0 left (default) and 2 right
A combination of these should have your table looking perfect.

Parse HTML Table in PowerShell V3

I have the following HTML table Link To the HTML
I want to parse it and convert it to XML/CSV/PS Object,
I tried to do with HtmlAgilityPack.dll but no success.
Can anybody give me any directions to do it?
I want to convert the table to a PSObject and export it to csv,
I currently have just the beginning of the code,
and access to the lines but i can't access to the values in the lines
Add-Type -Path C:\Windows\system32\HtmlAgilityPack.dll
$HTML = New-Object HtmlAgilityPack.HtmlDocument
$res = $HTML.Load("C:\Test\Test.html")
$table = $HTML.DocumentNode.SelectNodes("//table/tr/td/nobr")
when i access to $table[0..47].InnerHtml i get only the first ** column ** of the file,
i can't access to the 2nd and etc
Thanks Ohad
you can try this to get all the html in <nobr> tags. I let you find the logic to output what you want...
$ie = new-object -com "InternetExplorer.Application"
$ie.navigate("http://urltoyourfile.html")
$doc = $ie.Document
($doc.getElementsByTagName("nobr"))|%{$_.innerHTML}
Output :
Lead User
Accesses
Last Accessed
Average
Max
Min
Total
amirt</NO br>
2
01/20/2013 09:40:47
04:18:17
06:19:26
02:17:09
08:36:35
andream
1
01/20/2013 10:33:01
02:34:37
02:34:37
02:34:37
02:34:37
avnerm
1
01/17/2013 11:34:16
00:30:44
00:30:44
00:30:44
00:30:44
brouria
a way to parse it :
($doc.getElementsByTagName("nobr"))|%{
write-host -nonew $_.innerHTML";"
$cpt++
if ($cpt % 8 -eq 0){$cpt=1;write-host ""}
}

Read word document (*.doc) content with tables etc

I have a word document (2003). I am using Powershell to parse the content of the document.
The document contains a few lines of text at the top, a dozen tables with differing number of columns and then some more text.
I expect to be able to read the document as something like the below:
Read document (make necessary objects etc)
Get each line of text
If not part of a table, process as text and Write-Output
else
If part of a table
Get table number (by order) and parse output based on columns
end if
Below is the powershell script that I have begun to write:
$objWord = New-Object -Com Word.Application
$objWord.Visible = $false
$objDocument = $objWord.Documents.Open($filename)
$paras = $objDocument.Paragraphs
foreach ($para in $paras)
{
Write-Output $para.Range.Text
}
I am not sure if Paragraphs is what I want. Is there anything more suitable for my purpose?
All I am getting now is the entire content of the document. How do I control what I get. Like I want to get a line, be able to determine if it is part of a table or not and take an action based on what number table it is.
You can enumerate the tables in a Word document via the Tables collection. The Rows and Columns properties will allow you to determine the number of rows/columns in a given table. Individual cells can be accessed via the Cell object.
Example that will print the value of the cell in the last row and last column of each table in the document:
$wd = New-Object -ComObject Word.Application
$wd.Visible = $true
$doc = $wd.Documents.Open($filename)
$doc.Tables | ForEach-Object {
$_.Cell($_.Rows.Count, $_.Columns.Count).Range.Text
}