Reputation: 196499
Are there any examples out there on how to update a PowerPoint slide (by clearing the text in a certain textbox and updating it with new content)?
I have a monthly report to generate in PowerPoint and I have all the data in a database. I am trying to determine if I can simply autogenerate the slides by having a blank PowerPoint template with three textboxes and the data would get filled in from my C# code.
The other examples i would look for are:
Any help in the right direction would be appreciated. I see a few questions on SOF that are similar but none seems to answer this question.
I assume the easiest would be to use the OpenXML format (.pptx) as i am running on a web server that may not have PowerPoint on the machine.
Upvotes: 3
Views: 12174
Reputation: 15401
Yeah this is possible and here is a tutorial on how to do it online. They also have the sample code for the project in that blog which should help.
Upvotes: 2
Reputation: 29153
Sorry for the really late delay, hope you're still looking for this. Note this does NOT use the SDK - it just uses System.IO.Packaging
and Linq (and XML Literals). Anyway, here's what to do:
That's all you need. Then save the file and note the path, and change the filePath
variable below to reflect your presentation's path.
Run the below in a Console app:
Imports System.IO
Imports System.IO.Packaging ''# Add reference to WindowsBase for this
Imports <xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main">
Imports <xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
Imports <xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
Module Module1
Public Const documentRelationshipType As String = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"
Sub Main()
Dim slide, document As XElement
Dim pptPackage As Package = Nothing
Dim slidePart, documentPart As PackagePart
Dim filePath As String = "C:\Users\Me\Documents\visual studio 2010\Projects\FillPowerPoint\FillPowerPoint\sample.pptx"
pptPackage = Package.Open(filePath, FileMode.Open, FileAccess.ReadWrite)
Using pptPackage
Dim documentRelationship As PackageRelationship = pptPackage.GetRelationshipsByType(documentRelationshipType).FirstOrDefault
Dim documentUri As Uri = PackUriHelper.ResolvePartUri(New Uri("/", UriKind.Relative), documentRelationship.TargetUri)
documentPart = pptPackage.GetPart(documentUri)
document = XElement.Load(New StreamReader(documentPart.GetStream))
Dim slideList = From e In document.<p:sldIdLst>.<p:sldId>
Dim slideIndex As Integer = 3 ''# this is the slide number we want, 1-based
Dim slideReference As String = slideList(slideIndex - 1).@r:id.ToString
slidePart = pptPackage.GetPart(PackUriHelper.ResolvePartUri(documentPart.Uri, documentPart.GetRelationship(slideReference).TargetUri))
slide = XElement.Load(New StreamReader(slidePart.GetStream))
''# Replace just text value in Sample1 textbox
Dim Sample1 = From e In slide.<p:cSld>.<p:spTree>.<p:sp> Where e.<p:nvSpPr>.<p:cNvPr>.@name = "Sample1" Select e.<p:txBody>.<a:p>.<a:r>.<a:t>.SingleOrDefault
Sample1.Value = "new text in sample 1"
''# Replace text and make bold inn Sample2 textbox
Dim Sample2 = From e In slide.<p:cSld>.<p:spTree>.<p:sp> Where e.<p:nvSpPr>.<p:cNvPr>.@name = "Sample2" Select e.<p:txBody>.<a:p>.<a:r>.SingleOrDefault
Sample2.<a:rPr>.SingleOrDefault.Add(New XAttribute("b", 1))
Sample2.<a:t>.SingleOrDefault.Value = "new bold text in sample 2"
''# Replace text and make bold inn Sample2 textbox
Dim Sample3 = From e In slide.<p:cSld>.<p:spTree>.<p:sp> Where e.<p:nvSpPr>.<p:cNvPr>.@name = "Sample3" Select e.<p:txBody>.SingleOrDefault
Sample3.<a:p>.Remove()
Dim newParagraphs As XElement = <placeholder>
<a:p>
<a:r>
<a:rPr lang="en-US" dirty="0" smtClean="0"/>
<a:t>Sample3</a:t>
</a:r>
</a:p>
<a:p>
<a:r>
<a:rPr lang="en-US" smtClean="0"/>
<a:t>With a new paragraph</a:t>
</a:r>
<a:endParaRPr lang="en-US" dirty="0"/>
</a:p>
</placeholder>
Sample3.SingleOrDefault.Add(newParagraphs.Elements)
''# Create a new list of bullets
Dim s() As String = {"Bullet 1", "Bullet 2", "Bullet 3"}
Dim ListSample1 = From e In slide.<p:cSld>.<p:spTree>.<p:sp> Where e.<p:nvSpPr>.<p:cNvPr>.@name = "ListSample1" Select e.<p:txBody>.SingleOrDefault
ListSample1.<a:p>.Remove()
ListSample1.SingleOrDefault.Add(From e In s Select <a:p>
<a:pPr marL="285750" indent="-285750">
<a:buFont typeface="Arial" pitchFamily="34" charset="0"/>
<a:buChar char="•"/>
</a:pPr>
<a:r>
<a:rPr lang="en-US" dirty="0" smtClean="0"/>
<a:t><%= e %></a:t>
</a:r>
</a:p>)
slide.Save(slidePart.GetStream)
End Using
End Sub
End Module
Sorry, I know this is heavily weighted towards VB and XML Literals, but C# should be able to do the same thing with some conversion work.
Upvotes: 3
Reputation: 544
You probably want to have a look at the Office Automation API. Will let you programatically modify, create, etc powerpoint documents.
This document is for an older version of powerpoint but the same process works for newer versions. http://support.microsoft.com/default.aspx?scid=kb;EN-US;303718
Word of warning though, if you do use office automation api make sure you build it against the lowest version of office you want to support with this tool you are creating in c#.
Upvotes: 1