motivation:
毕设有一个小部分,是抽取从各种公开课下载的PPT的内容。之前用Python的win32com弄到哭也只弄出了纯文本,而不能识别哪些是标题(理论上应该是可以的?我看到源码里有HaveTitle等等的内容,然而调用就跪,QAQ)。然后就只抽出了纯文本,放弃治疗改用维基百科做知识库了。扔在一边就去做别的了。
于是这周老师又重新让我抽标题OTL,就搜到了这个看起来很厉害的东西。学习ING。
prework:
- 下载:https://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=5124 download里的两个文件,先装v2再装tool
- 在项目里加引用:解决方案资源管理器里,有个引用,左键添加引用,在扩展里添加DocumentFormat.OpenXml,框架里添加windowspackage
gao:
reference:https://msdn.microsoft.com/zh-cn/library/cc850843(v=office.14)# 获取演示文稿中的所有幻灯片的标题 https://msdn.microsoft.com/zh-cn/library/cc536290.aspx PresentationDocument 方法
示例代码里大部分说的挺清楚的~在此基础上可以获取一个大概的框架。在main函数里call GetSlideTitles(filename)基本就可以跑起来辣
运行示例代码的时候发现挂了 = =,报的错是The document cannot be opened because there is an invalid part with an unexpected content type. ...在stackouverflow里发现了一个解决方法,亲测可用。在调用GetSlideTitles之前先调用一次fixPowerPoint,就可以让文件正常打开了。fixPowerPoint代码如下:
private static void FixPowerpoint(string fileName) { //Opening the package associated with file Console.WriteLine(fileName); using (Package wdPackage = Package.Open(fileName, FileMode.Open, FileAccess.ReadWrite)) { //Uri of the printer settings part var binPartUri = new Uri("/ppt/printerSettings/printerSettings1.bin", UriKind.Relative); if (wdPackage.PartExists(binPartUri)) { //Uri of the presentation part which contains the relationship var presPartUri = new Uri("/ppt/presentation.xml", UriKind.RelativeOrAbsolute); var presPart = wdPackage.GetPart(presPartUri); //Getting the relationship from the URI var presentationPartRels = presPart.GetRelationships().Where(a => a.RelationshipType.Equals("http://schemas.openxmlformats.org/officeDocument/2006/relationships/printerSettings", StringComparison.InvariantCultureIgnoreCase)).SingleOrDefault(); if (presentationPartRels != null) { //Delete the relationship presPart.DeleteRelationship(presentationPartRels.Id); } //Delete the part wdPackage.DeletePart(binPartUri); } wdPackage.Close(); } }