简体   繁体   中英

Creating PPT presentation with Python vs C#

I am about to automate the process of creating a PPT but was not sure which direction to head.

The overall goal is to read an Excel spread sheets which will look like this:

Path:
<somepath>
_________________________________________________________
Picture name      |    Title                |   Subtitle  
__________________________________________________________

The user will give me a path to the pictures and then I will create a PPT presentation from the information they give me.

It seems as though there are two paths I can take either C# or Python, but I am not sure which one to choose. Does anyone have experience with this can you maybe list some pros and cons of each, or is there is another solution that is even better can you please let me know.

There are several ways to do it indeed. Let's start, if you ever plan to do an Add-In which could do the same functionality, you could find more information about how to create an Add-In under: https://netoffice.codeplex.com/

Now, let's get back to you main question. An easy way would be in C#.

//http://netoffice.codeplex.com/discussions/277323
/// Loop through all our sheets in Excel in order to make the replacement
foreach (Excel.Worksheet sheet in workbook.Sheets)
{
    /// Select current sheet
    sheet.Activate();
    workSheet = (Excel.Worksheet)xlsApp.Worksheets[worksheetnumber];
    //Console.WriteLine(workSheet.Name);
    foreach (Excel.Range cell in workSheet.UsedRange)
    {

    }
}

This piece of code allows you to loop through all your worksheets and check for the used cells.

In your case, you want some specific range to be taken in consideration.

sheet.UsedRange.Copy(Missing.Value);

You can save that one into a variable, and read it line by line and interpret its result.

That's for the Excel part, now we go for the Powerpoint part. How to do plan to create your Powerpoint, what information to you want to store, to show, per slide a picture? several pictures per slide? What you can do is, create your template Powerpoint and loop through it

foreach (PowerPoint.Slide slide in presentation.Slides)
{
        slide.Select();
        slide.Shapes.AddPicture(temporaryFilePath + ".bmp", MsoTriState.msoTrue, MsoTriState.msoTrue, pptleft, ppttop, pptwidth, pptheight);
}    

This would actually add a picture on each slide. If you want to create by yourself a new slide, you have to define a new object, either it is a table or a picture doesn't matter it's concept.

 PowerPoint.Table objTable = null;
 objTable = presentation.Slides[slide.SlideIndex].Shapes.AddTable(amountrowsshown, pptshape.Table.Columns.Count, topsize, leftsize, slide.Master.Width - widthsize, slide.Master.Height - heightsize).Table;
 presentation.Slides.Add(slide.SlideIndex + newaddslidecounter, NetOffice.PowerPointApi.Enums.PpSlideLayout.ppLayoutClipArtAndVerticalText);

So far, we've seen how to get Excel used range, now, your job to apply to the certain range you want to use it. You know how to iterate through all slides and how to add a new slide if requested. The missing part is, to save Excel and Powerpoint.

presentation.SaveAs("myfile.pptx");
presentation.Close();
ppApp.Quit();
workbook.SaveAs("myfile.xlsx");
workbook.Close();
xlsApp.Quit(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM