How can I search a word in a Word 2007 .docx file?

Gerry picture Gerry · Sep 22, 2008 · Viewed 44.1k times · Source

I'd like to search a Word 2007 file (.docx) for a text string, e.g., "some special phrase" that could/would be found from a search within Word.

Is there a way from Python to see the text? I have no interest in formatting - I just want to classify documents as having or not having "some special phrase".

Answer

mikemaccana picture mikemaccana · Dec 30, 2009

After reading your post above, I made a 100% native Python docx module to solve this specific problem.

# Import the module
from docx import *

# Open the .docx file
document = opendocx('A document.docx')

# Search returns true if found    
search(document,'your search string')

The docx module is at https://python-docx.readthedocs.org/en/latest/