问题描述
我有一个json,其中以base64格式存储各种文件类型(例如pdf,docx,doc)。因此,我已经能够成功转换pdf和docx文件,并通过将它们传递到内存中来读取它们的内容,而不是将它们转换为物理文件然后读取它们。但是,我无法使用文档文件执行此操作。
有人能指出我正确的方向吗?我在Windows上,尝试使用textract
,但无法使用该库。我愿意接受其他解决方案。
#This works using a docx file
resume = (df.iloc[180]['Candidate_Resume_Attachment_Base64_Image'])
resume_bytes = resume.encode('ascii')
decoded = base64.decodebytes(resume_bytes)
result = BytesIO()
result.write(decoded)
docxReader = docx2txt.process(result)
#This does not working using a doc file
message=((df.iloc[361]['Candidate_Resume_Attachment_Base64_Image']))
resume_bytes = message.encode('ascii')
decoded = base64.decodebytes(resume_bytes)
result = BytesIO()
result.write(decoded)
word = win32.gencache.Ensuredispatch('Word.Application')
word.Visible = False
doc = word.Documents.Open(result)
#error:
ret = self._oleobj_.InvokeTypes(19,LCID,1,(13,0),((16396,1),(16396,17),17)),FileName
com_error: (-2147352571,'Type mismatch.',None,16)
解决方法
万一其他人需要读取内存中的doc文件,这是我的棘手解决方案,直到找到更好的解决方案为止。
1)使用olefile库读取doc文件,这会导致unicode中的字符混合。 2)使用正则表达式捕获文本。
import olefile
#retrieve base64 image and decode into bytes,in this case from a df
message = row['text']
text_bytes = message.encode('ascii')
decoded = base64.decodebytes(text_bytes)
#write in memory
result = BytesIO()
result.write(decoded)
#open and read file
ole=olefile.OleFileIO(result)
y = ole.openstream('WordDocument').read()
y=y.decode('latin-1',errors='ignore')
#replace all characters that are not part of the unicode list below (all latin characters) and spaces with an Astrisk. This can probably be shortened using a similar pattern used in the next step and combining them
y=(re.sub(r'[^\x0A,\u00c0-\u00d6,\u00d8-\u00f6,\u00f8-\u02af,\u1d00-\u1d25,\u1d62-\u1d65,\u1d6b-\u1d77,\u1d79-\u1d9a,\u1e00-\u1eff,\u2090-\u2094,\u2184-\u2184,\u2488-\u2490,\u271d-\u271d,\u2c60-\u2c7c,\u2c7e-\u2c7f,\ua722-\ua76f,\ua771-\ua787,\ua78b-\ua78c,\ua7fb-\ua7ff,\ufb00-\ufb06,\x20-\x7E]',r'*',y))
#Isolate the body of the text from the rest of the gibberish
p=re.compile(r'\*{300,433}((?:[^*]|\*(?!\*{14}))+?)\*{15,}')
result=(re.findall(p,y))
#remove * left in the capture group
result = result[0].replace('*','')
对我来说,我需要确保在解码过程中不会丢失重音字符,并且由于我的文档使用英语,西班牙语和葡萄牙语,所以我选择使用latin-1进行解码。在这里,我使用正则表达式模式来标识所需的文本。解码后,我发现在所有文档中,捕获组都以〜400'*'和'':开头。不确定使用此方法解码时这是否是所有文档文档的规范,但我以此为起点创建正则表达式模式以将所需的文本与其余的乱码隔离。