當前位置: 妍妍網 > 碼農

一個可操作PPT的.Net開源庫

2024-02-27碼農

推薦一個簡單易用的.NET庫,使得可以方便處理PowerPoint演示文稿。

01

計畫簡介

ShapeCrawler 是一個專為處理 PowerPoint 演示文稿而設計的 .NET 庫。這個庫為開發者提供了一組簡單易用的 API,以便操作 PowerPoint (.pptx) 檔中的各種元素。

ShapeCrawler 庫的特點包括:

1、無需安裝 Microsoft Office: 是一個獨立的庫,不需要在開發或執行環境中安裝 Microsoft Office。

2、基於 Open XML SDK: 基於 Open XML SDK 的基礎上,封裝的提供更高層次的抽象。Open XML SDK 是一個開源庫,提供了Office 檔格式(如 .docx、.xlsx、.pptx 等)的底層API操作。

3、簡單易用: 提供了一個簡化的物件模型,使得開發者可以輕松地遍歷、查詢、修改和建立 PowerPoint 演示文稿中的形狀。你可以輕松地獲取形狀的內容(如位置、大小、填充顏色等),以及修改它們。

4、支持型別多: 支持包括文字域、圖形、影像、圖表等操作。

02

使用方法

1、簡單範例

// 開啟PPTvar pres = new Presentation("xxx.pptx");var shapes = pres.Slides[0].Shapes;// 獲取PPT的數量var shapesCount = shapes.Count;// 獲取文本var shape = shapes.GetByName("TextBox 1");var text = shape.TextFrame!.Text

2、設定文字域為自動適應

var pres = new Presentation("some.pptx");var shape = pres.Slides[0].Shapes.GetByName("AutoShape 1");var textFrame = shape.TextFrame!;textFrame.AutofitType = AutofitType.Resize;pres.Save();

3、替換文字

var pres = new Presentation("some.pptx");var textFrames = pres.Slides[0].TextFrames();foreach (var textFrame in textFrames){ textFrame.Text = "some text";}pres.Save();

4、更新圖片

ar pres = new Presentation("picture.pptx");// 獲取圖片控制項var picture = pres.Slides[0].Shapes.GetByName<IPicture>("Picture 1");// 更改圖片picture.Image.Update("new-image.png");// 獲取圖片的MIMEvar mimeType = picture.Image.MIME;pres.Save();

5、表格操作

var pres = new Presentation("some-pptx");var shapeCollection = pres.Slides[0].Shapes;shapeCollection.AddTable(x: 50, y: 100, columnsCount: 3, rowsCount: 2);var addedTable = (ITable)shapeCollection.Last();var cell = addedTable[0, 0];cell.TextFrame.Text = "Hi, Table!";pres.Save();

03

計畫地址

https://github.com/ShapeCrawler/ShapeCrawler