使用Python与C#创建PPT演示文稿

时间:2013-06-04 15:57:51

标签: c# python powerpoint

我即将自动化创建PPT的过程,但不确定要朝哪个方向发展。

总体目标是阅读Excel电子表格,如下所示:

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

用户将为我提供图片的路径,然后我将根据他们提供的信息创建PPT演示文稿。

好像有两条路径我可以采用C#或Python,但我不确定选择哪一条。有没有人有这方面的经验你可以列出每个的优点和缺点,或者是否有更好的解决方案,请你让我知道。

1 个答案:

答案 0 :(得分:1)

确实有几种方法可以做到。 让我们开始吧,如果你打算做一个可以执行相同功能的加载项,你可以在https://netoffice.codeplex.com/

下找到有关如何创建加载项的更多信息。

现在,让我们回到你的主要问题。一种简单的方法是在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)
    {

    }
}

这段代码允许您循环遍历所有工作表并检查已使用的单元格。

在您的情况下,您需要考虑一些特定的范围。

sheet.UsedRange.Copy(Missing.Value);

您可以将其保存到变量中,并逐行读取并解释其结果。

那是Excel部分,现在我们选择Powerpoint部分。 如何计划创建您的Powerpoint,您想要存储,显示,每张幻灯片图片的信息?每张幻灯片几张图片? 你可以做的是,创建你的模板Powerpoint并循环它

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

这实际上会在每张幻灯片上添加一张图片。 如果你想自己创建一个新的幻灯片,你必须定义一个新的对象,无论是表格还是图片都与它的概念无关。

 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);

到目前为止,我们已经看到了如何获得Excel使用范围,现在,您的工作是应用于您想要使用它的特定范围。您知道如何遍历所有幻灯片以及如何在需要时添加新幻灯片。缺少的部分是,保存Excel和Powerpoint。

presentation.SaveAs("myfile.pptx");
presentation.Close();
ppApp.Quit();
workbook.SaveAs("myfile.xlsx");
workbook.Close();
xlsApp.Quit(); 
相关问题