当前位置: 首页 > news >正文

poi生成的ppt,powerPoint打开提示内容错误解决方案

poi生成的ppt,powerPoint打开提示内容错误解决方案

最近做了ppt的生成,使用poi制作ppt,出现一个问题。微软的powerPoint打不开,提示错误信息

在这里插入图片描述

通过xml对比工具发现只需要删除幻灯片的某些标签即可解决。

用的是XML Notepand
在这里插入图片描述

分析思路:

1.把poi生成的pptx用wps打开,正常,但是poi生成的pptx在powerPoint打不开。尝试用wps另存一份,ok,wps另存后的ppt,powerPoint可以打开了。然后ppt的大小也有了变化,肯定是wps对这个ppt做了一些格式化操作。具体是啥呢,不清楚,只能用这个 XML Notepand 对比看看吧。
2. ppt本质上是xml的压缩包集合。我们对ppt进行解压。

在这里插入图片描述

3.里面的东西挺多了,我自己也不怎么懂,但是我想既然是ppt的某个幻灯片打不开,那我就对比一下能幻灯片,看看poi生成的ppt和wps另存后的ppt的幻灯片有哪些区别。

我们进入这个目录
在这里插入图片描述
在这里插入图片描述在这里插入图片描述

4.这个就是放置幻灯片的位置。

我们通过这个XML Notepand 进行一个对比观察,
在这里插入图片描述
我们先用xml notePand打开poi生成的一个幻灯片,我打开的是wsp另存的解压后的第4个幻灯片,ppt\slides\slide4.xml
在这里插入图片描述

在这里插入图片描述

然后选择这个,再次选择一个poi生成的ppt,选择同一个幻灯片,进行一个对比
在这里插入图片描述
然后进行两个文件对比
在这里插入图片描述

最后对比发现这个标签可能不可以被powerPoint正确识别,我们就把这个标签删除

在这里插入图片描述

<p:custDataLst><p:tags r:id="rId1"/>
</p:custDataLst>

最关键的来了,直接上代码

package com.ruoyi.ppt.utilsService;import com.ruoyi.ppt.aopCustom.customAnnotations.LogExecutionTime;
import com.ruoyi.ppt.config.ThreadPoolDealTask;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.w3c.dom.*;import javax.annotation.Resource;
import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;@Slf4j
@Component
public class PptXmlFormate {@Resourceprivate ThreadPoolDealTask threadPoolDealTask;@LogExecutionTime("PPT XML标签调整耗时")public void xmlFormate(String pptxFilePath, String useFont) {useFont = StringUtils.isBlank(useFont) ? "宋体" : useFont;// 使用 try-with-resources 自动关闭临时文件和 Zip 流try {// 创建临时文件来存储更新后的 PPTX 文件File tempFile = File.createTempFile("pptx_temp" + UUID.randomUUID(), ".zip");tempFile.deleteOnExit();// 读取 PPTX 文件并更新 XMLtry (FileInputStream fis = new FileInputStream(pptxFilePath);ZipInputStream zis = new ZipInputStream(fis);FileOutputStream fos = new FileOutputStream(tempFile);ZipOutputStream zos = new ZipOutputStream(fos)) {ZipEntry entry;Map<String, ByteArrayOutputStream> updatedFiles = new HashMap<>();// 遍历 PPTX 文件中的每个条目while ((entry = zis.getNextEntry()) != null) {String entryName = entry.getName();ByteArrayOutputStream entryData = new ByteArrayOutputStream();// 读取条目内容byte[] buffer = new byte[1024];int len;while ((len = zis.read(buffer)) > 0) {entryData.write(buffer, 0, len);}// 仅处理幻灯片 XML 文件if (entryName.startsWith("ppt/slides/slide") && entryName.endsWith(".xml")) {// 解析 XMLtry (ByteArrayInputStream bais = new ByteArrayInputStream(entryData.toByteArray());ByteArrayOutputStream updatedXml = new ByteArrayOutputStream()) {DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();factory.setNamespaceAware(true);DocumentBuilder builder = factory.newDocumentBuilder();Document document = builder.parse(bais);// 异步任务,并确保异常捕获CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {// 删除标签String custDataLstXpath = "//*[local-name()='custDataLst']";removeNodesUsingXPath(document, custDataLstXpath);}, threadPoolDealTask.getThreadPoolExecutor());// 捕获并处理异步任务中的异常future.exceptionally(ex -> {log.error("处理 XML 异步任务时发生错误: {}", ex.getMessage(), ex);return null;}).join(); // 等待任务完成// 写入修改后的 XML 内容TransformerFactory transformerFactory = TransformerFactory.newInstance();Transformer transformer = transformerFactory.newTransformer();transformer.setOutputProperty(OutputKeys.INDENT, "yes");DOMSource source = new DOMSource(document);StreamResult result = new StreamResult(updatedXml);transformer.transform(source, result);updatedFiles.put(entryName, updatedXml);}} else {// 对于其他条目,保持原样updatedFiles.put(entryName, entryData);}}// 写入更新后的 PPTX 文件for (Map.Entry<String, ByteArrayOutputStream> fileEntry : updatedFiles.entrySet()) {String entryName = fileEntry.getKey();ByteArrayOutputStream fileData = fileEntry.getValue();zos.putNextEntry(new ZipEntry(entryName));fileData.writeTo(zos);zos.closeEntry();}zos.finish();}// 将临时文件移动到原始 PPTX 文件路径,替换原文件Files.move(tempFile.toPath(), new File(pptxFilePath).toPath(), StandardCopyOption.REPLACE_EXISTING);} catch (IOException e) {log.error("处理文件时发生 IO 错误: {}", e.getMessage(), e);} catch (Exception e) {log.error("处理过程中发生错误: {}", e.getMessage(), e);}}public void removeNodesUsingXPath(Document document, String xpathExpression) {XPath xPath = XPathFactory.newInstance().newXPath();// 设置命名空间前缀和 URINamespaceContext nsContext = new NamespaceContext() {public String getNamespaceURI(String prefix) {switch (prefix) {case "a":return "http://schemas.openxmlformats.org/drawingml/2006/main";case "p":return "http://schemas.openxmlformats.org/presentationml/2006/main";default:return XMLConstants.NULL_NS_URI;}}public String getPrefix(String namespaceURI) {return null;}public Iterator getPrefixes(String namespaceURI) {return null;}};xPath.setNamespaceContext(nsContext);try {NodeList nodes = (NodeList) xPath.evaluate(xpathExpression, document, XPathConstants.NODESET);for (int i = nodes.getLength() - 1; i >= 0; i--) { // 从后向前遍历Node node = nodes.item(i);Node parentNode = node.getParentNode();if (parentNode != null) {parentNode.removeChild(node);}}} catch (Exception e) {log.error("Error removing nodes using XPath: {}", e.getMessage());}}}

注意哈,这里使用了线程池,不需要的可以删除,需要的话不会配置的,这里也给出代码

package com.ruoyi.ppt.config;import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;import javax.annotation.Resource;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger;@Slf4j
@Configuration
@Data
public class ThreadPoolDealTask {private volatile ThreadPoolExecutor threadPool;// 自定义 ThreadFactory,为线程池中的线程指定名称private ThreadFactory createThreadFactory(String poolName) {AtomicInteger counter = new AtomicInteger(0);return r -> {Thread thread = new Thread(r);thread.setName(poolName + "-thread-" + counter.incrementAndGet());return thread;};}// 懒加载线程池,只有在第一次使用时才会创建 用于 业务处理public void initializeThreadPool() {if (this.threadPool == null) {synchronized (this) {if (this.threadPool == null) { // 双重检查锁定this.threadPool = new ThreadPoolExecutor(2,10,30L,TimeUnit.SECONDS,new LinkedBlockingQueue<>(50),createThreadFactory("CustomThreadPool"), // 使用自定义的 ThreadFactorynew ThreadPoolExecutor.CallerRunsPolicy());// 启用核心线程超时this.threadPool.allowCoreThreadTimeOut(false);}}}}public ThreadPoolExecutor getThreadPoolExecutor() {return this.threadPool;}// 输出任务队列状态private void printQueueStatus() {if (threadPool instanceof ThreadPoolExecutor) {ThreadPoolExecutor executor = (ThreadPoolExecutor) threadPool;System.out.println("Submitting task to thread: " + Thread.currentThread().getName());System.out.println("当前任务队列大小: " + executor.getQueue().size());System.out.println("当前任务队列大小: " + executor.getQueue().size());System.out.println("当前活动线程数: " + executor.getActiveCount());System.out.println("当前线程池大小: " + executor.getPoolSize());}}// 提交任务并返回Future对象public <T> Future<T> submitTask(Callable<T> task) {initializeThreadPool(); // 确保线程池已初始化return threadPool.submit(task);}// 提交Runnable任务并返回Futurepublic Future<?> submitTask(Runnable task) {initializeThreadPool(); // 确保线程池已初始化return threadPool.submit(task);}// 优雅地关闭线程池public void shutdown() {if (threadPool != null) {threadPool.shutdown();try {// 等待现有任务执行完毕if (!threadPool.awaitTermination(60, TimeUnit.SECONDS)) {threadPool.shutdownNow(); // 强制关闭// 等待任务响应中断if (!threadPool.awaitTermination(60, TimeUnit.SECONDS)) {System.err.println("线程池未能在指定时间内关闭");}}} catch (InterruptedException ie) {// 在当前线程被中断时,也强制关闭线程池threadPool.shutdownNow();// 保留中断状态Thread.currentThread().interrupt();} finally {threadPool = null; // 关闭后将线程池引用置为空}}}}

现在解决了xml的标签问题,

最后只需要生成后的ppt地址,传入这个方法,执行一次标签删除即可

处理前效果:
在这里插入图片描述

处理后效果:
在这里插入图片描述

http://www.lryc.cn/news/446278.html

相关文章:

  • 基于stm32物联网身体健康检测系统
  • BeautifulSoup4在爬虫中的使用
  • Laya2.x出包alipay小游戏
  • Vue极简入门
  • 系统敏感信息搜索工具(支持Windows、Linux)
  • Fyne ( go跨平台GUI )中文文档-容器和布局 (四)
  • 文心智能体 恐怖类游戏
  • 智慧城市运营模式--政府和社会资本合作
  • 【Python报错已解决】ValueError: cannot convert float NaN to integer
  • ClickHouse 与 Quickwit 集成实现高效查询
  • Facebook Marketplace无法使用的原因及解决方案
  • uboot — uboot命令的使用
  • 基础漏洞——SSRF
  • 报错解决方案
  • 机器人的动力学——牛顿欧拉,拉格朗日,凯恩
  • 【AI写作】解释区块链技术的应用场景和优势
  • IPsec-Vpn
  • 一日连发两款视频大模型,火山引擎杀疯了!
  • JavaScript --数字Number的常用方法
  • GIS开发常用的开源地图数据框架有哪些?
  • SPSS26统计分析笔记——2 描述统计
  • C++——输入一个字符串,把其中的字符按逆序输出。如输入LIGHT,输出THGIL。用string方法。
  • 基于区块链的相亲交易系统源码解析
  • win11 wsl2安装ubuntu22最快捷方法
  • jekyll相关的技术点
  • 【Golang】Go语言中如何面向对象?
  • E2VPT: An Effective and Efficient Approach for Visual Prompt Tuning
  • 影刀RPA实战:网页爬虫之天猫商品数据
  • 微信小程序注册流程及APPID获取(完整版图文教程)
  • 分享课程:VUE数据可视化教程