文本介紹如何在C#程式中使用正則運算式替換PPT幻燈片中的指定文本內容,具體操作步驟如下:
1. 在程式中參考Spire.Presentation.dll,兩種方法可參考如下:
(1)直接在程式中通過nuget搜索 “ Spire.Presentation ” 下載安裝,
(2)將 Spire.Presentation for .NET 6.8.3 包下載到本地,解壓,手動將Bin檔案夾路徑下的Spire.Presentation.dll添加參考至程式,
兩種方法均可添加該程式集檔案,添加結果如圖:

C#
using Spire.Presentation; using System.Text.RegularExpressions; namespace ReplaceText_PPT { class Program { static void Main(string[] args) { //創建Presentation實體 Presentation ppt = new Presentation(); //加載示例檔案 ppt.LoadFromFile("test.pptx"); //獲取第1張幻燈片 ISlide slide = ppt.Slides[0]; //替換該幻燈片中所有“作業”以及其后到行尾的內容為“WORK” Regex regex = new Regex("作業.*"); string newvalue = https://www.cnblogs.com/Yesi/archive/2021/08/30/"WORK"; foreach (IShape shape in slide.Shapes) { shape.ReplaceTextWithRegex(regex, newvalue); } //保存結果檔案 ppt.SaveToFile("ReplaceTextWithRegex.pptx", FileFormat.Pptx2013); System.Diagnostics.Process.Start("ReplaceTextWithRegex.pptx"); } } }
Vb.net
Imports Spire.Presentation Imports System.Text.RegularExpressions Namespace ReplaceText_PPT Class Program Private Shared Sub Main(args As String()) '創建Presentation實體 Dim ppt As New Presentation() '加載示例檔案 ppt.LoadFromFile("test.pptx") '獲取第1張幻燈片 Dim slide As ISlide = ppt.Slides(0) '替換該幻燈片中所有“作業”以及其后到行尾的內容為“WORK” Dim regex As New Regex("作業.*") Dim newvalue As String = "WORK" For Each shape As IShape In slide.Shapes shape.ReplaceTextWithRegex(regex, newvalue) Next '保存結果檔案 ppt.SaveToFile("ReplaceTextWithRegex.pptx", FileFormat.Pptx2013) System.Diagnostics.Process.Start("ReplaceTextWithRegex.pptx") End Sub End Class End Namespace
替換效果對比:

—End—
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/295810.html
標籤:.NET技术
上一篇:記一次 .NET 某流媒體獨角獸 API 句柄泄漏分析
下一篇:c# 異步呼叫 利用委托異步呼叫
