我正在尝试使用 apache poi 将演讲者笔记从一个 powerpoint 传输到另一个 powerpoint,但我无法获得准确的传输。
四处寻找后,我找不到很多资源。我确实找到了这个链接:How to get pptx slide notes text using apache poi? , 它适用于大多数情况。但是,当原始 pptx 中涉及幻灯片母版等某些功能时,一些不属于演讲者备注的文本会被解释为演讲者备注。
XSLFNotes notes_src = slides_src[i].getNotes();
XSLFNotes notes_dst = ppt_dst.getNotesSlide(slides_dst[i]);
这一切都在一个 for 循环中,其中 i 是迭代次数。在这里,我从源文件中获取幻灯片 i,并从目标文件中获取相应的幻灯片 i。
for (XSLFShape shape_src : notes_src) {
if (shape_src instanceof XSLFTextShape) {
XSLFTextShape txShape = (XSLFTextShape) shape_src;
for (XSLFTextParagraph xslfParagraph : txShape.getTextParagraphs()) {
我在这里从幻灯片中获取文本。下面的 if 循环是我必须开始过滤掉一些实际上不是演讲者笔记的“演讲者”笔记的地方(例如,幻灯片编号以某种方式被解释为笔记;还打印了这个版权符号)。
if (!(xslfParagraph.getText().startsWith("" + (i + 1)) & xslfParagraph.getText().length() < 3) & !(xslfParagraph.getText().startsWith("Copyright ©"))) {
for (XSLFTextShape shape_dst : notes_dst.getPlaceholders()) {
if (shape_dst.getTextType() == Placeholder.BODY) {
shape_dst.setText(shape_dst.getText() + xslfParagraph.getText() + "\n");
下面的语句是另一个过滤器;如果涉及母版幻灯片的功能,奇怪的“单击以编辑母版文本样式...”文本片段也将被解释为演讲者注释。
shape_dst.setText(shape_dst.getText().replace("Click to edit Master text styles", "").replace("Second level", "").replace("Third level", "").replace("Fourth level", "").replace("Fifth level", ""));
}}}}}}
简而言之,不是演讲者笔记的内容显示为“笔记”。关于这个主题的在线资源不多;有人可以帮忙吗?
最佳答案
XSLFSlide.getNotes
获取的是注释幻灯片。这些可能不仅有包含注释的正文文本形状,还有通过其他占位符(如页眉、页脚、日期时间和幻灯片编号)填充的文本形状。要确定得到的是哪种文本形状,可以从形状中获取占位符类型。这是
CTShape cTShape = (CTShape)shape.getXmlObject();
STPlaceholderType.Enum type = cTShape.getNvSpPr().getNvPr().getPh().getType();
然后只能得到 STPlaceholderType.BODY
类型的文本形状。
例子:
import java.io.FileInputStream;
import org.apache.poi.xslf.usermodel.*;
import org.openxmlformats.schemas.presentationml.x2006.main.CTShape;
import org.openxmlformats.schemas.presentationml.x2006.main.STPlaceholderType;
import java.util.List;
public class PowerPointReadNotes {
public static void main(String[] args) throws Exception {
XMLSlideShow slideShow = new XMLSlideShow(new FileInputStream("PowerPointHavingNotes.pptx"));
List<XSLFSlide> slides = slideShow.getSlides();
for (XSLFSlide slide : slides) {
XSLFNotes notes = slide.getNotes();
for (XSLFShape shape : notes) {
CTShape cTShape = (CTShape)shape.getXmlObject();
STPlaceholderType.Enum type = cTShape.getNvSpPr().getNvPr().getPh().getType();
System.out.println("type: " + type);
if (type == STPlaceholderType.BODY) { // get only shapes of type BODY
if (shape instanceof XSLFTextShape) {
XSLFTextShape textShape = (XSLFTextShape) shape;
for (XSLFTextParagraph paragraph : textShape) {
System.out.println(paragraph.getText());
}
}
}
}
}
}
}
可能的类型是 BODY、CHART、CLIP_ART、CTR_TITLE、DGM、DT、FTR、HDR、MEDIA、OBJ、PIC、SLD_IMG、SLD_NUM、SUB_TITLE、TBL、TITLE
。
不幸的是,没有任何关于公开可用的 ooxml 模式的文档。所以我们需要下载ooxml-schemas的源码然后执行 javadoc
形成那些以获得描述类和方法的 API 文档。
然后我们找到 org.openxmlformats.schemas.presentationml.x2006.main.*
类,它们是 Office Open XML
的表示部分的类。可以查看由 javadoc
创建的 API
文档中的 /org/openxmlformats/schemas/presentationml/x2006/main/CTShape.html
然后继续 getNvSpPr()
- getNvPr()
- getPh()
- getType()
。
使用当前的 apache poi 4.1.0
有一个枚举 Placeholder在也可以使用的高级 API
中。
例子:
import java.io.FileInputStream;
import org.apache.poi.xslf.usermodel.*;
import org.apache.poi.sl.usermodel.Placeholder;
import java.util.List;
public class PowerPointReadNotesHL {
public static void main(String[] args) throws Exception {
XMLSlideShow slideShow = new XMLSlideShow(new FileInputStream("PowerPointHavingNotes.pptx"));
List<XSLFSlide> slides = slideShow.getSlides();
for (XSLFSlide slide : slides) {
XSLFNotes notes = slide.getNotes();
for (XSLFShape shape : notes) {
Placeholder placeholder = shape.getPlaceholder();
System.out.println("placeholder: " + placeholder);
if (placeholder == Placeholder.BODY) { // get only shapes of type BODY
if (shape instanceof XSLFTextShape) {
XSLFTextShape textShape = (XSLFTextShape) shape;
for (XSLFTextParagraph paragraph : textShape) {
System.out.println(paragraph.getText());
}
}
}
}
}
}
}
那么直接使用低级 ooxml-schema
类就没有必要了。
关于java - 有没有办法使用 Apache poi 从给定的 PowerPoint 文件中准确获取演讲者笔记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57643266/