Accessing existing tables

Hi

When opening a PowerPoint file how do I access existing tables in order to change the contents or extract the text, the examples show how to do such things to tables you are creating but I can’t see how I access tables that already exist?

Many thanks

Bon

Hi Bon,

Try this:

var presentation = PresentationDocument.Load("input.pptx");

// Get first table on first slide.
var slide = presentation.Slides[0];
var table = slide.Content.Drawings
    .OfType<GraphicFrame>()
    .First(frame => frame.Table != null)
    .Table;

// Extract text.
Console.WriteLine(table.TextContent.ToString());

// Or extract text in tabular format.
foreach (var row in table.Rows)
{
    foreach (var cell in row.Cells)
    {
        Console.Write(cell.TextContent.ToString().Replace("\r\n", ""));
        Console.Write("\t");
    }
    Console.WriteLine();
}

// Update cells.
table.Rows[0].Cells[0].Text.AddParagraph().AddRun(
    "Added new text to the end of first cell.");
table.Rows[0].Cells[1].TextContent.LoadText(
    "Replaced the whole text of the second cell.");

presentation.Save("output.pptx");

I hope this helps.

Regards,
Mario

Thanks Mario! Exactly what we were after! :+1: