文本和图片是powerpoint文档的重要组成部分。本文将介绍如何使用 spire.presentation 提取powerpoint幻灯片文档中的文本和图片并将它们保存到本地路径。
提取ppt文档中的文本
c#
//实例化一个presentation对象
presentation ppt = new presentation();
//加载ppt文档
ppt.loadfromfile("sample.pptx", fileformat.pptx2010);
//实例化一个stringbuilder 对象
stringbuilder sb = new stringbuilder();
//提取ppt所有页面的文本
for (int i = 0; i < ppt.slides.count; i )
{
for (int j = 0; j < ppt.slides[i].shapes.count; j )
{
if (ppt.slides[i].shapes[j] is iautoshape)
{
iautoshape shape = ppt.slides[i].shapes[j] as iautoshape;
if (shape.textframe != null)
{
foreach (textparagraph tp in shape.textframe.paragraphs)
{
sb.append(tp.text environment.newline);
}
}
}
}
}
//将提取到的文本写为.txt格式并保存到本地路径
file.writealltext("获取文本.txt", sb.tostring());
vb.net
'实例化一个presentation对象
dim ppt as new presentation()
'加载ppt文档
ppt.loadfromfile("sample.pptx", fileformat.pptx2010)
'实例化一个stringbuilder 对象
dim sb as new stringbuilder()
'提取ppt所有页面的文本
for i as integer = 0 to ppt.slides.count - 1
for j as integer = 0 to ppt.slides(i).shapes.count - 1
if typeof ppt.slides(i).shapes(j) is iautoshape then
dim shape as iautoshape = trycast(ppt.slides(i).shapes(j), iautoshape)
if shape.textframe isnot nothing then
for each tp as textparagraph in shape.textframe.paragraphs
sb.append(tp.text environment.newline)
next
end if
end if
next
next
'将提取到的文本写为.txt格式并保存到本地路径
file.writealltext("获取文本.txt", sb.tostring())
提取ppt文档中的图片
c#
//实例化一个presentation对象
presentation ppt = new presentation();
//加载ppt文档
ppt.loadfromfile("sample.pptx", fileformat.pptx2010);
//遍历ppt文档中的所有图片,并将图片以png格式保存到本地路径
for (int i = 0; i < ppt.images.count; i )
{
image image = ppt.images[i].image;
image.save(string.format("images{0}.png", i));
}
vb.net
'实例化一个presentation对象
dim ppt as new presentation()
'加载ppt文档
ppt.loadfromfile("sample.pptx", fileformat.pptx2010)
'遍历ppt文档中的所有图片,并将图片以png格式保存到本地路径
for i as integer = 0 to ppt.images.count - 1
dim image as image = ppt.images(i).image
image.save(string.format("images{0}.png", i))
next
提取到本地路径的文本和图片: