package com.ots.project.tool.report.LAQ.chart; import com.ots.framework.config.EssConfig; import com.ots.project.tool.report.LAQ.LAQTemplate; import lombok.extern.slf4j.Slf4j; import org.apache.poi.util.Units; import org.apache.poi.xwpf.usermodel.Document; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import java.io.*; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.util.List; import java.util.Map; import java.util.Random; import java.util.UUID; /** * 采用python生成图片,再插入 */ @Slf4j public class LAQAllPng { public static void changeChart(XWPFDocument document, Map textMap) { log.info("【进入全景图渲染模式】"); try{ List laqTemplateList = (List)textMap.get("allList"); //生成图片 String laqExcelPath = (String) textMap.get("laqExcelPath"); log.info("laqExcelPath文件路径:"+laqExcelPath); // 指定要检查或创建的目录路径 String baseDirectoryPath = "/home/data/laq/"; String bgPath = baseDirectoryPath+"bgPng/"; String reportPath = baseDirectoryPath+"reportCharts/"+ UUID.randomUUID()+"/"; // 创建 File 对象 File reportDirectory = new File(reportPath); // 检查目录是否存在 if (!reportDirectory.exists()) { // 目录不存在,创建目录 boolean created = reportDirectory.mkdirs(); // 使用 mkdirs() 方法可以创建多级目录 } // 构建命令和参数 ProcessBuilder processBuilder = new ProcessBuilder( "/root/miniconda3/envs/generate-charts-env/bin/python", // Python 执行程序 "/root/python-program/generate_charts.py", // Python 脚本路径 laqExcelPath, bgPath, reportPath // 传递给 Python 脚本的参数 ); try { // 启动进程 Process process = processBuilder.start(); // 获取标准输出 BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = reader.readLine()) != null) { log.info("【执行py生成图表结果】"+line); } // 获取错误输出(用于调试) BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream())); while ((line = errorReader.readLine()) != null) { log.info("【执行py生成图表结果222】错误:"+line); } // 等待进程完成 int exitCode = process.waitFor(); log.info("执行python完成,图表已经生成到路径:"+reportPath); } catch (IOException | InterruptedException e) { e.printStackTrace(); } updateImage(document,reportPath); System.out.println("全景图渲染完成"); //要删除生成图表路径。 deleteDirectory(reportDirectory); log.info("目录:"+reportPath+",删除成功"); } catch (Exception ex) { log.error("LAQ全景图异常:"+ex.getMessage()); } } /** * 删除目录及其内容 * @param dir 要删除的目录 * @return 删除是否成功 */ public static boolean deleteDirectory(File dir) { // 如果目录不存在,直接返回 false if (!dir.exists()) { return false; } // 如果是目录,则删除目录中的所有内容 if (dir.isDirectory()) { File[] files = dir.listFiles(); if (files != null) { for (File file : files) { // 递归删除子文件或子目录 if (!deleteDirectory(file)) { return false; // 如果删除子文件/子目录失败,返回 false } } } } // 删除空目录或文件 return dir.delete(); } public static void updateImage(XWPFDocument document,String reportImgPath){ //替换图片 try{ // 获取所有段落 List paragraphs = document.getParagraphs(); //测试用的 // String img1 = "D:\\桌面文件\\LAQ全景图报告\\111.png"; // String img2 ="D:\\桌面文件\\LAQ全景图报告\\222.png"; // String img3 ="D:\\桌面文件\\LAQ全景图报告\\333.png"; // /home/data String img1 = reportImgPath+"charts_top.png"; String img2 =reportImgPath+"charts_middle.png"; String img3 =reportImgPath+"charts_bottom.png"; // 遍历段落进行替换操作 synchronized (paragraphs) { for (XWPFParagraph paragraph : paragraphs) { String text = paragraph.getText(); // 将图片插入到段落中 int width = Units.toEMU(580); // 图片宽度 int height = Units.toEMU(700); // 图片高度 //用%%号在前面就被替换了 if (text.contains("{{box0}}")) { // 检查是否包含要替换的文字变量 // 获取所有运行的列表 List runs = paragraph.getRuns(); // 移除所有运行 for (int i = runs.size() - 1; i >= 0 ; i--) { paragraph.removeRun(i); } //创建Random类对象 Random random = new Random(); //产生随机数 int number = random.nextInt(999) + 1; // 创建图片对象 XWPFRun run = paragraph.createRun(); int pictureType = Document.PICTURE_TYPE_PNG; // 图片类型 run.addPicture(new FileInputStream(img1), pictureType, "Seal"+number, width, height); } if (text.contains("{{box1}}")) { // 检查是否包含要替换的文字变量 // 获取所有运行的列表 List runs = paragraph.getRuns(); // 移除所有运行 for (int i = runs.size() - 1; i >= 0 ; i--) { paragraph.removeRun(i); } // 创建图片对象 XWPFRun run = paragraph.createRun(); int pictureType = Document.PICTURE_TYPE_PNG; // 图片类型 run.addPicture(new FileInputStream(img2), pictureType, "image.png", width, height); } if (text.contains("{{box2}}")) { // 检查是否包含要替换的文字变量 // 获取所有运行的列表 List runs = paragraph.getRuns(); // 移除所有运行 for (int i = runs.size() - 1; i >= 0 ; i--) { paragraph.removeRun(i); } // 创建图片对象 XWPFRun run = paragraph.createRun(); int pictureType = Document.PICTURE_TYPE_PNG; // 图片类型 run.addPicture(new FileInputStream(img3), pictureType, "image.png", width, height); } } } } catch (Exception ex) { log.info("【异常】"+ex.getMessage()); } } }