java 读取ppt并写入txt

package document;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.model.Shape;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.model.TextBox;
import org.apache.poi.hslf.usermodel.SlideShow;

/**
* 将PPT中的内容复制到txt中
* @author DanielCooger
* <a href="
mailto:tangjunfeng52099@gmail.com">daniel</a>
*/

public class PPT {
private static String date = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
//新建txt文件
private static String ppt = "d:\\doc\\PPT" + date + ".txt";
//excel文件
private static String path="d:\\document\\1.ppt";

public static void main(String[] args) {
   try {
    // 得到源文件
    SlideShow ss = new SlideShow(new HSLFSlideShow(path));
    // 得到源文件中的幻灯片数量
    Slide[] slides = ss.getSlides();
    for (int a = 0; a < slides.length; a++) {
     // 得到每张幻灯片中的字符串数量
     Shape[] sps = slides[a].getShapes();
     for (int i = 0; i < sps.length; i++) {
      System.out.println(((TextBox) sps[i]).getText());
      new PPT().insert(ppt, ((TextBox)sps[i]).getText(), true);
     }
    }
   } catch (Exception e) {
    e.getMessage();
   }
}

/**
* 将文本写入相应的文本中
*/
public void insert(String path, String content, boolean append) {
   BufferedWriter bw;
   File file;
   try {
    boolean addstr = append;
    file = new File(path);
    // 创建文件输出流写入文件
    FileWriter fw = new FileWriter(file, addstr);
    bw = new BufferedWriter(fw);
    // 将文本内容写入文件
    fw.write(content);
    fw.flush();
    fw.close();
   } catch (Exception e) {
    e.getMessage();
   }
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值