Is it possible to update a PowerPoint slide with new data (in C #)?

Are there any examples of how to refresh a PowerPoint slide (by clearing text in a specific text box and updating it with new content)?

I have a monthly report to create in PowerPoint, and I have all the data in the database. I'm trying to determine if I can just auto-generate slides by having an empty PowerPoint template with three text fields and the data will be populated from my C # code.

Other examples I would like to find are:

  • bulleted lists
  • tables

Any help in the right direction would be appreciated. I see several questions on SOF that are similar, but no one answers this question.

I assume that the easiest way is to use the OpenXML format (.pptx), as I run on a web server that might not have PowerPoint on the machine.

+3
c # powerpoint openxml
source share
3 answers

Yes, it is possible, and here is a tutorial on how to do this online. They also have sample code for a project on this blog that should help.

+2
source share

Sorry for the very late delay, hope you are still looking for this. Note that this does NOT use the SDK - it just uses System.IO.Packaging and Linq (and XML literals). Anyway, here's what to do:

  • Create a presentation. On slide 3, add 4 text fields.
  • Place the text in the three of them and enter them "Sample1", "Sample2" and "Sample3".
  • In the last text box, place two lines of text and then makes these lines bullet holes. Name it "ListSample1".

That is all you need. Then save the file and pay attention to the path and change the filePath variable below to reflect the presentation path.

Run below in the console application:

 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 with respect to VB and XML Literals, but C # should be able to do the same with some conversions.

+3
source share

You probably need to take a look at the Office Automation API. Allows you to programmatically modify, create, etc. Powerpoint docs.

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

A word of warning though, if you are using office api automation, be sure to build it against the lowest version of the office you want to support using this tool that you create in C #.

+1
source share

All Articles