如何使用Python解析Word文档中的文本?

2024-06-30 16:46:46 发布

您现在位置:Python中文网/ 问答频道 /正文

当你尝试打开一个msword文档或大多数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

Tags: oftheto文档文本内容docwindows
3条回答

There is no generic why to extract information from every file format. You need to know the format to know how to extract the information.

只是想先说明一下。因此,您应该寻找能够转换/提取所需信息的库和软件。正如Ofir提到的,微软已经为他们的格式提供了相应的工具。在

但是如果你不能这样做,并且想冒险在文件中看到你认为有趣的文本,你可以做一个普通的读取,并寻找将建立文本的字节序列。接下来的问题是,在我的文本搜索中,我应该支持哪些语言/字符集。是多字节文本吗?在

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

注意:一些新格式,如openoffice和docx是压缩容器中的多个文件。因此,您需要先对文件进行反压缩,然后在查找的文本之后扫描XML文档。在

doc是一种二进制格式,它不是标记语言之类的。 规格:http://www.microsoft.com/interop/docs/OfficeBinaryFormats.mspx

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

相关问题 更多 >