使用 Java 将 Microsoft Word 文档读取为纯文本(DOC、DOCX)

发布于 2024-08-21 07:31:18 字数 96 浏览 3 评论 0原文

我正在寻找 Java 中的东西来读取 Word 文档以处理其文本。我所需要的只是文本,没有什么花哨的。我了解 Apache POI,但它现在不包括对 DOCX 的支持,有什么吗?

I'm looking for something in Java to read in Word documents to process their text.. all I need is there text, nothing fancy. I know about Apache POI, however it doesn't include support for DOCX right now, anything out there?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

痴意少年 2024-08-28 07:31:18

如果您不需要格式化信息、图像和所有其他花哨的东西,那么这项工作就会容易得多。只需 5 到 10 行代码即可。

  1. 将 DOCX 视为 zip 文件。它由一堆文件组成,其中包括“document.xml”。使用 ZipInputStream 并单独提取该文件。 (您可以使用您最喜欢的 zip 实用程序并打开 docx 并亲自查看!)
  2. 使用 SAX 解析器并读取节点 body/p/r/t 之间的内容 - 瞧,您得到了文本!

仅当您仅需要文本时才适用。

If you don't require formatting information, images and all other fancy stuff, then the job is lot easier. Just some 5 to 10 lines of code will do.

  1. Treat DOCX as a zip file. It consists a bunch of files which includes 'document.xml'. Use ZipInputStream and extract that file alone. (you may use your favorite zip utility and open docx and see for yourself!)
  2. Use a SAX parser and read contents between node body/p/r/t - voila you got the text!

This is applicable only if you need the text only.

伊面 2024-08-28 07:31:18

通过谷歌搜索,我发现了 OpenXML4J。这可能会解决您的问题。在我确信社区中的某个人会有更好的见解之前,我还没有使用过它。

注意:这是一个重复的问题。这有解决方案以及一些讨论。 问题链接。

With some googling I found OpenXML4J. This might solve your issue. I have not used this before I am sure someone in the community will have better insight.

Note: This is a duplicate question. This has the solution plus a bit of discussion. Link to the question.

淤浪 2024-08-28 07:31:18

尝试 apache poi - 它可以处理 doc、docx、xls、xlsx、ppt、pptx。

另一个生产级解决方案是无头模式下的 OpenOffice,它甚至可以在服务器端场景中使用。

Try apache poi - it can handle doc, docx, xls, xlsx, ppt, pptx.

Another production-level solution is OpenOffice in headless mode which can even be used in a server-side scenario.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文