Take the 2-minute tour ×

for working with MS word files in python, there is python win32 extensions, which can be used in windows. How do I do the same in linux? Is there any library?

share|improve this question
add comment

11 Answers

up vote 13 down vote accepted

You could make a subprocess call to antiword. Antiword is a linux commandline utility for dumping text out of a word doc. Works pretty well for simple documents (obviously it loses formatting). It's available through apt, and probably as RPM, or you could compile it yourself.

share|improve this answer
add comment

OpenOffice.org can be scripted with Python: see here.

Since OOo can load most MS Word files flawlessly, I'd say that's your best bet.

share|improve this answer
Not flawlessly. Close, but far from flawless in my experience (OO 2.0 - 3.0). –  SpliFF May 26 '09 at 15:17
As flawless as MS Word N+1 opens MS Words N files, and way better than MS Word N+1 opens MS Words N-1 files, IMHO –  voyager Sep 29 '09 at 14:50
add comment

Use the native Python docx module. Here's how to extract all the text from a doc:

document = opendocx('Hello world.docx')

# This location is where most document content lives 
docbody = document.xpath('/w:document/w:body', namespaces=wordnamespaces)[0]

# Extract all text
print getdocumenttext(document)

See http://github.com/mikemaccana/python-docx

Parsing XML with regexs invokes cthulu. Don't do it!

share|improve this answer
add comment

I know this is an old question, but I was recently trying to find a way to extract text from MS word files, and the best solution by far I found was with wvLib:


After installing the library, using it in Python is pretty easy:

import commands

exe = 'wvText ' + word_file + ' ' + output_txt_file
out = commands.getoutput(exe)
exe = 'cat ' + output_txt_file
out = commands.getoutput(exe)

And that's it. Pretty much, what we're doing is using the commands.getouput function to run a couple of shell scripts, namely wvText (which extracts text from a Word document, and cat to read the file output). After that, the entire text from the Word document will be in the out variable, ready to use.

Hopefully this will help anyone having similar issues in the future.

share|improve this answer
add comment

benjamin's answer is a pretty good one. I have just consolidated...

import zipfile, re

docx = zipfile.ZipFile('/path/to/file/mydocument.docx')
content = docx.read('word/document.xml')
cleaned = re.sub('<(.|\n)*?>','',content)
print cleaned
share|improve this answer
I should reiterate this only works for docx (Word 2007 or later). For .doc files wvware is your best bet. Depending on your environment it can be a pain to setup, but it does do a very nice job. –  Chad Dec 28 '09 at 3:41
To remove XML entities like &nbsp; from 'text': >>>from xml.sax.saxutils import unescape >>>text=unescape(cleaned) –  aitchnyu Aug 1 '11 at 8:06
add comment

Take a look at how the doc format works and create word document using PHP in linux. The former is especially useful. Abiword is my recommended tool. There are limitations though:

However, if the document has complicated tables, text boxes, embedded spreadsheets, and so forth, then it might not work as expected. Developing good MS Word filters is a very difficult process, so please bear with us as we work on getting Word documents to open correctly. If you have a Word document which fails to load, please open a Bug and include the document so we can improve the importer.

share|improve this answer
Not just that though! Even the most basic text saved in the Word 97 format is nearly impossible to get at easily without relying on word to do it for you (COM). Most word documents are not HTML! –  William Keller Sep 24 '08 at 3:30
Abiword doesn't assume that it's a HTML document, and considering how extensive the tool is...I don't think it was "easy" to implement it. Abiword is a tool that helps you to read MS Word files...and since the author is concerned with text retrieval, this suffices. –  Swati Sep 24 '08 at 3:42
Ah, I'd always thought that abiword was just another word processor! Man, that would have saved me some headaches awhile back. –  William Keller Sep 24 '08 at 12:11
add comment

(Note: I posted this on this question as well, but it seems relevant here, so please excuse the repost.)

Now, this is pretty ugly and pretty hacky, but it seems to work for me for basic text extraction. Obviously to use this in a Qt program you'd have to spawn a process for it etc, but the command line I've hacked together is:

unzip -p file.docx | grep '<w:t' | sed 's/<[^<]*>//g' | grep -v '^[[:space:]]*$'

So that's:

unzip -p file.docx: -p == "unzip to stdout"

grep '<w:t': Grab just the lines containing '<w:t' (<w:t> is the Word 2007 XML element for "text", as far as I can tell)

sed 's/<[^<]>//g'*: Remove everything inside tags

grep -v '^[[:space:]]$'*: Remove blank lines

There is likely a more efficient way to do this, but it seems to work for me on the few docs I've tested it with.

As far as I'm aware, unzip, grep and sed all have ports for Windows and any of the Unixes, so it should be reasonably cross-platform. Despit being a bit of an ugly hack ;)

share|improve this answer
add comment

If your intention is to use purely python modules without calling a subprocess, you can use the zipfile python modude.

content = ""
# Load DocX into zipfile
docx = zipfile.ZipFile('/home/whateverdocument.docx')
# Unpack zipfile
unpacked = docx.infolist()
# Find the /word/document.xml file in the package and assign it to variable
for item in unpacked:
    if item.orig_filename == 'word/document.xml':
        content = docx.read(item.orig_filename)


Your content string however needs to be cleaned up, one way of doing this is:

# Clean the content string from xml tags for better search
fullyclean = []
halfclean = content.split('<')
for item in halfclean:
    if '>' in item:
        bad_good = item.split('>')
        if bad_good[-1] != '':

# Assemble a new string with all pure content
content = " ".join(fullyclean)

But there is surely a more elegant way to clean up the string, probably using the re module. Hope this helps.

share|improve this answer
To remove XML entities like &nbsp; from 'text': >>>from xml.sax.saxutils import unescape >>>text=unescape(content) –  aitchnyu Aug 1 '11 at 8:05
add comment

Unoconv might also be a good alternative: http://linux.die.net/man/1/unoconv

share|improve this answer
add comment

I'm not sure if you're going to have much lock without using COM. The .doc format is ridiculously complex, and is often called a "memory dump" of Word at the time of saving!

At Swati, that's in HTML, which is fine and dandy, but most word documents aren't so nice!

share|improve this answer
Added limitations :) –  Swati Sep 24 '08 at 3:22
add comment

Just an option for reading 'doc' files without using COM: miette. Should work on any platform.

share|improve this answer
add comment

Your Answer


