C# Net 使用 openxml 提取ppt中的音频、视频、图片、文本

C# Net 使用 openxml 提取ppt中的音频、视频、图片、文本

 

名称空间:

using System;
using DocumentFormat.OpenXml.Packaging;
using System.IO;
using System.Linq;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Presentation;
using A = DocumentFormat.OpenXml.Drawing;
using P14 = DocumentFormat.OpenXml.Office2010.PowerPoint;

  

代码如下:

        public void GetVideo(string path)
        {
            using (var doc = PresentationDocument.Open(path, false))
            {
                var presentation = doc.PresentationPart.Presentation;

                foreach (SlideId slideId in presentation.SlideIdList)
                {
                    SlidePart slidePart = doc.PresentationPart.GetPartById(slideId.RelationshipId) as SlidePart;
                    if (slidePart == null || slidePart.Slide == null)
                        continue;

                    //ppt中显示的真实编号
                    var SlideNumber = presentation.FirstSlideNum?.Value ?? 1 + presentation.SlideIdList.ToList().IndexOf(slideId);

                    Slide slide = slidePart.Slide;

                    //音频
                    var audioList = slide.Descendants<Audio>();
                    //视频
                    var videoList = slide.Descendants<Video>();
                    //图片
                    var picList = slide.CommonSlideData.ShapeTree.Descendants<Picture>().Where(o => o.ShapeProperties.ChildElements.Any(n => n is A.NoFill));
                    //文本框
                    var txBodyList = slide.CommonSlideData.ShapeTree.Descendants<TextBody>();
                    foreach (var audio in audioList)
                    {
                        //音频关联的形状
                        var spTgt = audio.CommonMediaNode.TargetElement.ShapeTarget;
                        //形状
                        var cNvPr = slide.Descendants<NonVisualDrawingProperties>().FirstOrDefault(o => o.Id == spTgt.ShapeId);

                        //形状信息
                        var ShapeId = cNvPr.Id.Value;
                        var ShapeName = cNvPr.Name.Value;
                        var ShapeDescr = cNvPr.Description?.Value;

                        //上级和上上级
                        var nvPicPr = (NonVisualPictureProperties)cNvPr.Parent;
                        var pic = (Picture)nvPicPr.Parent;

                        //音频文件关联
                        var audioFile = nvPicPr.ApplicationNonVisualDrawingProperties.Elements<A.AudioFromFile>().FirstOrDefault();
                        ////视频文件关联
                        //var videoFile = nvPicPr.ApplicationNonVisualDrawingProperties.Elements<A.VideoFromFile>().FirstOrDefault();

                        var uri = slidePart.ExternalRelationships.FirstOrDefault(o => o.Id == audioFile.Link)?.Uri;//外部关系
                        if (uri == null)
                            uri = slidePart.DataPartReferenceRelationships.FirstOrDefault(o => o.Id == audioFile.Link)?.Uri;//引用关系

                        //音频图片文件关联
                        var embed = pic.BlipFill.Blip.Embed.Value;
                        var part = slidePart.GetPartById(embed);
                        var stream = part.GetStream();

                    }

                }
            }
        }

  

 

完毕,图片和文本,此代码举一反三就出来了

 

上一篇:【Python 1-11】Python手把手教程之——字典的用法和对字典的管理


下一篇:Dictionary