java poi替换pptx中的文字

lxwinner 2012-02-19 02:55:10
最近项目中有个需求,PPTS中的文字能够进行替换。比如在PPT中设置一个标志位“#year#”,用户能在界面上把这个标志位替换成自己所需要的值。
以下是我写的代码。
public void test4()
{
try
{
XMLSlideShow ppt = new XMLSlideShow( new FileInputStream("2.pptx") );

Pattern pyear = Pattern.compile( "([#]year[#])" ) ;
Matcher myear = null ;
Pattern pmonth = Pattern.compile( "([#]month[#])" ) ;
Matcher mmonth = null ;

for( XSLFSlide slide : ppt.getSlides() )
{
for( XSLFShape shape : slide.getShapes() )
{
// System.out.println( shape.getShapeName() + " " + shape.toString() ) ;

if ( shape instanceof XSLFTextShape )
{
XSLFTextShape txtshape = (XSLFTextShape)shape ;
System.out.println( "XSLFTextShape" + ":" + shape.getShapeName() + ":" + txtshape.getText() ) ;
//
if ( txtshape.getShapeName().equals("Text Box 14") )
{
txtshape.setText("fuck") ;
}

myear = pyear.matcher( txtshape.getText() ) ;
// mmonth = pmonth.matcher( txtshape.getText() ) ;
// System.out.println(myear.replaceAll("2011")) ;
// txtshape.setText( myear.replaceAll("2011") ) ;
// txtshape.setText( mmonth.replaceAll("09") ) ;
// System.out.println( txtshape.getTextParagraphs().get(0).getText() );
// System.out.println( txtshape.getTextParagraphs().get(0).getTextRuns().get(0).getText() ) ;

for ( XSLFTextParagraph p : txtshape.getTextParagraphs() )
{
// System.out.println( p.getText() ) ;
// System.out.println( p.getTextRuns().toString() ) ;
for ( XSLFTextRun textRun : p.getTextRuns() )
{
// System.out.println( textRun.getText() ) ;
}
}
}
else if ( shape instanceof XSLFConnectorShape )
{
XSLFConnectorShape connectorShape = (XSLFConnectorShape)shape ;
System.out.println( "XSLFConnectorShape" + ":" + shape.getShapeName() ) ;
}
else if ( shape instanceof XSLFPictureShape )
{
XSLFPictureShape picShape = (XSLFPictureShape)shape ;
System.out.println( "XSLFPictureShape" + ":" + shape.getShapeName() ) ;
}
}
}

FileOutputStream out = new FileOutputStream("3.pptx") ;
ppt.write( out ) ;
out.close() ;
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}


以上代码只是进行测试下方案是否可行,所以规范度什么的不会太高。现在有一个问题就是,如果PPTX中的文字外面还有套一层矩型框之类的东西,文字就无法替换。如果是用以下代码,则可以读取,但也没找到替换的办法。
public void test5()
{
try
{
XMLSlideShow ppt = new XMLSlideShow( new FileInputStream("2.pptx") );

for( XSLFSlide slide : ppt.getSlides() )
{
System.out.println( slide.getCommonSlideData().getDrawingText().size() ) ;
for ( DrawingTextBody drawingText : slide.getCommonSlideData().getDrawingText() )
{
for ( DrawingParagraph ph : drawingText.getParagraphs() )
{
System.out.println( ph.getText() ) ;
}
}

// for ( DrawingParagraph ph : slide.getCommonSlideData().getText() )
// {
// System.out.println( ph.getText() ) ;
// }
}
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
如果需要测试PPT的话可以找我要22841297@qq.com
...全文
1025 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
hust_moonlight 2014-03-28
  • 打赏
  • 举报
回复
谢谢 参考了下
qqq765 2012-05-24
  • 打赏
  • 举报
回复
帮了不少忙哈
lxwinner 2012-02-20
  • 打赏
  • 举报
回复
还是没人来啊。
lxwinner 2012-02-19
  • 打赏
  • 举报
回复
自己再顶顶吧。

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧