简体   繁体   中英

Searching docx Files in python

I am trying to use the python-docx module to search docx files for specific strings:

https://github.com/python-openxml/python-docx

But for some reason none of the functions I try to use within the module seem to be working (opendocx, search, etc). I have installed the module and have imported it in my script so I can't figure out what's going wrong. For example though when I try to use opendocx() I get an error saying the module has no attribute 'opendocx'

Other people seem to be able to use this module fine, am I missing something obvious?

EDIT:

Here is the code where I am trying to use the doc:

def parseFile2(filename):
    document = opendocx(filename)
    for key in SEARCH_STRINGS:
        if search(document, key):
            return True

The filename is passed in from another function with a full path and again the error I am getting is that the module has no attribute opendocx

You may be doing import modulename rather than from modulename import class . This can often lead to the behavior you're seeing. For example:

>>> import math
>>> sqrt(64)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'sqrt' is not defined
>>> from math import sqrt
>>> sqrt(64)
8.0

After a quick look at the documentation of the module you're not using the Document class of the module.

from docx import Document

def parseFile2(filename):
    document = Document(filename)
    for key in SEARCH_STRINGS:
        if search(document, key): # dont know if this part works, cause i didn't install the module
            return True

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM