Accessing Microsoft Word XML Data in Python
- Michael

- Dec 7, 2021
- 3 min read
Updated: Dec 13, 2021

In my various editorial roles with the Academy of Art University, I wrote and edited tutorials for myriad software applications. I wrote copy using Microsoft Word and other text editors. I've used Adobe Dreamweaver and various content management systems.
However, today's work is increasingly done with XML or JSON data directly, not with Word files. Starting with this blog post, I'll be sharing some of what I learn as I dig deeper into this subject.
I've taken a few coding classes that provided me with the basics of working with XML and JSON data in an integrated development environment. So when I think of working with those files, Visual Studio Code is the first application that comes to mind. A Microsoft Word DOCX file is inherently composed of XML files. My first impulse is to try to interface directly with those files. That might give me the most control in working with the content.
After reviewing the class "XML Essential Learning" on LinkedIn, I decided that the content of the class I'd been watching could serve as useful data upon which to experiment. I copied the content of the video lectures out of the transcript field in LinkedIn and quickly formatted them in Google Docs. That XML lecture will be the "lorem ipsum" text for this exercise.
First, I'll click on my lecture document to rename it. I replace the ".docx" with ".zip." Now I can easily access the underlying XML files. I open the archive and extract its files.
Among the XML files in the DOCX archive I've exported is a file called "document.xml". This file contains the core content of the Word document. To better understand how to work with this content, I reviewed this article by author Stepan Yakovenko that explains how to access specific information encoded in a DOCX-based XML file. He provides this test example:
<w:document>
<w:body>
<w:p w:rsidR="005F670F" w:rsidRDefault="005F79F5">
<w:r><w:t>Test</w:t></w:r>
</w:p>
<w:sectPr w:rsidR="005F670F">
<w:pgSz w:w="12240" w:h="15840"/>
<w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="720" w:footer="720"
w:gutter="0"/>
<w:cols w:space="720"/>
<w:docGrid w:linePitch="360"/>
</w:sectPr>
</w:body>
</w:document>All of these lines of code in Yakovenko's example correspond to one word of text in his test document: "Test". The text "Test" is found in the <w.body> node of the <w.document> node. The node <w.body> is a child of <w.document>, and it itself has four child nodes. The node <w:p> includes the text in the test document's first and only p node.
We can see a similar structure in the document.xml file I'm working with. This code below corresponds to another p tag, the first paragraph of text in the exported Google document:
<w:p w:rsidR="00000000" w:rsidDel="00000000" w:rsidP="00000000" w:rsidRDefault="00000000" w:rsidRPr="00000000" w14:paraId="00000001">
<w:pPr>
<w:pStyle w:val="Heading1"/>
<w:rPr/>
</w:pPr>
<w:bookmarkStart w:colFirst="0" w:colLast="0" w:name="_tpjqdwwdkv3m" w:id="0"/>
<w:bookmarkEnd w:id="0"/>
<w:r w:rsidDel="00000000" w:rsidR="00000000" w:rsidRPr="00000000">
<w:rPr>
<w:rtl w:val="0"/>
</w:rPr>
<w:t xml:space="preserve">XML Essential Training</w:t>
</w:r>
</w:p>Let's simplify that a bit to better see the structure of this code:
<w:p w14:paraId="00000001">
<w:pPr>
<w:pStyle w:val="Heading1"/>
<w:rPr/>
</w:pPr>
<w:r>
<w:rPr>
<w:t>XML Essential Training</w:t>
</w:r>
</w:p>I can see see the parent/child relationships in this XML file. Now I'll move over to Python. I've opened a Jupyter Notebook originating from the XML file's folder. Here's the code I used to open the XML file in Python:
import xml.etree.ElementTree as et
xtree = et.parse('document.xml")
xroot = xtree.getroot()The XML is now contained in the 'xroot' object. If we enter "print(xroot[0].tag)", we find the body tag for this XML file, which is "{http://schemas.openxmlformats.org/wordprocessingml/2006/main}body".
Digging a little deeper, we find that "print(xroot[0][0][1][0].tag)" returns the rPr tag I'm looking for. The tag is actually titled "{http://schemas.openxmlformats.org/wordprocessingml/2006/main}rPr", or "rPr" for short.
This node should include the text I'm looking for, "XML Essential Training." Now that I've found it, this is where I will stop this first lesson. In future installments, I'll show in more detail how to use Python to manipulate this XML file. Thanks for reading!


Comments