C# 用C语言提取powerpoint标题#
我有powerponint 97-2003文件(.ppt扩展名),我需要使用C#以编程方式提取幻灯片标题。 我尝试过使用Microsoft.Office.Interop,但没有成功。 我用谷歌搜索过,最多我找到了如何获取PowerPoint的参考资料。幻灯片:C# 用C语言提取powerpoint标题#,c#,powerpoint,office-interop,C#,Powerpoint,Office Interop,我有powerponint 97-2003文件(.ppt扩展名),我需要使用C#以编程方式提取幻灯片标题。 我尝试过使用Microsoft.Office.Interop,但没有成功。 我用谷歌搜索过,最多我找到了如何获取PowerPoint的参考资料。幻灯片: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Office.Core; usin
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Core;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
namespace Tester
{
class Program
{
static void Main(string[] args)
{
Microsoft.Office.Interop.PowerPoint.Application presentationApp = new Microsoft.Office.Interop.PowerPoint.Application();
try
{
string pptPath = @"D:\somefile.ppt";
TestReadingTitles(presentationApp, pptPath);
}
finally
{
presentationApp.Quit();
}
}
private static void TestReadingTitles(Microsoft.Office.Interop.PowerPoint.Application presentationApp, string pptPath)
{
presentationApp.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
Microsoft.Office.Interop.PowerPoint.Presentations presentations = presentationApp.Presentations;
Microsoft.Office.Core.MsoTriState readOnly = Microsoft.Office.Core.MsoTriState.msoTrue;
Microsoft.Office.Core.MsoTriState untitled = Microsoft.Office.Core.MsoTriState.msoTrue;
Microsoft.Office.Core.MsoTriState withWindow = Microsoft.Office.Core.MsoTriState.msoFalse;
Microsoft.Office.Interop.PowerPoint.Presentation presentation = presentations.Open(pptPath, readOnly, untitled, withWindow);
for (int i = 0; i < presentation.Slides.Count; i++)
{
foreach (PowerPoint.Slide slide in presentation.Slides)
{
string slidetitle = ??????????????????;
}
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用Microsoft.Office.Core;
使用PowerPoint=Microsoft.Office.Interop.PowerPoint;
名称空间测试器
{
班级计划
{
静态void Main(字符串[]参数)
{
Microsoft.Office.Interop.PowerPoint.Application presentationApp=新的Microsoft.Office.Interop.PowerPoint.Application();
尝试
{
字符串pptPath=@“D:\somefile.ppt”;
TestReadingTitles(presentationApp,pptPath);
}
最后
{
presentationApp.Quit();
}
}
私有静态void TestReadingTitles(Microsoft.Office.Interop.PowerPoint.Application presentationApp,string pptPath)
{
presentationApp.Visible=Microsoft.Office.Core.MsoTriState.msoTrue;
Microsoft.Office.Interop.PowerPoint.Presentations演示文稿=presentationApp.Presentations;
Microsoft.Office.Core.MsoTriState readOnly=Microsoft.Office.Core.MsoTriState.msoTrue;
Microsoft.Office.Core.MsoTriState untitled=Microsoft.Office.Core.MsoTriState.msoTrue;
Microsoft.Office.Core.MsoTriState with window=Microsoft.Office.Core.MsoTriState.msoFalse;
Microsoft.Office.Interop.PowerPoint.Presentation演示文稿=演示文稿。打开(pptPath,只读,无标题,withWindow);
对于(int i=0;i
我没有从ppt直接提取幻灯片标题的解决方案。这是一个工作区-首先临时将其转换为pptx,然后使用openxml提取标题。
对于从ppt到pptx的转换,我使用了我不喜欢的Microsoft Interop,但我没有更好的解决方案
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Presentation;
using D = DocumentFormat.OpenXml.Drawing;
using Shape = DocumentFormat.OpenXml.Presentation.Shape;
namespace Tester
{
class Program
{
static void Main(string[] args)
{
string pptPath = @"D:\mypresentation.ppt";
ReadTitles(pptPath);
}
private static void ReadTitles(string pptPath)
{
IList<string> slideTitles = GetSlidesTitles(pptPath);
Debug.Print("SLIDES TITLES FOR {0}:", pptPath);
foreach (string slideTitle in slideTitles)
{
Debug.Print("\t {0}", slideTitle);
}
}
private static IList<string> GetSlidesTitles(string pptPath)
{
string pptxPath = SaveAsPptx(pptPath);
IList<string> titles = GetSlideTitles(pptxPath);
try
{
File.Delete(pptxPath);
Debug.Print("Temporary pptx file {0} deleted.", pptxPath);
}
catch (Exception e)
{
Debug.Print("Error deleting file {0}. ERROR: {1}", pptxPath, e.Message);
}
return titles;
}
private static string SaveAsPptx(string pptPathIn)
{
Microsoft.Office.Interop.PowerPoint.Application presentationApp = new Microsoft.Office.Interop.PowerPoint.Application();
string pptxPathOut = null;
try
{
string pptDir = Path.GetDirectoryName(pptPathIn);
string pptFileNameOnly = Path.GetFileNameWithoutExtension(pptPathIn);
pptxPathOut = Path.Combine(pptDir, pptFileNameOnly + ".pptx");
presentationApp.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
Microsoft.Office.Interop.PowerPoint.Presentations presentations = presentationApp.Presentations;
Microsoft.Office.Core.MsoTriState readOnly = Microsoft.Office.Core.MsoTriState.msoFalse;
Microsoft.Office.Core.MsoTriState untitled = Microsoft.Office.Core.MsoTriState.msoFalse;
Microsoft.Office.Core.MsoTriState withWindow = Microsoft.Office.Core.MsoTriState.msoFalse;
Debug.Print("Opening ppt file {0} ...", pptPathIn);
Microsoft.Office.Interop.PowerPoint.Presentation presentation = presentations.Open(pptPathIn, readOnly, untitled, withWindow);
Debug.Print("Starting creation of pptx from ppt {0}", pptPathIn);
presentation.SaveCopyAs(pptxPathOut, PowerPoint.PpSaveAsFileType.ppSaveAsOpenXMLPresentation, Microsoft.Office.Core.MsoTriState.msoFalse);
Debug.Print("Successfully created pptx {0} from ppt {1}", pptxPathOut, pptPathIn);
}
catch (Exception e)
{
Debug.Print("Error during creating pptx from ppt " + pptPathIn, e);
}
finally
{
presentationApp.Quit();
}
return pptxPathOut;
}
// Get a list of the titles of all the slides in the presentation.
public static IList<string> GetSlideTitles(string presentationFile)
{
// Open the presentation as read-only.
using (PresentationDocument presentationDocument = PresentationDocument.Open(presentationFile, false))
{
return GetSlideTitles(presentationDocument);
}
}
// Get a list of the titles of all the slides in the presentation.
public static IList<string> GetSlideTitles(PresentationDocument presentationDocument)
{
if (presentationDocument == null)
{
throw new ArgumentNullException("presentationDocument");
}
// Get a PresentationPart object from the PresentationDocument object.
PresentationPart presentationPart = presentationDocument.PresentationPart;
if (presentationPart != null &&
presentationPart.Presentation != null)
{
// Get a Presentation object from the PresentationPart object.
Presentation presentation = presentationPart.Presentation;
if (presentation.SlideIdList != null)
{
List<string> titlesList = new List<string>();
// Get the title of each slide in the slide order.
foreach (var slideId in presentation.SlideIdList.Elements<SlideId>())
{
SlidePart slidePart = presentationPart.GetPartById(slideId.RelationshipId) as SlidePart;
// Get the slide title.
string title = GetSlideTitle(slidePart);
// An empty title can also be added.
titlesList.Add(title);
}
return titlesList;
}
}
return null;
}
// Get the title string of the slide.
public static string GetSlideTitle(SlidePart slidePart)
{
if (slidePart == null)
{
throw new ArgumentNullException("slidePart");
}
// Declare a paragraph separator.
string paragraphSeparator = null;
if (slidePart.Slide != null)
{
// Find all the title shapes.
var shapes = from shape in slidePart.Slide.Descendants<Shape>()
where IsTitleShape(shape)
select shape;
StringBuilder paragraphText = new StringBuilder();
foreach (var shape in shapes)
{
// Get the text in each paragraph in this shape.
foreach (var paragraph in shape.TextBody.Descendants<D.Paragraph>())
{
// Add a line break.
paragraphText.Append(paragraphSeparator);
foreach (var text in paragraph.Descendants<D.Text>())
{
paragraphText.Append(text.Text);
}
paragraphSeparator = "\n";
}
}
return paragraphText.ToString();
}
return string.Empty;
}
// Determines whether the shape is a title shape.
private static bool IsTitleShape(Shape shape)
{
var placeholderShape = shape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.GetFirstChild<PlaceholderShape>();
if (placeholderShape != null && placeholderShape.Type != null && placeholderShape.Type.HasValue)
{
switch ((PlaceholderValues)placeholderShape.Type)
{
// Any title shape.
case PlaceholderValues.Title:
// A centered title.
case PlaceholderValues.CenteredTitle:
return true;
default:
return false;
}
}
return false;
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统诊断;
使用System.IO;
使用System.Linq;
使用系统文本;
使用PowerPoint=Microsoft.Office.Interop.PowerPoint;
使用DocumentFormat.OpenXml.Packaging;
使用DocumentFormat.OpenXml.Presentation;
使用D=DocumentFormat.OpenXml.Drawing;
使用Shape=DocumentFormat.OpenXml.Presentation.Shape;
名称空间测试器
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串pptPath=@“D:\mypresentation.ppt”;
阅读标题(pptPath);
}
私有静态void ReadTitles(字符串pptPath)
{
IList slideTitles=GetSlideTitles(pptPath);
打印({0}的幻灯片标题:,pptPath);
foreach(slidetiles中的字符串slidetile)
{
打印(“\t{0}”,slideTitle);
}
}
私有静态IList GetSlideStiles(字符串pptPath)
{
字符串pptxPath=SaveAsPptx(pptPath);
IList titles=GetSlideTitles(pptxPath);
尝试
{
Delete(pptxPath);
Print(“临时pptx文件{0}已删除。”,pptxPath);
}
捕获(例外e)
{
Print(“删除文件{0}时出错。错误:{1}”,pptxPath,e.Message);
}
返回标题;
}
私有静态字符串SaveAsPptx(字符串pptPathIn)
{
Microsoft.Office.Interop.PowerPoint.Application presentationApp=新的Microsoft.Office.Interop.PowerPoint.Application();
字符串pptxPathOut=null;
尝试
{
字符串pptDir=Path.GetDirectoryName(pptPathIn);
字符串pptFileNameOnly=Path.GetFileNameWithoutExtension(pptPathIn);
pptxPathOut=Path.Combine(pptDir,pptFileNameOnly+“.pptx”);
presentationApp.Visible=Microsoft.Office.Core.MsoTriState.msoTrue;
Microsoft.Office.Interop.PowerPoint.Presentations演示文稿=presentationApp.Presentations;
Microsoft.Office.Core.MsoTriState readOnly=Microsoft.Office.Core.MsoTriState.msoFalse;
Microsoft.Office.Core.MsoTriState untitled=Microsoft.Office.Core.MsoTriState.msoFalse;
Microsoft.Office.Core.MsoTriState with window=Microsoft.Office.Core.MsoTriState.msoFalse;
Print(“正在打开ppt文件{0}…”,pptPathIn);
Microsoft.Office.Interop.PowerPoint.Presentation Presentation=演示文稿。打开(pptPathIn、只读、无标题、withWindow);
Print(“从ppt{0}开始创建pptx”,pptPathIn);
presentation.SaveCopyAs(pptxPathOut,PowerPoint.PpSaveAsFileType.ppsaveasopenxmlpentation,Microsoft.Office.Core.MsoTriState.msoFalse);
Print(“从ppt{1}成功创建了pptx{0}”,pptxPathOut,pptPathIn);
}
捕获(例外e)
{
Debug.Print(“从ppt创建pptx时出错”+pptPathIn,e);
}
最后
{
presentationApp.Quit();
}
返回pptxPathOut;
}
//获取演示文稿中所有幻灯片的标题列表。
公共静态IList GetSlideTitles(字符串表示文件)
{
//以只读方式打开演示文稿。
使用(PresentationDocument PresentationDocument=PresentationDocument.Open(presentationFile,false))
{
返回GetSlideTiles(演示文档);
}
}
//
using System;
using System.Collections.Generic;
using Microsoft.Office.Core;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
namespace Mintra.Publisher.DocumentConverter.Core.Utils
{
class InteropUtility
{
public static IList<string> GetPresentationTitles(string pptPath)
{
IList<string> result = new List<string>();
var presentationApp = new Microsoft.Office.Interop.PowerPoint.Application();
try
{
presentationApp.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
Microsoft.Office.Interop.PowerPoint.Presentations presentations = presentationApp.Presentations;
var readOnly = Microsoft.Office.Core.MsoTriState.msoTrue;
var untitled = Microsoft.Office.Core.MsoTriState.msoTrue;
var withWindow = Microsoft.Office.Core.MsoTriState.msoFalse;
Microsoft.Office.Interop.PowerPoint.Presentation presentation = presentations.Open(pptPath, readOnly, untitled, withWindow);
int i = 0;
foreach (PowerPoint.Slide slide in presentation.Slides)
{
string defaultTitle = String.Format("Slide {0}", i);
String shapeTitle = ExtractSlideTitlefromShape(slide, defaultTitle);
result.Add(shapeTitle);
}
}
finally
{
presentationApp.Quit();
}
return result;
}
private static string ExtractSlideTitlefromShape(PowerPoint.Slide slide, string defaultValue)
{
PowerPoint.HeadersFooters headersFooters = slide.HeadersFooters;
PowerPoint.Shapes mastershapes = slide.Master.Shapes;
for (int i = 1; i <= slide.Shapes.Count; i++)
{
PowerPoint.Shape shape = slide.Shapes[i];
bool hasTextFrame = shape.HasTextFrame == MsoTriState.msoTrue;
bool isTypePlaceholder = shape.Type.Equals(MsoShapeType.msoPlaceholder);
bool hasTextInTextFrame = shape.TextFrame.HasText == MsoTriState.msoTrue;
bool isTitleShape = shape.Name.ToLower().Contains("title");
if (isTypePlaceholder && hasTextFrame && hasTextInTextFrame && isTitleShape)
{
return shape.TextFrame.TextRange.Text;
}
}
return defaultValue;
}
}
}
Microsoft.Office.Interop.PowerPoint.Application pptApplication = new Microsoft.Office.Interop.PowerPoint.Application();
Microsoft.Office.Interop.PowerPoint.Slides slides;
Microsoft.Office.Interop.PowerPoint._Slide slide;
// Create the Presentation File
Presentation pptPresentation = pptApplication.Presentations.Add(MsoTriState.msoTrue);
for (int i = 0; i < 2; i++)
{
Microsoft.Office.Interop.PowerPoint.CustomLayout customLayout = pptPresentation.SlideMaster.CustomLayouts[Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutChartAndText];
// customLayout.t
// Create new Slide
slides = pptPresentation.Slides;
slide = slides.AddSlide(1, customLayout);
slide.Shapes.Title.Top = 0;
slide.Shapes.Title.TextFrame.TextRange.Text = "Welcome!";
private static string ExtractSlideTitlefromSlide(Microsoft.Office.Interop.PowerPoint.Slide slide, string defaultValue)
{
if (slide.Shapes.HasTitle == Office.MsoTriState.msoTrue)
{
if (slide.Shapes.Title.TextFrame.HasText == Office.MsoTriState.msoTrue)
{
return slide.Shapes.Title.TextFrame.TextRange.Text;
}
}
return defaultValue;
}