Search code examples
python-2.7unicodekey-valuepython-docx

Extract particular word from docx table using python native library docx


The main objective of my code is to extract a particular word from the docx table.

TextDoc.docx

Docx table

Extract.py

from docx import *
mydoc = opendocx('/home/user/TestDoc.docx')    
search(mydoc,'Key1')

I got result as True, but my objective is something like

get_value('Key1')

output should be

Value1

Help would be appreciated.


Solution

  • I don't believe you can do that directly with the legacy versions of python-docx. You'll need to be on the most recent version (~0.5.0), which has a different API.

    pip uninstall docx
    pip install python-docx
    

    Something like this should work:

    from docx import Document
    
    document = Document('..path..')
    table = document.tables[0]
    cell = table.row[0].cells[0]
    paragraph = cell.paragraphs[0]
    text = paragraph.text
    

    Documentation for python-docx is here: http://python-docx.readthedocs.org/