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,否则会出错.