文字介紹如何在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 = "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—