doc文档转化为docx,python

10 篇文章 0 订阅
8 篇文章 0 订阅

doc文件转化为docx

目前找到doc文件转化为docx比较少,商业版 com.spire.doc 很好用,但是需要收费
查找资料,找到一种利用python脚本,将doc转化docx的实现方式,性能好像很差

Java代码

package com.pdf.pdfdemo;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Python {

    public static void main(String[] args) throws Exception
    {
        python2();
    }

    public static void python1() throws  Exception{
        // define the command string
        String commandStr = new String(
                "python E:\\python\\TestMain.py ");
        //Create a Process instance and execute commands
        Process pr = Runtime.getRuntime().exec(commandStr);
        //Get the result produced by executing the above commands
        BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream()));
        String line = null;
        String result = "";
        while ((line = in.readLine()) != null)
        {
            result += line + "\r\n";
        }
        System.out.println(result);

        in.close();
        int endFlag = pr.waitFor();
        if (endFlag == 0)
        {
            System.out.println("The process is ended normally.");
        }
    }

    public static void python2() throws  Exception{
        try {
            //设置命令行传入参数
            String[] args1 = new String[] { "python", "E:\\python\\test1.py", "E:\\python\\11.doc","E:\\python\\zn2.docx"};
            Process pr = Runtime.getRuntime().exec(args1);
            BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
            in.close();
            int endFlag = pr.waitFor();
            if (endFlag == 0)
            {
                System.out.println("The process is ended normally.");
            }
            pr.destroy();
            System.out.println("end");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

python脚本

# coding=gbk
import sys
from win32com import client as wc
w = wc.Dispatch('Word.Application')
doc=w.Documents.Open(sys.argv[1])
doc.SaveAs(sys.argv[2],16)#必须有参数16,否则会出错.

需要安装python

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值