有没有办法使用 Apache poi 从给定的 PowerPoint 文件中准确获取演讲者笔记?
Is there a way to get speaker notes accurately from a given PowerPoint file with Apache poi?
我正在尝试使用 apache poi 将演讲者笔记从一个 powerpoint 传输到另一个 powerpoint,但无法准确传输。
找了一圈,没找到多少资源。我确实找到了这个 link: 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 循环是我必须开始过滤掉一些实际上不是演讲者笔记的 "speaker" 笔记的地方(例如,幻灯片编号以某种方式被解释为笔记;还打印了这个版权符号)。
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");
下面的语句是另一个过滤器;如果涉及母版幻灯片的功能,一段奇怪的 "click to edit master text styles..." 文本也会被解释为演讲者注释。
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", ""));
}}}}}}
简而言之,不是演讲者笔记的内容显示为 "notes"。关于这个主题的在线资源不多;有人可以帮忙吗?
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 模式的文档 public 可用。因此,我们需要下载 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
在高级 API
中有一个枚举 Placeholder 也可以使用。
示例:
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
类就没有必要了
我正在尝试使用 apache poi 将演讲者笔记从一个 powerpoint 传输到另一个 powerpoint,但无法准确传输。
找了一圈,没找到多少资源。我确实找到了这个 link: 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 循环是我必须开始过滤掉一些实际上不是演讲者笔记的 "speaker" 笔记的地方(例如,幻灯片编号以某种方式被解释为笔记;还打印了这个版权符号)。
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");
下面的语句是另一个过滤器;如果涉及母版幻灯片的功能,一段奇怪的 "click to edit master text styles..." 文本也会被解释为演讲者注释。
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", ""));
}}}}}}
简而言之,不是演讲者笔记的内容显示为 "notes"。关于这个主题的在线资源不多;有人可以帮忙吗?
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 模式的文档 public 可用。因此,我们需要下载 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
在高级 API
中有一个枚举 Placeholder 也可以使用。
示例:
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
类就没有必要了