|
|
|
@ -1,62 +1,29 @@
|
|
|
|
|
package com.dsideal.base.Tools.FillData.Test;
|
|
|
|
|
|
|
|
|
|
import com.dsideal.base.Tools.FillData.ExcelKit.ExcelKit;
|
|
|
|
|
import com.dsideal.base.Util.FileUtil;
|
|
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
|
import java.nio.charset.Charset;
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
public class TestCallPython {
|
|
|
|
|
/**
|
|
|
|
|
* 调用python+com读取WORD中的图表
|
|
|
|
|
*
|
|
|
|
|
* @throws IOException
|
|
|
|
|
* @throws InterruptedException
|
|
|
|
|
*/
|
|
|
|
|
public static void readTuBiao() throws IOException, InterruptedException {
|
|
|
|
|
// 创建ProcessBuilder对象,并设置Python脚本的路径
|
|
|
|
|
ProcessBuilder processBuilder = new ProcessBuilder("D:\\anaconda3\\envs\\py310\\python.exe", "D:\\dsWork\\YunNanDsBase\\Py\\TuBiao.py");
|
|
|
|
|
// 重定向错误流到标准输出,这样可以在Java中捕获所有的输出
|
|
|
|
|
processBuilder.redirectErrorStream(true);
|
|
|
|
|
// 启动进程
|
|
|
|
|
Process process = processBuilder.start();
|
|
|
|
|
// 读取Python脚本的输出
|
|
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
|
|
|
|
String line;
|
|
|
|
|
while ((line = reader.readLine()) != null) {
|
|
|
|
|
System.out.println(line);
|
|
|
|
|
}
|
|
|
|
|
// 等待Python脚本执行完成
|
|
|
|
|
process.waitFor();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws InterruptedException, IOException {
|
|
|
|
|
//哪个word文档
|
|
|
|
|
String docPath = "D:\\dsWork\\YunNanDsBase\\Doc\\全省及州市县区人口与教育报告集20241023\\16个州市报告2022\\分析报告20240510\\红河哈尼族彝族自治州人口变化及其对教育的影响20240419.docx";
|
|
|
|
|
//第几个图表
|
|
|
|
|
int tuBiaoNum = 1;
|
|
|
|
|
String taskTxt = "c:/task.txt";
|
|
|
|
|
//Excel文件生成位置
|
|
|
|
|
String excelPath="c:/task.xlsx";
|
|
|
|
|
//如果文件存在则删除
|
|
|
|
|
if (new File(taskTxt).exists()) {
|
|
|
|
|
new File(taskTxt).delete();
|
|
|
|
|
}
|
|
|
|
|
//将docPath写入taskTxt,第一行
|
|
|
|
|
//将tuBiaoNum写入taskTxt,第二行
|
|
|
|
|
FileWriter writer = new FileWriter(taskTxt, StandardCharsets.UTF_8);
|
|
|
|
|
writer.write(docPath);
|
|
|
|
|
writer.write("\n");
|
|
|
|
|
writer.write(String.valueOf(tuBiaoNum));
|
|
|
|
|
writer.write("\n");
|
|
|
|
|
writer.flush();
|
|
|
|
|
writer.close();
|
|
|
|
|
String excelPath = "c:/task.xlsx";
|
|
|
|
|
|
|
|
|
|
//写入交互文本文件
|
|
|
|
|
ExcelKit.callPythonPrepare(docPath, tuBiaoNum);
|
|
|
|
|
//对图表进行读取
|
|
|
|
|
readTuBiao();
|
|
|
|
|
ExcelKit.delExcel(excelPath);
|
|
|
|
|
ExcelKit.callPythonRead();
|
|
|
|
|
//读取生成的EXCEL,使用POI就可以了
|
|
|
|
|
List<List<String>> sheetList = ExcelKit.readSheet(excelPath,1);
|
|
|
|
|
List<List<String>> sheetList = ExcelKit.readSheet(excelPath, 1);
|
|
|
|
|
System.out.println(sheetList);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|