Bad*_*dri 27 python linux ms-word
为了在python中使用MS word文件,有python win32扩展,可以在windows中使用.我如何在linux中做同样的事情?有图书馆吗?
mik*_*ana 31
使用本机Python docx模块.以下是如何从doc中提取所有文本:
document = docx.Document(filename)
docText = '\n\n'.join([
paragraph.text.encode('utf-8') for paragraph in document.paragraphs
])
print docText
Run Code Online (Sandbox Code Playgroud)
还可以查看Textract,它可以拉出表格等.
使用正则表达式解析XML会调用cthulu.不要这样做!
Cha*_*had 14
本杰明的回答非常好.我刚刚巩固了......
import zipfile, re
docx = zipfile.ZipFile('/path/to/file/mydocument.docx')
content = docx.read('word/document.xml').decode('utf-8')
cleaned = re.sub('<(.|\n)*?>','',content)
print(cleaned)
Run Code Online (Sandbox Code Playgroud)
我知道这是一个老问题,但我最近试图找到一种从MS word文件中提取文本的方法,到目前为止我发现的最佳解决方案是使用wvLib:
http://wvware.sourceforge.net/
安装库之后,在Python中使用它非常简单:
import commands
exe = 'wvText ' + word_file + ' ' + output_txt_file
out = commands.getoutput(exe)
exe = 'cat ' + output_txt_file
out = commands.getoutput(exe)
Run Code Online (Sandbox Code Playgroud)
就是这样.实际上,我们正在做的是使用commands.getouput函数来运行几个shell脚本,即wvText(从Word文档中提取文本,以及cat来读取文件输出).之后,Word文档中的整个文本将出现在out变量中,随时可以使用.
希望这将有助于将来遇到类似问题的任何人.
| 归档时间: |
|
| 查看次数: |
56096 次 |
| 最近记录: |