在Python中搜索docx文件
我正在尝试使用python-docx这个模块来搜索docx文件中的特定字符串:
https://github.com/python-openxml/python-docx
但是不知道为什么,我尝试使用的模块中的函数(比如opendocx、search等)似乎都不管用。我已经安装了这个模块,并在我的脚本中导入了它,所以我搞不清楚问题出在哪里。例如,当我尝试使用opendocx()时,出现了一个错误,提示这个模块没有'opendocx'这个属性。
其他人似乎都能正常使用这个模块,我是不是漏掉了什么明显的东西?
编辑:
这是我尝试使用文档的代码:
def parseFile2(filename):
document = opendocx(filename)
for key in SEARCH_STRINGS:
if search(document, key):
return True
文件名是从另一个函数传入的,带有完整路径,而我遇到的错误再次是这个模块没有'opendocx'这个属性。
2 个回答
0
快速看了一下你所用模块的文档,发现你并没有使用这个模块的Document类。
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
0
你可能在使用 import modulename
这种方式,而不是 from modulename import class
。这样做常常会导致你看到的情况。例如:
>>> 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