POI 忽略了一些 docx 片段
POI ignoring some snippets of docx
我正在尝试使用此代码 (POI 3.11) 从 docx 文件中提取文本:
XWPFDocument doc = new XWPFDocument(OPCPackage.open("sample.docx"));
for (XWPFParagraph p : doc.getParagraphs()) {
List<XWPFRun> runs = p.getRuns();
if (runs != null) {
for (XWPFRun r : runs) {
String text = r.getText(0);
System.out.println(text);
}
}
}
这是 sample.docx 中的 document.xml 文件:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing">
<w:body>
<w:p>
<w:pPr>
<w:pStyle w:val="Normal"/>
<w:rPr/>
</w:pPr>
<w:bookmarkStart w:id="0" w:name="__DdeLink__59_1605705532"/>
<w:bookmarkEnd w:id="0"/>
<w:r>
<w:rPr/>
<w:t>A</w:t> // THIS PRINT!
<w:tab/>
<w:t>B</w:t> // THIS IS NOT! WHY?!
</w:r>
</w:p>
<w:sectPr>
<w:type w:val="nextPage"/>
<w:pgSz w:h="16838" w:w="11906"/>
<w:pgMar w:bottom="1134" w:footer="0" w:gutter="0" w:header="0" w:left="1134" w:right="1134" w:top="1134"/>
<w:pgNumType w:fmt="decimal"/>
<w:formProt w:val="false"/>
<w:textDirection w:val="lrTb"/>
<w:docGrid w:charSpace="4294961151" w:linePitch="240" w:type="default"/>
</w:sectPr>
</w:body>
</w:document>
当我运行代码时,结果如下:
A
我不明白为什么,但出于未知原因,文本中的一些片段(字母 B)被忽略了(如果我使用 LibreOffice 打开文件,它显示成功)。
看着这个 link (http://apache-poi.1045710.n5.nabble.com/POI-3-10-1-XWPFRun-getText-Does-Not-Return-Full-Line-of-Text-tp5716539p5716541.html),我发现每个段落可能有多个运行(文本片段)并且每个段落可能有不同的样式或没有,具体取决于文件的历史。
在这一期中,文件中有一个段落包含两个文本片段。 String text = r.getText(0)
我只抓到一只。
我怎么在 API 中找不到 returns 一个段落的所有片段的方法,我需要做一个解决方法来解决:
if (runs != null) {
for (XWPFRun r : runs) {
int i = 0;
while (true) {
try {
String text = r.getText(i);
if (text == null) {
break;
}
System.out.println(text);
i++;
} catch (IndexOutOfBoundsException ex) {
break;
}
}
}
}
我希望这能对某人有所帮助!
我正在尝试使用此代码 (POI 3.11) 从 docx 文件中提取文本:
XWPFDocument doc = new XWPFDocument(OPCPackage.open("sample.docx"));
for (XWPFParagraph p : doc.getParagraphs()) {
List<XWPFRun> runs = p.getRuns();
if (runs != null) {
for (XWPFRun r : runs) {
String text = r.getText(0);
System.out.println(text);
}
}
}
这是 sample.docx 中的 document.xml 文件:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing">
<w:body>
<w:p>
<w:pPr>
<w:pStyle w:val="Normal"/>
<w:rPr/>
</w:pPr>
<w:bookmarkStart w:id="0" w:name="__DdeLink__59_1605705532"/>
<w:bookmarkEnd w:id="0"/>
<w:r>
<w:rPr/>
<w:t>A</w:t> // THIS PRINT!
<w:tab/>
<w:t>B</w:t> // THIS IS NOT! WHY?!
</w:r>
</w:p>
<w:sectPr>
<w:type w:val="nextPage"/>
<w:pgSz w:h="16838" w:w="11906"/>
<w:pgMar w:bottom="1134" w:footer="0" w:gutter="0" w:header="0" w:left="1134" w:right="1134" w:top="1134"/>
<w:pgNumType w:fmt="decimal"/>
<w:formProt w:val="false"/>
<w:textDirection w:val="lrTb"/>
<w:docGrid w:charSpace="4294961151" w:linePitch="240" w:type="default"/>
</w:sectPr>
</w:body>
</w:document>
当我运行代码时,结果如下:
A
我不明白为什么,但出于未知原因,文本中的一些片段(字母 B)被忽略了(如果我使用 LibreOffice 打开文件,它显示成功)。
看着这个 link (http://apache-poi.1045710.n5.nabble.com/POI-3-10-1-XWPFRun-getText-Does-Not-Return-Full-Line-of-Text-tp5716539p5716541.html),我发现每个段落可能有多个运行(文本片段)并且每个段落可能有不同的样式或没有,具体取决于文件的历史。
在这一期中,文件中有一个段落包含两个文本片段。 String text = r.getText(0)
我只抓到一只。
我怎么在 API 中找不到 returns 一个段落的所有片段的方法,我需要做一个解决方法来解决:
if (runs != null) {
for (XWPFRun r : runs) {
int i = 0;
while (true) {
try {
String text = r.getText(i);
if (text == null) {
break;
}
System.out.println(text);
i++;
} catch (IndexOutOfBoundsException ex) {
break;
}
}
}
}
我希望这能对某人有所帮助!