【问题标题】:How to parse just the text from a Word Doc using Python?如何使用 Python 仅解析 Word Doc 中的文本?
【发布时间】:2010-02-14 15:53:52
【问题描述】:

当您尝试打开 MS Word 文档或大多数 Windows 文件格式时,您会看到下面给出的乱码被实际文本间歇性地破坏。我需要提取输入的文本并希望忽略乱码——如下所示。我如何只提取重要的文本,而忽略其余的内容。请指教。

这是一个 word doc 的open("sample.doc",r").read() 示例。谢谢

00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00In an Interesting news,his is the first time we polled Indian channel community for their preferred memory supplier. Transcend came a close second, was seen to be more popular among class A city based resellers, was also the most recalled memory brand among customers according to resellers. However Transcend channels complained of parallel imports and constant unavailability of the products in grey x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x

【问题讨论】:

    标签: python regex ms-word screen-scraping


    【解决方案1】:

    似乎最可行的工具是OleFileIO,尤其是在您需要全 python 解决方案时。

    【讨论】:

    • 以上适用于.DOC文件。 .DOCX 文件只是压缩的 xml 文件。
    【解决方案2】:

    doc 是一种二进制格式,它不是一种标记语言或其他东西。 规格:http://www.microsoft.com/interop/docs/OfficeBinaryFormats.mspx

    【讨论】:

    • 链接当前已失效。
    【解决方案3】:

    没有通用的提取原因 来自每种文件格式的信息。 你需要知道格式才能知道 如何提取信息。

    只是想先声明一下。因此,您应该寻找可以转换/提取所需信息的库和软件。正如 Ofir 所提到的,微软为其格式提供了相应的工具。

    但是,如果您无法做到这一点,并且想冒险在文件中看到您认为有趣的文本,您可以进行正常读取并查找将构建文本的字节序列。那么问题来了,在寻找文本时我应该支持哪些语言/字符集。是多字节文本吗?

    简单的开始是遍历数据并查找 [a-zA-z0-9_- ] 的序列来查找文本。但是 word 可能是多字节的。所以你应该将双字节扫描为一个字符。

    注意:一些新格式如 open office 和 docx 是压缩容器中的多个文件。所以你需要先解压缩文件,然后在你要找的文本之后扫描XML文档。

    【讨论】:

      【解决方案4】:

      Word 文档是一种压缩格式。你需要先解压它才能得到真实的数据(尝试在winrar之类的程序中打开一个doc文件,你会看到它包含多个文件。

      它甚至似乎是 XML,所以读取格式应该不会那么难,虽然我不确定你是否通过这种方式获取所有数据。

      【讨论】:

        【解决方案5】:

        我遇到了类似的问题,需要查询数百个 Word 文档。我将 Word 文件转换为文本文件并使用普通的文本解析工具。效果很好。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-11-08
          • 1970-01-01
          • 1970-01-01
          • 2014-01-04
          • 1970-01-01
          • 2017-02-02
          • 2016-04-05
          • 2017-04-18
          相关资源
          最近更新 更多