main
黄海 8 months ago
parent 3a58bd25de
commit 5160afefb0

@ -10,6 +10,7 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.usermodel.XWPFChart;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
@ -332,4 +333,45 @@ public class ExcelKit {
}
return --rlt;
}
/**
* python+comWORD
*
* @throws IOException
* @throws InterruptedException
*/
public static void callPythonRead() throws IOException, InterruptedException {
// 创建ProcessBuilder对象并设置Python脚本的路径
String python = "D:\\anaconda3\\envs\\py310\\python.exe";
String py = "D:\\dsWork\\YunNanDsBase\\Py\\TuBiao.py";
ProcessBuilder processBuilder = new ProcessBuilder(python, 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 callPythonPrepare(String docPath,int tuBiaoNum) throws IOException {
String taskTxt = "c:/task.txt";
//如果文件存在则删除
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();
}
}

@ -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+comWORD
*
* @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);
}
}

Loading…
Cancel
Save