title: Using olefile to extract text from Word .doc
body: <p>I am only concerned with getting the text from .doc files. I am using python 3.6 on windows 10, so textract/antiword are off the table. I looked at other references from <a href="https://stackoverflow.com/questions/125222/extracting-text-from-ms-word-files-in-python">this question</a> but they are all old and incompatible with windows 10 and/or python 3.6.</p>
  
  <p>My document is a .doc file with a mix of Chinese and English. I am not familiar with how Word stores its files, and I don't have Word on my machine. Using olefile I was able to get the bytes of the document, but I do not know how to traverse the headers and layout correctly to extract the text. If I naively try </p>
  
  <pre><code>from olefile import OleFileIO as ofio
  ole = ofio('d.doc')
  stream = ole.openstream('WordDocument')
  data = stream.read()
  data.decode('utf-16')
  &gt;&gt;&gt;UnicodeDecodeError: 'utf-16-le' codec can't decode bytes in position 9884-9885: illegal encoding
  data[9884:9885]
  &gt;&gt;&gt;b'\xfa'
  data[:9884].decode('utf-16')
  </code></pre>
  
  <p>Then the last line gives me about half the doc, starting and ending with a lot of garbage characters. I suspect I could keep trying this method to get the text piece-by-piece, but I ultimately need to do this for a lot of files. Even if I did it this way, I can't think of a good way to automate it. How can I reliably get the text from a .doc using olefile? </p>
  
  <p>(Feel free to include alternatives to olefile in your answer as well, if you know of one that would work with my specs)</p>