How to extract data from MS Word Documents using Python | by Natalie Olivo | Towards Data Science

This blog will go into detail on extracting information from Word Documents locally. Since many companies and roles are inseparable from the Microsoft Office Suite, this is a useful blog for anyone faced with data transferred through .doc or .docx formats.
As a prerequisite, you will need Python installed on your computer. For those of you doing this at work, you likely do not have admin rights. This blog explains how to install Anaconda on a Windows machine without admin rights.
You can find the Notebook supporting this blog here.
We’ll be taking advantage of each word document’s XML make-up. From there, we will use the regex library to find each URL in the document text, then adding the URLs to a list, which will be perfect for performing for-loops.
#specific to extracting information from word documentsimport osimport zipfile#other tools useful in extracting the information from our documentimport re#to pretty print our xml:import xml.dom.minidom
os will allow you to navigate and find relevant files on your operating system
zipfile will allow you to extract the xml from the file
xml.dom.minidom to parse the xml code
First we’ll need to direct our code to open files in the file location they are stored. To see this from our notebooks (rather than opening up a file explorer), we can use os. While knowing the file path to a single file of interest eliminates the need to use os in this simple example, this library can later be used to create list of documents stored in a target folder. Having a list of documents stored in a folder is helpful should you to want to write a for-loop to extract information from all word documents stored in a folder.
To see a list of files in your current directory, use a single period in the os filepath:
os.listdir('.')
To see a list of files in the directory above your current location, use a double period:
os.listdir('..')
Once you’ve found where your word documents are stored, you’ll be able to convert the file found with the file path to a zipfile.ZipFile file type, which, for our purposes can be read.
The ZIP file format is a common archive and compression standard
https://docs.python.org/3/library/zipfile.html
document = zipfile.ZipFile('../docs/TESU CBE 29 Employee Job Description Evaluation - Final Approved.docx')#document will be the filetype zipfile.ZipFile
Now the .read() object to the zipfile class requires a name argument, which is different from the file name or the file path.
ZipFile.read(name, pwd=None)
To see examples of available names we can use the .name() object
document.namelist()
In the Jupyter Notebook to this blog, I explore a few of these names to show what they are. The name with the Word Document body text in it is ‘word/document.xml’
I found the pretty print technique in StackOverflow user Nate Bolton’s response to the question: Pretty Printing XML in Python.
We’ll only be using pretty print to help us identify patterns in the XML to extract our data. I personally do not know XML very well, so I’ll be relying on syntax patterns to find each URL in the text of our word document. If you already know your syntax pattern to extract your data, you may not need to pretty print it at all.
In our example, we find that the characters >http and < surround each hyperlink contained in the document text.
I’ll need to accomplish our goal of collecting all text between the aforementioned characters. To help with how to do this using regex, I used the following StackOverflow question, which contains what I am looking for in the initial ask: Regular Expression to find a string included between two characters while EXCLUDING the delimiters.
While I do want to keep the http, I do not want to keep the < or >. I will make these modifications to my list items using string slicing and list comprehension.
link_list = re.findall('http.*?\<',xml_str)[1:]link_list = [x[:-1] for x in link_list]
To see the full Jupyter Notebook behind this blog, click here!
If you’re interested in creating and writing MS Word documents using python, check out the library python-docx.
There are other methods of extracting text and information from word documents, such as the docx2txt and the docx libraries featured in the answers to the following Python Forum post.
This is a sister-blog to my entry about Thomas Edison State University’s (TESU) open source materials accessibility initiative. Medium post / Github repository
The initial blog I composed for this project was specific and borders on the line of unwieldy, so this blog is the first in a series I will be writing to take deep dives into separate aspects of the TESU project and make the material more accessible.