gpt4 book ai didi

java - 使用 Apache poi 从 docx 获取文本样式

转载 作者:搜寻专家 更新时间:2023-11-01 03:20:44 25 4
gpt4 key购买 nike

我正在尝试从 MS docx 文件中获取样式信息,我在编写带有粗体、斜体等添加样式的文件内容时没有问题。字体大小等,但是读取文件内容和获取样式信息不是很清楚。我试过使用 XWPFDocument,这个 API 似乎没有读取样式的能力。我现在正在尝试 XWPFWordExtractor,它看起来更有前途,但我仍然无法获取文本的样式信息。

我阅读的内容类型类似于以下内容。

“你好,这是粗体文本,这是斜体文本,这是粗斜体文本 "

任何指向示例的指针都会很棒。

最佳答案

好的,根据 Gagravarr 的评论,解决方案如下,完全符合我的要求。所以基本上 Gagravarr 回答了这个问题,但我不确定除了说它听到给他信用之外还有什么。

for (XWPFParagraph paragraph : docx.getParagraphs()) {
int pos = 0;
for (XWPFRun run : paragraph.getRuns()) {
System.out.println("Current run IsBold : " + run.isBold());
System.out.println("Current run IsItalic : " + run.isItalic());
for (char c : run.text().toCharArray()) {

System.out.print(c);
pos++;
}
System.out.println();
}
}

`

输出如下


当前运行 IsBold : false
当前运行 IsItalic : false
“你好,这是
当前运行 IsBold : true
当前运行 IsItalic : false
粗体文字
当前运行 IsBold : false
当前运行 IsItalic : false
这是
当前运行 IsBold : false
当前运行 IsItalic : true
斜体文字
当前运行 IsBold : false
当前运行 IsItalic : false
一种
当前运行 IsBold : false
当前运行 IsItalic : false
n
当前运行 IsBold : false
当前运行 IsItalic : false
d 这是
当前运行 IsBold : true
当前运行 IsItalic : true
粗斜体文本
当前运行 IsBold : false
当前运行 IsItalic : false
"

关于java - 使用 Apache poi 从 docx 获取文本样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31234497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com