How do I extract data from a doc/docx file using Python

35,453

Solution 1

It seems that pywin32 does the trick. You can iterate through all the tables in a document and through all the cells inside a table. It's a bit tricky to get the data (the last 2 characters from every entry have to be omitted), but otherwise, it's a ten minute code. If anyone needs additional details, please say so in the comments.

Solution 2

The docx is a zip file containing an XML of the document. You can open the zip, read the document and parse data using ElementTree.

The advantage of this technique is that you don't need any extra python libraries installed.

import zipfile
import xml.etree.ElementTree

WORD_NAMESPACE = '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}'
PARA = WORD_NAMESPACE + 'p'
TEXT = WORD_NAMESPACE + 't'
TABLE = WORD_NAMESPACE + 'tbl'
ROW = WORD_NAMESPACE + 'tr'
CELL = WORD_NAMESPACE + 'tc'

with zipfile.ZipFile('<path to docx file>') as docx:
    tree = xml.etree.ElementTree.XML(docx.read('word/document.xml'))

for table in tree.iter(TABLE):
    for row in table.iter(ROW):
        for cell in row.iter(CELL):
            print ''.join(node.text for node in cell.iter(TEXT))

See my stackoverflow answer to How to read contents of an Table in MS-Word file Using Python? for more details and references.

In answer to a comment below, Images are not as clear cut to extract. I have created an empty docx and inserted one image into it. I then open the docx file as a zip archive (using 7zip) and looked at the document.xml. All the image information is stored as attributes in the XML not the CDATA like the text is. So you need to find the tag you are interested in and pull out the information that you are looking for.

For example adding to the script above:

IMAGE = '{http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing}' + 'docPr'

for image in tree.iter(IMAGE):
    print image.attrib

outputs:

{'id': '1', 'name': 'Picture 1'}

I'm no expert at the openxml format but I hope this helps.

I do note that the zip file contains a directory called media which contains a file called image1.jpeg that contains a renamed copy of my embedded image. You can look around in the docx zipfile to investigate what is available.

Solution 3

To search in a document with python-docx

# 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')

You also have a function to get the text of a document:

https://github.com/mikemaccana/python-docx/blob/master/docx.py#L910

# Import the module
from docx import *

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

Using https://github.com/mikemaccana/python-docx

Share:
35,453
Stefan Urziceanu
Author by

Stefan Urziceanu

Updated on January 06, 2021

Comments

  • Stefan Urziceanu
    Stefan Urziceanu over 3 years

    I know there are similar questions out there, but I couldn't find something that would answer my prayers. What I need is a way to access certain data from MS-Word files and save it in an XML file. Reading up on python-docx did not help, as it only seems to allow one to write into word documents, rather than read. To present my task exactly (or how i chose to approach my task): I would like to search for a key word or phrase in the document (the document contains tables) and extract text data from the table where the key word/phrase is found. Anybody have any ideas?

  • dasen
    dasen over 9 years
    Could you post a quick example of code to do just that? (i.e., select table and read the contents to a variable, for instance) Thanks
  • Soferio
    Soferio about 8 years
    If you are going to grab cells in a table using docx, just make sure you read this in order to avoid performance issues: github.com/python-openxml/python-docx/issues/174
  • dashesy
    dashesy almost 8 years
    @dasen Here
  • Jaynam Modi
    Jaynam Modi over 3 years
    could you point out the NAMESPACE tag for an image? i tried but there's multiple fits, and by chance also those for lists which i presumed to be ilvl.
  • Mike Robins
    Mike Robins over 3 years
    I've added some information to the answer above, I hope that helps.
  • Jaynam Modi
    Jaynam Modi over 3 years
    thank you very much, I reached the same conclusion the same night while researching