实现了翻译给定PPT的功能。
思路:遍历PPT文件里每一个文本框,提取其中的内容,调用大模型翻译。
遇见的问题:
1.需要是标准文本框,否则无法识别。
2.遇见了直接把整个PPT的翻译结果填到一个文本框的情况,调整了提示词解决了问题。
用到的代码如下:
package top.pulselink.chatglm;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFShape;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.poi.xslf.usermodel.XSLFTextBox;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class PPTModifier {
public static void modifyPPT(ChatClient chats, String inputPath, String outputPath) throws IOException {
// 读取PPT文件
FileInputStream inputStream = new FileInputStream(inputPath);
XMLSlideShow ppt = new XMLSlideShow(inputStream);
inputStream.close();
// 获取每个幻灯片
for (XSLFSlide slide : ppt.getSlides()) {
// 获取幻灯片中的文本框
for (XSLFShape shape : slide.getShapes()) {
if (shape instanceof XSLFTextBox) {
XSLFTextBox textBox = (XSLFTextBox) shape;
String message = textBox.getText(); // 获取文本框内容
// 调用ChatClient的AsyncInvoke方法获取修改后的文本
chats.AsyncInvoke(message);
String modifiedMessage = chats.getResponseMessage();
// 清空文本框内容并添加修改后的内容
textBox.clearText();
textBox.setText(modifiedMessage);
}
}
}
// 保存修改后的PPT文件
FileOutputStream outputStream = new FileOutputStream(outputPath);
ppt.write(outputStream);
outputStream.close();
}
public static void main(String[] args) {
ChatClient chats = new ChatClient("c8db142638066beb984b2bbad5bdcf03.Dx1rvSfX27j8z6es"); // 用你的API密钥初始化ChatClient
try {
modifyPPT(chats, "D:\\Dadada\\Downloads\\ppt.pptx", "output.pptx");
System.out.println("PPT修改完成,并已保存为output.pptx");
} catch (IOException e) {
e.printStackTrace();
}
}
}
依然是采用打jar包,再在spring boot项目中调用的方式,用到的service和controller代码如下:
package com.example.ppt_project_backend.service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import top.pulselink.chatglm.ChatClient;
import top.pulselink.chatglm.PPTModifier;
import java.io.IOException;
@Service
public class PPTService {
private final ChatClient chatClient;
public PPTService(@Value("${chatglm.api.key}") String apiKey) {
if (apiKey == null || apiKey.isEmpty()) {
throw new IllegalStateException("API key is required");
}
this.chatClient = new ChatClient(apiKey);
this.chatClient.registerShutdownHook();
}
public void modifyPPT(String inputPath, String outputPath) throws IOException {
PPTModifier.modifyPPT(chatClient, inputPath, outputPath);
}
}
package com.example.ppt_project_backend.controller;
import com.example.ppt_project_backend.service.PPTService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
@RestController
@RequestMapping("/ppt")
public class PPTController {
@Autowired
private PPTService pptService;
@PostMapping("/modify")
public String modifyPPT(@RequestParam String inputPath, @RequestParam String outputPath) {
try {
pptService.modifyPPT(inputPath, outputPath);
return "PPT modification completed and saved to " + outputPath;
} catch (IOException e) {
e.printStackTrace();
return "Error modifying PPT: " + e.getMessage();
}
}
}
然后运行主程序PptProjectBackendApplication.java,并在cmd中输入
curl -X POST "http://localhost:8080/translate/modify" -d "inputPath=D:\Dadada\Downloads\ppt.pptx&outputPath=D:\Dadada\Downloads\ppt11.pptx"
注意把inputPath和outputPath换成自己对应的路径。
输出结果:
原PPT
翻译后