使用 python 脚本 (Unix) 解析 .doc (Word) 文件



可能的重复项:
从python中的MS Word文件中提取文本

我想解析(以便使用表达式执行搜索)带有 python 脚本的.doc文件。它运行在 unix 机器上。

谁能帮忙?

你可以使用,PyUno

样本

# HelloWorld python script for the scripting framework
def HelloWorldPython( ):
    """Prints the string 'Hello World(in Python)' into the current document"""
#get the doc from the scripting context which is made available to all scripts
    model = XSCRIPTCONTEXT.getDocument()
#get the XText interface
    text = model.Text
#create an XTextRange at the end of the document
    tRange = text.End
#and set the string
    tRange.String = "Hello World (in Python)"
    return None

其他, PyUNO 示例

你可以看看这个项目:python-docx。下载库后,您可以在 shell 中运行python example-extracttext.py docfile.docx textfile.txt | grep some-expression。当然,您也可以在必要时在python代码中进行更复杂的搜索。

python-docx的缺点是它目前只支持ms-Word 2007/2008,如果你关心你,我推荐antiword,它支持Microsoft Word版本2,6,7,97,2000,2002和2003。实际上,我一直在我的 vimrc 中使用它,以便能够在 VIM 编辑器中查看 ms-word 文件。虽然它不是python脚本,但它可以很容易地从Python调用。

最新更新