i have a python class that allow the user to select list of files then read the files and search for requested word.
the problem is i am able to select files and read but i am not able to search for word.
i will display the functions :
- preview the selected file
- read the file
- search for word
the code display a windows that includes
- edit text for extention
- button for open file dialog
- listWidget that includes the list of files
- edit text for enter searching word
- button for run the searching
- edit text that display the read file
code:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import (QApplication, QCheckBox, QColorDialog, QDialog,
QErrorMessage, QFileDialog, QFontDialog, QFrame, QGridLayout,
QInputDialog, QLabel, QLineEdit, QMessageBox, QPushButton)
from PyQt5.QtCore import QDir, Qt
from PyQt5.QtWidgets import *
import os,time,re
import pdfviewer
class pdfViewer(pdfviewer.Ui_PdfPreviewWindow):
def __init__(self,PdfPreviewObj):
#QWidget.__init__(self)
self.PdfPreviewObj =PdfPreviewObj
self.setupUi(PdfPreviewObj)
self.PdfPreviewObj.show()
self.pushButtonOpenFolder.clicked.connect(self.setExistingDirectory)
self.pushButtonSearch.clicked.connect(self.searchWord)
'''
search for entered string using regular expression in the lineEditSearch
==> highlight the requested word
==> display number of occurence of the searched word
'''
def searchWord(self,selectedFile):
fileToSearchInside = self.readFile(selectedFile)
searchedSTR = self.lineEditSearch.text()
# i think the error is here but i do not know how to fix it
while fileToSearchInside>0:
try:
if(searchedSTR in fileToSearchInside):
print("matched string")
except Exception as e:
print(e)
'''
read file based on the user click
'''
def readFile(self, currentFile):
currentFile = self.listWidgetPDFlist.currentItem().text()
print(currentFile)
try:
with open(currentFile) as ctf:
ctfRead = ctf.read()
print(ctfRead)
return(ctfRead)
except Exception as e:
print("the selected file is not readble because : {0}".format(e))
'''
get the name of the current item selected in the list (==>the path name )
set in the edit text the selected item (==> selected file)
'''
def previewSelectedFile(self):
Item=self.listWidgetPDFlist.currentItem().text()
self.textEdit_PDFpreview.setText(self.readFile(Item))