java 批量打开doc, docx文件,并获取文本第一行有效数据作为文档重命名

1 篇文章 0 订阅

前几天室友不小心把老师的硬盘的资料弄不见了,后来请人数据恢复找回来大部分资料,但有4000个doc和docx文档恢复时原名称已经不见了。所以帮他做了一个小程序,自动打开文本,获取文档第一行有效文字,将其作为文档重命名时的新名称。

流程:

1.准备工作:POI包导入,doc,docx文件打开需要导入相关的依赖包(好像只需要以下几个就可以了,不过我全导进去了)ra

然后add to buildpath。

下载以下的源文件就好了。


2.对文档重名工具:(可以使用好压对文档进行批量重命名)

先选中文件(crtl +A)—》右键—》其它压缩命令—》批量重命名。


3.上代码:如果要运行需要改循环数字+文件的路径。除了打开文件的方式不同,大部分代码是相同的。

以下是修改docx的代码:

public class readdocx {

	public static void main(String[] args) {
        try {
        	for(int i =2001; i<=2007; i++){ //以数字命名文件名,方便获取        		
        		String lll="zxcvbnmlkjhgfdsaqwertyuiopQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
        	    //由Random生成随机数
        		Random random=new Random();  
        	    StringBuffer sb=new StringBuffer();
        	    String randomKey = null;
        	    for(int j=0; j<3; ++j){
        	    	int number=random.nextInt(62);
        	    	sb.append(lll.charAt(number));
        	    	randomKey = sb.toString();
        	    }
        	String filepath = "E:\\Document\\" + String.valueOf(i)+ ".docx";//
        	File file = new File(filepath); 
       		//检查文件是否存在,如不存在,则跳到下一个文件查找
       		if(!file.exists()){
       			continue;
       		}
        		
       		OPCPackage opcPackage = POIXMLDocument.openPackage(filepath);
            POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
            String[] name = extractor.getText().split("\n");
            String str =null;
        	String str1 =null;
        	//做判断,找到有字符的一行数组
        	for(int k=0;k<name.length; ++k){
        		String str2 = name[k].replaceAll("\\s*", "");
        		if(str2.length()==0){
        			continue;
        		}else{
        			str = str2;
        			System.out.println(str);
        			//剔除重命名非法字符
        			str.replaceAll("\\p{Punct}","");
        			Pattern p = Pattern.compile("\\s*|\t|\r|\n|:|");  
       				Matcher m = p.matcher(str);  
       				str1 = m.replaceAll("");
        			str1=str1.replaceAll(":", "");
        			str1=str1.replaceAll("\\?", "");
        			str1=str1.replaceAll("/", "");
        			break;
        			}
        	}
        	String filename = null;
        	if(str1==null){
        		continue;
        	}else if(str1.length()>35){
        		filename = str1.substring(0, 30);
        	}else {
       			filename = str1;
       		}
       		filename=filename+ sb.toString() +".docx"; //将承载的字符转换成字符串
       		System.out.println(filename);
       		opcPackage.close();
           	File file2= new File(filepath); //指定文件名及路径
           	System.out.println(file2);
           	String path=file2.getAbsolutePath();
           	//System.out.println(path);
           	if(path.indexOf("\\")>=0){     
           		path= path.substring(0,path.lastIndexOf("\\"))+"\\";
           		//System.out.println(path);
           	}
            	file.renameTo(new File(path+filename));
        	}
        } catch (Exception e) {
            e.printStackTrace();
        }
	}
}

以下是doc文档的代码:

public class readdoc {
    public static void main(String[] args) {
        try {
        	for(int i =1059; i<=3000; i++){ 
        		//以数字命名文件名,方便获取
        		String lll="zxcvbnmlkjhgfdsaqwertyuiopQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
        	    //由Random生成随机数
        		Random random=new Random();  
        		String randomKey = null;
        	    StringBuffer sb=new StringBuffer();
        	    for(int j=0; j<3; ++j){
        	    	int number=random.nextInt(62);
        	    	sb.append(lll.charAt(number));//将产生的数字通过length次承载到sb中
        	    	randomKey = sb.toString();
        	    }
        	String filepath = "E:\\Document\\" + String.valueOf(i)+ ".doc";//
        	File file = new File(filepath); 
        	//检查文件是否存在,如不存在,则跳到下一个文件查找
        	if(!file.exists()){
        		continue;
        	}
        	InputStream is = new FileInputStream(new File(filepath));
        	WordExtractor extractor = new WordExtractor(is);
        	//以分割符号获取每一行的数字转换成字符串数组
        	String[] name = extractor.getText().split("\r");
        	String str =null;
        	String str1 =null;
        	//做判断,找到有字符的一行数组
        	for(int k=0;k<name.length; ++k){
        		String str2 = name[k].replaceAll("\\s*", "");
        		if(str2.length()==0){
        			continue;
        		}else{
        			str = str2;
        			System.out.println(str);
        			//剔除字符串时遇到的非法字符
        			str.replaceAll("\\p{Punct}","");
        			Pattern p = Pattern.compile("\\s*|\t|\r|\n|:|");  
        			Matcher m = p.matcher(str);  
        			str1 = m.replaceAll("");
        			str1=str1.replaceAll(":", "");
       				str1=str1.replaceAll("\\?", "");
       				str1=str1.replaceAll("/", "");
        			break;
        		}
        	}
        	String filename = null;
        	if(str1==null){
        		continue; //退出循环,空文件或者是图片文件
        	}else if(str1.length()>35){
        		filename = str1.substring(0, 30);
        	}else {
        		filename = str1;
        	}
        	filename=filename+ randomKey +".doc"; //将承载的字符转换成字符串
        	System.out.println(filename);
            File file2= new File(filepath); //指定文件名及路径
            System.out.println(file2);
           	String path=file2.getAbsolutePath();
            //System.out.println(path);
            if(path.indexOf("\\")>=0){     
            	path= path.substring(0,path.lastIndexOf("\\"))+"\\";
            	//System.out.println(path);
            }
            file.renameTo(new File(path+filename));
        }   
       } catch (Exception e) {
            e.printStackTrace();
       }
   }
}

5.代码还不够完善,但是运行时错误是3/1000,所以能应对大部分文件了,若报错,请直接跳过该文件继续运行。


测试结果如下:


新手一枚,而且第一次发博,文章有错望指教。

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值