如何在Python中读取.doc文件

时间:2018-06-13 10:21:17

标签: python file filereader

我有这个文件是.doc扩展名并包含表格中的信息。 在python中有任何方法,以便我可以将所有数据复制到文本文件(.txt)

1 个答案:

答案 0 :(得分:0)

它被复制了一个 我只是将所有答案整合到一个地方

对于Linux用户 使用不在Windows中的textract库

import textract
text = textract.process("path/to/file.extension")
text = text.decode("utf-8") 

对于Windows用户,如果用户知道编码

from bs4 import BeautifulSoup as bs
soup = bs(open(filename).read())
[s.extract() for s in soup(['style', 'script'])]
tmpText = soup.get_text()
text = "".join("".join(tmpText.split('\t')).split('\n')).encode('utf-8').strip()
print text

仅适用于Windows用户

import win32com.client

word = win32com.client.Dispatch("Word.Application")
word.visible = False
wb = word.Documents.Open("myfile.doc")
doc = word.ActiveDocument
print(doc.Range().Text)