python 提取word文件中信息_从python中的MS word文件中提取文本

这是一个老问题吗? 我相信这样的事情不存在。 只有回答和未回答的问题。 这一个是相当没有答案,或者如果你愿意,一半答案。 那么,不使用COM互操作来阅读* .docx(MS Word 2007及更高版本)文档的方法都会被覆盖。 但是,仅使用Python从* .doc(MS Word 97-2000)中提取文本的方法缺少。 这是复杂的吗? 这样做:不是真的,要明白:那是另一回事。

当我没有找到任何完成的代码时,我阅读了一些格式规范,并挖掘出了一些其他语言提出的算法。

MS Word(* .doc)文件是一个OLE2复合文件。 不要打扰你很多不必要的细节,把它想象成存储在文件中的文件系统。它实际上使用FAT结构,所以定义成立。 (嗯,也许你可以在Linux上循环挂载它) 这样,你可以在一个文件中存储更多的文件,比如图片等。 在* .docx中通过使用ZIP存档来完成相同的操作。 PyPI上有可用于读取OLE文件的软件包。像(olefile,compoundfiles,...) 我使用了compoundfiles包来打开* .doc文件。 但是,在MS Word 97-2000中,内部子文件不是XML或HTML,而是二进制文件。 由于这还不够,每个都包含关于其他人的信息,所以您必须至少阅读其中的两个,并相应地解开存储的信息。 要充分理解,请阅读我从中采用该算法的PDF文档。

下面的代码是很匆忙编写和测试少量的文件。据我所知,它按预期工作。 有时会出现一些乱码,并且几乎总是出现在文本的末尾。 中间也可能有一些奇怪的字符。

那些只希望搜索文字的人会很高兴。 不过,我敦促任何能够帮助改进此代码的人这样做。

doc2text module:

"""

This is Python implementation of C# algorithm proposed in:

http://b2xtranslator.sourceforge.net/howtos/How_to_retrieve_text_from_a_binary_doc_file.pdf

Python implementation author is Dalen Bernaca.

Code needs refining and probably bug fixing!

As I am not a C# expert I would like some code rechecks by one.

Parts of which I am uncertain are:

* Did the author of original algorithm used uint32 and int32 when unpacking correctly?

I copied each occurence as in original algo.

* Is the FIB length for MS Word 97 1472 bytes as in MS Word 2000, and would it make any difference if it is not?

* Did I interpret each C# command correctly?

I think I did!

"""

from compoundfiles import CompoundFileReader, CompoundFileError

from struct import unpack

__all__ = ["doc2text"]

def doc2text (path):

text = u""

cr = CompoundFileReader(path)

# Load WordDocument stream:

try:

f = cr.open("WordDocument")

doc = f.read()

f.close()

except: cr.close(); raise CompoundFileError, "The file is corrupted or it is not a Word document at all."

# Extract file information block and piece table stream informations from it:

fib = doc[:1472]

fcClx = unpack("L", fib[0x01a2l:0x01a6l])[0]

lcbClx = unpack("L", fib[0x01a6l:0x01a6+4l])[0]

tableFlag = unpack("L", fib[0x000al:0x000al+4l])[0] & 0x0200l == 0x0200l

tableName = ("0Table", "1Table")[tableFlag]

# Load piece table stream:

try:

f = cr.open(tableName)

table = f.read()

f.close()

except: cr.close(); raise CompoundFileError, "The file is corrupt. '%s' piece table stream is missing." % tableName

cr.close()

# Find piece table inside a table stream:

clx = table[fcClx:fcClx+lcbClx]

pos = 0

pieceTable = ""

lcbPieceTable = 0

while True:

if clx[pos]=="\x02":

# This is piece table, we store it:

lcbPieceTable = unpack("l", clx[pos+1:pos+5])[0]

pieceTable = clx[pos+5:pos+5+lcbPieceTable]

break

elif clx[pos]=="\x01":

# This is beggining of some other substructure, we skip it:

pos = pos+1+1+ord(clx[pos+1])

else: break

if not pieceTable: raise CompoundFileError, "The file is corrupt. Cannot locate a piece table."

# Read info from pieceTable, about each piece and extract it from WordDocument stream:

pieceCount = (lcbPieceTable-4)/12

for x in xrange(pieceCount):

cpStart = unpack("l", pieceTable[x*4:x*4+4])[0]

cpEnd = unpack("l", pieceTable[(x+1)*4:(x+1)*4+4])[0]

ofsetDescriptor = ((pieceCount+1)*4)+(x*8)

pieceDescriptor = pieceTable[ofsetDescriptor:ofsetDescriptor+8]

fcValue = unpack("L", pieceDescriptor[2:6])[0]

isANSII = (fcValue & 0x40000000) == 0x40000000

fc = fcValue & 0xbfffffff

cb = cpEnd-cpStart

enc = ("utf-16", "cp1252")[isANSII]

cb = (cb*2, cb)[isANSII]

text += doc[fc:fc+cb].decode(enc, "ignore")

return "\n".join(text.splitlines())

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值