I don’t understand this python program, why is the function "run" executed? In the codes segment and the whole program, I don’t see the run() been called. Here is the segments of code where the function is declared and the class that contains it which has been executed. The main window is class window2 and the function "run" is declared in it.
###cut off
class PDFWorker(QThread):
progress_signal = pyqtSignal(int)
completed_signal = pyqtSignal()
def __init__(self, files_list, output_path2, start_page, end_page, spinBox_2_value):
super().__init__()
self.files_list = files_list
self.output_path2 = output_path2
self.start_page = start_page
self.end_page = end_page
self.spinBox_2_value = spinBox_2_value
def run(self):
print("run4")
total_pages = 0
# First, calculate the total number of pages across all PDFs for progress tracking
for file in self.files_list:
doc = fitz.open(file)
total_pages += (len(doc) - self.spinBox_2_value) - self.start_page
doc.close()
pages_processed = 0
# Process each file individually
for i, file in enumerate(self.files_list):
doc = fitz.open(file)
num_pages_in_doc = len(doc) # Get total pages for this document
# Calculate the end_page for the current document, ensuring it doesn't exceed the total pages
end_page = min(num_pages_in_doc - self.spinBox_2_value, self.end_page)
text = ""
print(f"Processing {file} from page {self.start_page} to {end_page}")
for page_num in range(self.start_page, end_page):
page = doc.load_page(page_num)
pix = page.get_pixmap()
image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
text += "\n" + pytesseract.image_to_string(image)
pages_processed += 1
# Emit progress signal
progress = int((pages_processed / total_pages) * 100)
self.progress_signal.emit(progress)
doc.close()
# Ensure each output file has a unique name
output_file = Path(self.output_path2, f"{Path(file).stem}_{i+1}.docx")
self.create_docx(text, output_file)
print(f"Created {output_file}")
print(text)
# Emit completed signal
self.completed_signal.emit()
def create_docx(self, text, file_path):
doc = Document()
paragraphs = text.split('\n')
for paragraph in paragraphs:
doc.add_paragraph(paragraph)
doc.save(file_path)
class window2(QMainWindow):
def __init__(self):
super(window2, self).__init__()
loadUi(os.path.join(base_path, '2.ui'), self)
self.action.triggered.connect(self.page1)
self.pushButton_start.clicked.connect(self.start)
self.pushButton.clicked.connect(self.select_input_path)
self.pushButton_2.clicked.connect(self.select_output_folder)
def start(self):
print("run1")
global path2, output_path2
if path2 == "":
QMessageBox.critical(self, '', 'E:0014')
return
if output_path2 == "":
QMessageBox.critical(self, '', 'E:0015')
return
start_page = self.spinBox.value()
spinBox_2_value = self.spinBox_2.value()
if self.radioButton_single.isChecked():
files_list = [path2] # Single file
elif self.radioButton_all.isChecked():
files_list = find_files(path2, ".pdf") # Multiple files
# Open the first file to calculate the total number of pages
doc = fitz.open(files_list[0])
end_page = len(doc) # Set end_page to the total number of pages in the PDF
doc.close()
# Create a PDF worker thread and start processing
print("run2")
self.pdf_worker = PDFWorker(files_list, output_path2, start_page, end_page, spinBox_2_value)
self.pdf_worker.progress_signal.connect(self.update_progress_bar)
self.pdf_worker.completed_signal.connect(self.pdf_processing_completed)
self.pdf_worker.start()
###cut off
>Solution :
Your class inherits from QThread, and the QThread documentation has this to say about the run() method:
run()The starting point for the thread. After calling start(), the newly created thread calls this function. The default implementation simply calls exec().
You can reimplement this function to facilitate advanced thread management. Returning from this method will end the execution of the thread.
So whatever is written into the run() will be what happens when the thread runs. You start the thread running by calling start().