<p>Is there a reasonable way to extract plain text from a Word file that doesn't depend on COM automation?  (This is a a feature for a web app deployed on a non-Windows platform - that's non-negotiable in this case.)</p>

<p>Antiword seems like it might be a reasonable option, but it seems like it might be abandoned.</p>

<p>A Python solution would be ideal, but doesn't appear to be available.</p>

[{'answer_id': 42485, 'author': 'Unsliced', 'author_id': 2902, 'author_profile': 'https://Stackoverflow.com/users/2902', 'pm_score': 1, 'selected': False, 'text': '<p>Open Office has an <a href="http://api.openoffice.org/" rel="nofollow noreferrer">API</a></p>\n'}, {'answer_id': 43301, 'author': 'paulmorriss', 'author_id': 2983, 'author_profile': 'https://Stackoverflow.com/users/2983', 'pm_score': 2, 'selected': False, 'text': '<p>Using the OpenOffice API, and Python, and <a href="http://www.pitonyak.org/oo.php" rel="nofollow noreferrer">Andrew Pitonyak\'s excellent online macro book</a> I managed to do this. Section 7.16.4 is the place to start.</p>\n\n<p>One other tip to make it work without needing the screen at all is to use the Hidden property:</p>\n\n<pre><code>RO = PropertyValue(\'ReadOnly\', 0, True, 0)\nHidden = PropertyValue(\'Hidden\', 0, True, 0)\nxDoc = desktop.loadComponentFromURL( docpath,"_blank", 0, (RO, Hidden,) )\n</code></pre>\n\n<p>Otherwise the document flicks up on the screen (probably on the webserver console) when you open it.</p>\n'}, {'answer_id': 43364, 'author': 'codeape', 'author_id': 3571, 'author_profile': 'https://Stackoverflow.com/users/3571', 'pm_score': 5, 'selected': True, 'text': '<p>I use catdoc or antiword for this, whatever gives the result that is the easiest to parse. I have embedded this in python functions, so it is easy to use from the parsing system (which is written in python).</p>\n\n<pre><code>import os\n\ndef doc_to_text_catdoc(filename):\n    (fi, fo, fe) = os.popen3(\'catdoc -w "%s"\' % filename)\n    fi.close()\n    retval = fo.read()\n    erroroutput = fe.read()\n    fo.close()\n    fe.close()\n    if not erroroutput:\n        return retval\n    else:\n        raise OSError("Executing the command caused an error: %s" % erroroutput)\n\n# similar doc_to_text_antiword()\n</code></pre>\n\n<p>The -w switch to catdoc turns off line wrapping, BTW.</p>\n'}, {'answer_id': 1387028, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>For docx files, check out the Python script docx2txt available at</p>\n\n<p><a href="http://cobweb.ecn.purdue.edu/~kak/distMisc/docx2txt" rel="nofollow noreferrer">http://cobweb.ecn.purdue.edu/~kak/distMisc/docx2txt</a></p>\n\n<p>for extracting the plain text from a docx document.</p>\n'}, {'answer_id': 1979931, 'author': 'mikemaccana', 'author_id': 123671, 'author_profile': 'https://Stackoverflow.com/users/123671', 'pm_score': 4, 'selected': False, 'text': '<p>(Same answer as <a href="https://stackoverflow.com/questions/125222/extracting-text-from-ms-word-files-in-python">extracting text from MS word files in python</a>)</p>\n<p>Use the native Python docx module which I made this week. Here\'s how to extract all the text from a doc:</p>\n<pre><code>document = opendocx(\'Hello world.docx\')\n\n# This location is where most document content lives \ndocbody = document.xpath(\'/w:document/w:body\', namespaces=wordnamespaces)[0]\n\n# Extract all text\nprint getdocumenttext(document)\n</code></pre>\n<p>See <a href="https://python-docx.readthedocs.org/en/latest/" rel="nofollow noreferrer">Python DocX site</a></p>\n<p>100% Python, no COM, no .net, no Java, no parsing serialized XML with regexs.</p>\n'}, {'answer_id': 20663596, 'author': 'Etienne', 'author_id': 146481, 'author_profile': 'https://Stackoverflow.com/users/146481', 'pm_score': 2, 'selected': False, 'text': '<p>If all you want to do is extracting text from Word files (.docx), it\'s possible to do it only with Python. Like Guy Starbuck wrote it, you just need to unzip the file and then parse the XML. Inspired by <code>python-docx</code>, I have written a <a href="http://etienned.github.io/posts/extract-text-from-word-docx-simply/" rel="nofollow">simple function</a> to do this:</p>\n\n<pre><code>try:\n    from xml.etree.cElementTree import XML\nexcept ImportError:\n    from xml.etree.ElementTree import XML\nimport zipfile\n\n\n"""\nModule that extract text from MS XML Word document (.docx).\n(Inspired by python-docx &lt;https://github.com/mikemaccana/python-docx&gt;)\n"""\n\nWORD_NAMESPACE = \'{http://schemas.openxmlformats.org/wordprocessingml/2006/main}\'\nPARA = WORD_NAMESPACE + \'p\'\nTEXT = WORD_NAMESPACE + \'t\'\n\n\ndef get_docx_text(path):\n    """\n    Take the path of a docx file as argument, return the text in unicode.\n    """\n    document = zipfile.ZipFile(path)\n    xml_content = document.read(\'word/document.xml\')\n    document.close()\n    tree = XML(xml_content)\n\n    paragraphs = []\n    for paragraph in tree.getiterator(PARA):\n        texts = [node.text\n                 for node in paragraph.getiterator(TEXT)\n                 if node.text]\n        if texts:\n            paragraphs.append(\'\'.join(texts))\n\n    return \'\\n\\n\'.join(paragraphs)\n</code></pre>\n'}, {'answer_id': 30122626, 'author': 'markling', 'author_id': 2455413, 'author_profile': 'https://Stackoverflow.com/users/2455413', 'pm_score': 1, 'selected': False, 'text': '<p><a href="https://stackoverflow.com/questions/16516044/is-it-possible-to-read-word-files-doc-docx-in-python/30122239#30122239">This worked well</a> for .doc and .odt.</p>\n\n<p>It calls openoffice on the command line to convert your file to text, which you can then simply load into python.</p>\n\n<p>(It seems to have other format options, though they are not apparenlty documented.)</p>\n'}, {'answer_id': 51905584, 'author': 'Dhinesh kumar M', 'author_id': 7479213, 'author_profile': 'https://Stackoverflow.com/users/7479213', 'pm_score': 2, 'selected': False, 'text': '<p><strong>tika-python</strong></p>\n\n<p>A Python port of the Apache Tika library, According to the documentation Apache tika supports text extraction from over 1500 file formats.  </p>\n\n<p><strong>Note:</strong> It also works charmingly with <strong>pyinstaller</strong></p>\n\n<p>Install with pip :</p>\n\n<pre><code>pip install tika\n</code></pre>\n\n<p><strong>Sample:</strong></p>\n\n<pre><code>#!/usr/bin/env python\nfrom tika import parser\nparsed = parser.from_file(\'/path/to/file\')\nprint(parsed["metadata"]) #To get the meta data of the file\nprint(parsed["content"]) # To get the content of the file\n</code></pre>\n\n<p>Link to official <a href="https://github.com/chrismattmann/tika-python" rel="nofollow noreferrer">GitHub</a></p>\n'}, {'answer_id': 62974454, 'author': 'prossblad', 'author_id': 5012591, 'author_profile': 'https://Stackoverflow.com/users/5012591', 'pm_score': 0, 'selected': False, 'text': "<p>Honestly <strong>don't use &quot;pip install tika</strong>&quot;, this has been developed for mono-user (one developper working on his laptop) and not for multi-users (multi-developpers).</p>\n<p>The small class TikaWrapper.py bellow which uses Tika in command line is widely enough to meet our needs.</p>\n<p>You just have to instanciate this class with JAVA_HOME path and the Tika jar path, that's all ! And it works perfectly for lot of formats (e.g: PDF, DOCX, ODT, XLSX, PPT, etc.).</p>\n<pre><code>#!/bin/python\n# -*- coding: utf-8 -*-\n\n# Class to extract metadata and text from different file types (such as PPT, XLS, and PDF)\n# Developed by Philippe ROSSIGNOL\n#####################\n# TikaWrapper class #\n#####################\nclass TikaWrapper:\n\n    java_home = None\n    tikalib_path = None\n\n    # Constructor\n    def __init__(self, java_home, tikalib_path):\n        self.java_home = java_home\n        self.tika_lib_path = tikalib_path\n\n    def extractMetadata(self, filePath, encoding=&quot;UTF-8&quot;, returnTuple=False):\n        '''\n        - Description:\n          Extract metadata from a document\n        \n        - Params:\n          filePath: The document file path\n          encoding: The encoding (default = &quot;UTF-8&quot;)\n          returnTuple: If True return a tuple which contains both the output and the error (default = False)\n        \n        - Examples:\n          metadata = extractMetadata(filePath=&quot;MyDocument.docx&quot;)\n          metadata, error = extractMetadata(filePath=&quot;MyDocument.docx&quot;, encoding=&quot;UTF-8&quot;, returnTuple=True)\n        '''\n        cmd = self._getCmd(self._cmdExtractMetadata, filePath, encoding)\n        out, err = self._execute(cmd, encoding)\n        if (returnTuple): return out, err\n        return out\n\n    def extractText(self, filePath, encoding=&quot;UTF-8&quot;, returnTuple=False):\n        '''\n        - Description:\n          Extract text from a document\n        \n        - Params:\n          filePath: The document file path\n          encoding: The encoding (default = &quot;UTF-8&quot;)\n          returnTuple: If True return a tuple which contains both the output and the error (default = False)\n        \n        - Examples:\n          text = extractText(filePath=&quot;MyDocument.docx&quot;)\n          text, error = extractText(filePath=&quot;MyDocument.docx&quot;, encoding=&quot;UTF-8&quot;, returnTuple=True)\n        '''\n        cmd = self._getCmd(self._cmdExtractText, filePath, encoding)\n        out, err = self._execute(cmd, encoding)\n        return out, err\n\n    # ===========\n    # = PRIVATE =\n    # ===========\n\n    _cmdExtractMetadata = &quot;${JAVA_HOME}/bin/java -jar ${TIKALIB_PATH} --metadata ${FILE_PATH}&quot;\n    _cmdExtractText = &quot;${JAVA_HOME}/bin/java -jar ${TIKALIB_PATH} --encoding=${ENCODING} --text ${FILE_PATH}&quot;\n\n    def _getCmd(self, cmdModel, filePath, encoding):\n        cmd = cmdModel.replace(&quot;${JAVA_HOME}&quot;, self.java_home)\n        cmd = cmd.replace(&quot;${TIKALIB_PATH}&quot;, self.tika_lib_path)\n        cmd = cmd.replace(&quot;${ENCODING}&quot;, encoding)\n        cmd = cmd.replace(&quot;${FILE_PATH}&quot;, filePath)\n        return cmd\n\n    def _execute(self, cmd, encoding):\n        import subprocess\n        process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)\n        out, err = process.communicate()\n        out = out.decode(encoding=encoding)\n        err = err.decode(encoding=encoding)\n        return out, err\n</code></pre>\n"}, {'answer_id': 63790262, 'author': 'Vishal Sanap', 'author_id': 7752269, 'author_profile': 'https://Stackoverflow.com/users/7752269', 'pm_score': 0, 'selected': False, 'text': '<p>Just in case if someone wants to do in Java language there is Apache poi api. extractor.getText() will extract plane text from docx . Here is the link <a href="https://www.tutorialspoint.com/apache_poi_word/apache_poi_word_text_extraction.htm" rel="nofollow noreferrer">https://www.tutorialspoint.com/apache_poi_word/apache_poi_word_text_extraction.htm</a></p>\n'}, {'answer_id': 70816823, 'author': 'vhx.ai', 'author_id': 18004763, 'author_profile': 'https://Stackoverflow.com/users/18004763', 'pm_score': 0, 'selected': False, 'text': '<p><strong>Textract-Plus</strong></p>\n<p>Use textract-plus which can extract text from most of the document extensions including doc , docm , dotx and docx.\n(It uses antiword as a backend for doc files)\n<a href="https://textract-plus.readthedocs.io/en/latest/index.html" rel="nofollow noreferrer">refer docs</a></p>\n<p>Install-</p>\n<pre><code>pip install textract-plus\n</code></pre>\n<p>Sample-</p>\n<pre><code>import textractplus as tp\ntext=tp.process(\'path/to/yourfile.doc\')\n</code></pre>\n'}, {'answer_id': 71948159, 'author': 'CpILL', 'author_id': 196732, 'author_profile': 'https://Stackoverflow.com/users/196732', 'pm_score': 0, 'selected': False, 'text': '<p>There is also <a href="https://pandoc.org/" rel="nofollow noreferrer">pandoc</a> the swiss-army-knife of documents. It converts from every format to nearly every other format. From the demos page</p>\n<pre><code>pandoc -s input_file.docx -o output_file.txt\n</code></pre>\n'}, {'answer_id': 72573235, 'author': 'r-or', 'author_id': 12875177, 'author_profile': 'https://Stackoverflow.com/users/12875177', 'pm_score': 0, 'selected': False, 'text': '<p>Like <a href="https://stackoverflow.com/a/20663596/12875177">Etienne\'s answer</a>.\nWith python 3.9 <code>getiterator</code> was deprecated in ET, so you need to replace it with <code>iter</code>:</p>\n<pre><code>\ndef get_docx_text(path):\n    &quot;&quot;&quot;\n    Take the path of a docx file as argument, return the text in unicode.\n    &quot;&quot;&quot;\n    document = zipfile.ZipFile(path)\n    xml_content = document.read(\'word/document.xml\')\n    document.close()\n    tree = XML(xml_content)\n\n    paragraphs = []\n    for paragraph in tree.iter(PARA):\n        texts = [node.text\n                 for node in paragraph.iter(TEXT)\n                 if node.text]\n        if texts:\n            paragraphs.append(\'\'.join(texts))\n\n    return \'\\n\\n\'.join(paragraphs)\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/42482', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2678/']