none
Updating or inserting a text in powerpoint slidemaster RRS feed

  • Question

  • Hi All

    I have a MS Powerpoint 2010 presentation. It has got slide master as well. The slide master displays a set of table structure to show some common information. So, it has a table with rows and cells. I intend to programatically update the existing table cell values in slide master.

    I tried various approaches but unsuccessful -

    1. Tried to update the inner text -

     PresentationPart presPart = myPres.PresentationPart;
    
                    SlideMasterPart sectionSlidePart = (SlideMasterPart)presPart.GetPartById("rId1");
    
                    CommonSlideData sldData = sectionSlidePart.SlideMaster.CommonSlideData;
    
    sldData.InnerText = "MyText";

    However, I get error message that inner text cannot be updated.

    2. I tried to loop through and find individual table cells but still the same issue of inner text.

     

    I am new to open xml formats and hope someone can point me in right direction.#

    Many Thanks

    Best Regards

    Amit

     

     

    Saturday, December 3, 2011 7:37 PM

Answers

  • Hi Amit,

    Thank you for posting.

    I have tried on my side and found that we need to get the Text property and set its value, below is the code snippet for you to reference:

    using System;
    using System.Linq;
    using System.IO;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Presentation;
     
    namespace OpenXmlPowerpointUpdateText
    {
        class Program
        {
            static void Main(string[] args)
            {
                string presentationFolder = @"D:\MyWorkingDocuments\OpenXmlCases\OpenXmlPowerpointUpdateText\OpenXmlPowerpointUpdateText\bin\Debug\";
                string presentationFile = "test.pptx";
                string copiedPresentationFile = "copytest.pptx";
                // Make a copy of the template presentation. This will throw an
                // exception if the template presentation does not exist.
                File.Copy(presentationFolder + presentationFile,
                  presentationFolder + copiedPresentationFile, true);
                // Open the destination presentation.
                using (PresentationDocument presDocument =
                  PresentationDocument.Open(presentationFolder + copiedPresentationFile,
                  true))
                {
                    PresentationPart presPart = presDocument.PresentationPart;
     
                    SlideMasterPart sectionSlidePart = (SlideMasterPart)presPart.GetPartById("rId1");
     
                    CommonSlideData sldData = sectionSlidePart.SlideMaster.CommonSlideData;
                    var shapes = sldData.Descendants<Shape>();
                    int count = shapes.Count();
                    foreach (Shape shp in shapes)
                    {
                        var textBodys = shp.Descendants<TextBody>();
                        foreach (TextBody textBody in textBodys)
                        {
                            var paras = textBody.Descendants<DocumentFormat.OpenXml.Drawing.Paragraph>();
                            foreach (DocumentFormat.OpenXml.Drawing.Paragraph p in paras)
                            {
                                var t = p.Descendants<DocumentFormat.OpenXml.Drawing.Text>().FirstOrDefault();
                                if (t != null)
                                {
                                    // here you can modify the text
                                    // t.Text = "the text you want to change";
                                    t.Text = "the text you want to change agfdsafd";
                                    Console.WriteLine(t.Text);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    

    Hope this can give  you some hint and just feel free to follow up after you have tried.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by Amit SP Monday, December 5, 2011 2:14 PM
    Monday, December 5, 2011 6:58 AM
  • Hi Bruce

    Many Thanks for your reply. The code you supplied is definitely helpful. I had to however change and write teh below code to make it work. This is probably due to the structure of my template which has a table structure with various rows and cells.

    string presentationFolder = @"C:\TestFile\";
                string presentationFile = "2010-Q3-SP-163.pptx";
                
                using (PresentationDocument presDocument =
                  PresentationDocument.Open(presentationFolder + presentationFile,
                  true))
                {
                    PresentationPart presPart = presDocument.PresentationPart;
    
                    SlideMasterPart sectionSlidePart = (SlideMasterPart)presPart.GetPartById("rId1");
    
                    CommonSlideData sldData = sectionSlidePart.SlideMaster.CommonSlideData;
    
                    var graphicFrames = sldData.Descendants<GraphicFrame>();
    
                    foreach (GraphicFrame graphicFrame in graphicFrames)
                    {
                        var graphicDataColl = graphicFrame.Descendants<T.GraphicData>();
                        foreach (T.GraphicData graphicData in graphicDataColl)
                        {
                            var tblRows = graphicData.Descendants<T.TableRow>();
                            foreach (T.TableRow tblRow in tblRows)
                            {
                                var paras = tblRow.Descendants<DocumentFormat.OpenXml.Drawing.Paragraph>();
                                foreach (DocumentFormat.OpenXml.Drawing.Paragraph p in paras)
                                {
                                    var t = p.Descendants<DocumentFormat.OpenXml.Drawing.Text>().FirstOrDefault();
                                    if (t != null)
                                    {
                                        if (t.Text == "MATTHEW")
                                        {
                                            t.Text = "TEXT TO BE REPLACED";
                                        }
    
                                    }
                                }
                            }
                        }
                    }
                }
    
    <br/><br/>
    

    This solves the problem. Thanks for your help.

    Best Regards

    Amit Gautam

    • Marked as answer by Bruce Song Tuesday, December 6, 2011 4:45 AM
    Monday, December 5, 2011 2:13 PM

All replies

  • Hi Amit,

    Thank you for posting.

    I have tried on my side and found that we need to get the Text property and set its value, below is the code snippet for you to reference:

    using System;
    using System.Linq;
    using System.IO;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Presentation;
     
    namespace OpenXmlPowerpointUpdateText
    {
        class Program
        {
            static void Main(string[] args)
            {
                string presentationFolder = @"D:\MyWorkingDocuments\OpenXmlCases\OpenXmlPowerpointUpdateText\OpenXmlPowerpointUpdateText\bin\Debug\";
                string presentationFile = "test.pptx";
                string copiedPresentationFile = "copytest.pptx";
                // Make a copy of the template presentation. This will throw an
                // exception if the template presentation does not exist.
                File.Copy(presentationFolder + presentationFile,
                  presentationFolder + copiedPresentationFile, true);
                // Open the destination presentation.
                using (PresentationDocument presDocument =
                  PresentationDocument.Open(presentationFolder + copiedPresentationFile,
                  true))
                {
                    PresentationPart presPart = presDocument.PresentationPart;
     
                    SlideMasterPart sectionSlidePart = (SlideMasterPart)presPart.GetPartById("rId1");
     
                    CommonSlideData sldData = sectionSlidePart.SlideMaster.CommonSlideData;
                    var shapes = sldData.Descendants<Shape>();
                    int count = shapes.Count();
                    foreach (Shape shp in shapes)
                    {
                        var textBodys = shp.Descendants<TextBody>();
                        foreach (TextBody textBody in textBodys)
                        {
                            var paras = textBody.Descendants<DocumentFormat.OpenXml.Drawing.Paragraph>();
                            foreach (DocumentFormat.OpenXml.Drawing.Paragraph p in paras)
                            {
                                var t = p.Descendants<DocumentFormat.OpenXml.Drawing.Text>().FirstOrDefault();
                                if (t != null)
                                {
                                    // here you can modify the text
                                    // t.Text = "the text you want to change";
                                    t.Text = "the text you want to change agfdsafd";
                                    Console.WriteLine(t.Text);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    

    Hope this can give  you some hint and just feel free to follow up after you have tried.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by Amit SP Monday, December 5, 2011 2:14 PM
    Monday, December 5, 2011 6:58 AM
  • Hi Bruce

    Many Thanks for your reply. The code you supplied is definitely helpful. I had to however change and write teh below code to make it work. This is probably due to the structure of my template which has a table structure with various rows and cells.

    string presentationFolder = @"C:\TestFile\";
                string presentationFile = "2010-Q3-SP-163.pptx";
                
                using (PresentationDocument presDocument =
                  PresentationDocument.Open(presentationFolder + presentationFile,
                  true))
                {
                    PresentationPart presPart = presDocument.PresentationPart;
    
                    SlideMasterPart sectionSlidePart = (SlideMasterPart)presPart.GetPartById("rId1");
    
                    CommonSlideData sldData = sectionSlidePart.SlideMaster.CommonSlideData;
    
                    var graphicFrames = sldData.Descendants<GraphicFrame>();
    
                    foreach (GraphicFrame graphicFrame in graphicFrames)
                    {
                        var graphicDataColl = graphicFrame.Descendants<T.GraphicData>();
                        foreach (T.GraphicData graphicData in graphicDataColl)
                        {
                            var tblRows = graphicData.Descendants<T.TableRow>();
                            foreach (T.TableRow tblRow in tblRows)
                            {
                                var paras = tblRow.Descendants<DocumentFormat.OpenXml.Drawing.Paragraph>();
                                foreach (DocumentFormat.OpenXml.Drawing.Paragraph p in paras)
                                {
                                    var t = p.Descendants<DocumentFormat.OpenXml.Drawing.Text>().FirstOrDefault();
                                    if (t != null)
                                    {
                                        if (t.Text == "MATTHEW")
                                        {
                                            t.Text = "TEXT TO BE REPLACED";
                                        }
    
                                    }
                                }
                            }
                        }
                    }
                }
    
    <br/><br/>
    

    This solves the problem. Thanks for your help.

    Best Regards

    Amit Gautam

    • Marked as answer by Bruce Song Tuesday, December 6, 2011 4:45 AM
    Monday, December 5, 2011 2:13 PM