我已经使用PyQt5 for GUI创建了finder.py
脚本。
我已经在Windows上使用PyInstaller将脚本编译为.exe,并在virtualenv中运行以下命令:
pyinstaller finder.py
但是,当我尝试打开finder.exe时(无论是从virtualenv内部还是从外部),我都会得到
ModuleNotFoundError:没有名为“ PyQt5”的模块
根据其他SO答案,我尝试在创建可执行文件时将-c
,-F
,--windowed
,--onefile
的组合作为参数传递,但是没有用。
我该如何解决?
完整的finder.py
代码:
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QWidget, QLabel, QLineEdit, QPushButton, QComboBox
from PyQt5.QtCore import QSize
import openpyxl as xl
import os
import xlrd
class Finder():
def __init__(self, directory, keywords, fileformats):
self.directory = directory
self.keywords = keywords
self.fileformats = fileformats
def filenames(self):
for root, dirs, filenames in os.walk(self.directory):
for filename in filenames:
if filename.endswith(self.fileformats):
yield os.path.join(root, filename)
def find_in_pdf(self):
for filename in self.filenames():
print(filename)
if filename.endswith(str(self.fileformats)):
try:
pdfFileObj = open(filename, 'rb')
print('opened')
except:
print('fail')
pass
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
pageObj = pdfReader.getPage(0)
pdf_text = pageObj.extractText()
for keyword in self.keywords:
if keyword in pdf_text:
print('FOUND IN ' + filename)
# else:
# print('NOT FOUND')
print('THE END')
def find_in_excel(self):
for filename in self.filenames():
if filename.endswith(self.fileformats):
try:
wb = xlrd.open_workbook(filename)
except:
pass
print('Opened ' + filename)
for sheet in wb.sheets():
print('Searching in sheet ' + sheet.name)
rows = sheet.nrows
columns = sheet.ncols
for row in range(0, rows):
for column in range(0, columns):
#print('Cell value: ' + str(sheet.cell(row,column).value))
for keyword in self.keywords:
if keyword in str(sheet.cell(row,column).value):
#print('**************************************')
print(keyword, filename, sheet.name, row, column)
#print('**************************************')
print('THE END')
class FinderGUI(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setMinimumSize(QSize(1000, 200))
self.setWindowTitle("Finder")
self.dir_label = QLabel(self)
self.dir_label.setText('Lookup directory: ')
self.dir_input = QLineEdit(self)
self.keywords_label = QLabel(self)
self.keywords_label.setText('Keywords: ')
self.keywords_input = QLineEdit(self)
self.dropdown = QComboBox(self)
self.dropdown_label = QLabel(self)
self.dropdown_label.setText('File formats: ')
self.dropdown.addItems(['.xlsx, .xlsm, .xls', '.pdf', ])
search_button = QPushButton('Search', self)
self.dir_input.move(140, 20)
self.dir_label.move(20, 20)
self.keywords_input.move(140, 60)
self.keywords_label.move(20, 60)
self.dropdown.move(140, 100)
self.dropdown_label.move(20, 100)
search_button.move(20,155)
self.dir_input.resize(850, 32)
self.keywords_input.resize(850, 32)
search_button.resize(200,32)
self.dropdown.resize(200, 32)
search_button.clicked.connect(self.find)
def find(self):
lookup_dir = self.dir_input.text()
keywords = self.keywords_input.text()
fileformats = self.dropdown.currentText()
input_data = {'lookup_dir':lookup_dir, 'keywords':keywords, 'file_formats':fileformats}
finder = Finder(lookup_dir.replace('\\', '/'), keywords.split(', '), tuple(fileformats.split(', ')))
if '.pdf' in finder.fileformats:
finder.find_in_pdf()
else:
finder.find_in_excel()
#print(input_data)
return input_data
#directory.replace('\\', '/')
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = FinderGUI()
mainWin.show()
sys.exit(app.exec_())