I Created an GUI with python and other few required library my task is to convert an non-searchable pdf - searchable pdf and save it as a new pdf and extract a word form it and save the file with that name . I integrated this code with the GUL which has start, cancel, close buttons, the start but is working properly and after completion I am able to exit the code correctly too but when I tried to cancel the running. Its show running exited but its not stopped. its stopped execution after when the ocrmypdf.pdf() runed completely and gonna save it as pdf then its stopping what want is during its processing time only it's needed to be stopped. here is my code
import tkinter as tk
from tkinter import filedialog
import customtkinter
from tkinter import ttk
import threading
import sys
import fitz
import PyPDF2
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
import io
import os
import ocrmypdf
import re
canceled = False
customtkinter.set_appearance_mode("System")
app = customtkinter.CTk() # create window
app.geometry("400x240")
def backend(input,output):
def create_footer_page(footer, page_orientation): # Adding Footer to the page
packet = io.BytesIO()
# Create a canvas for the header PDF
can = canvas.Canvas(packet, pagesize=letter)
can.setFont("Times-Roman", 10)
if page_orientation == 'l': # Adding the footer when the page rotation is 90
can.setFillColorRGB(0, 0, 1)
can.drawString(750, 565, footer)
else: # Adding the footer when the page rotation is 0
can.setFillColorRGB(0, 0, 1)
can.drawString(500, 800, footer)
can.save()
packet.seek(0)
header_pdf = PyPDF2.PdfFileReader(packet)
return header_pdf.pages[0] # return's the page to the add_header_to_landscape after adding footer
def create_header_page(text, page_orientation):
packet = io.BytesIO()
# Create a canvas for the header PDF
can = canvas.Canvas(packet, pagesize=letter)
can.setFont("Times-Roman", 10)
if page_orientation == 'l': # Adding the Header when the page in Landscape mode
can.setFillColorRGB(0, 0, 1)
can.drawString(300, 565, text)
else: # Adding the Header when the page rotation is 0
can.setFillColorRGB(0, 0, 1)
can.drawString(150, 800, text)
can.save()
packet.seek(0)
header_pdf = PyPDF2.PdfFileReader(packet)
return header_pdf.pages[0] # return's the page to the add_header_to_landscape after adding header
def add_header_to_landscape(input_pdf, output_pdf, header):
pdf_reader = PyPDF2.PdfFileReader(input_pdf)
pdf_writer = PyPDF2.PdfFileWriter()
num_pages = len(pdf_reader.pages)
for page_num in range(num_pages):
page = pdf_reader.pages[page_num]
width = page.mediaBox.getWidth()
height = page.mediaBox.getHeight()
if height < width:
page_orientation = 'l'
else:
page_orientation = 'p'
footer = f'page {page_num + 1} of {num_pages}'
footer_pdf = create_footer_page(footer, page_orientation) # Function for adding the footer in pages
header_pdf = create_header_page(header, page_orientation) # Function for adding the Header in pages
page.mergePage(header_pdf)
page.mergePage(footer_pdf)
pdf_writer.addPage(page)
with open(output_pdf, 'wb') as output_file:
pdf_writer.write(output_file)
def word(input_pdf):
dco = fitz.open(input_pdf)
page = dco[0]
text = page.get_text()
lines = text.split('\n')
search = r"LIB\d{5}|L1B\d{5}|LIBO\d{4}"
word = ''
header = ''
for i in range(0, 5):
for j in lines[i]:
if j == ' ' or j == ':' or j == '-':
if re.search(search, word) != None:
x = re.search(search, word).group()
header = x[4::]
header = 'LIB0' + header
word = ''
else:
word += j
return header
def rotate_pdf_pages(input_pdf, output_pdf):
ocrmypdf.ocr(input_pdf, output_pdf, skip_text=True, rotate_pages_threshold=1, rotate_pages=True)
def process():
#print("in func")
proxy_file_path = ''
if input:
for filename in os.listdir(input):
if filename.endswith(".pdf"):
input_file_path = os.path.join(input, filename)
proxy_file_path = os.path.join(output, "proxy.pdf")
rotate_pdf_pages(input_file_path, proxy_file_path)
header_part = word(proxy_file_path)
header_text = f"{header_part}"
output_file_path = os.path.join(output, header_text + '.pdf')
add_header_to_landscape(proxy_file_path, output_file_path, header_text)
print(f"Processed: {input_file_path} -> {output_file_path}")
os.remove(proxy_file_path) # Delete proxy file
process()
def button_callback():
print("button pressed")
def browse_folder():
input_folder_path = filedialog.askdirectory()
input_folder_entry.delete(0, tk.END) # Clear previous content if any
input_folder_entry.insert(tk.END, input_folder_path)
def browse_folder_1():
output_folder_path = filedialog.askdirectory()
output_folder_entry.delete(0, tk.END) # Clear previous content if any
output_folder_entry.insert(tk.END, output_folder_path)
def start_processing():
global canceled, progress_bar, progress_percent_label
canceled = False
progress_bar = ttk.Progressbar(main_frame, mode="determinate", maximum=100, length=300, orient="horizontal")
progress_bar.grid(row=16, column=2, columnspan=2, padx=5, pady=20, ipadx=5, ipady=5)
progress_bar.start()
send_data = threading.Thread(target=senddatatobackend)
send_data.start()
def cancel_processing():
global canceled
canceled = True
input_folder_entry.delete(0, tk.END)
output_folder_entry.delete(0, tk.END)
progress_bar.stop()
progress_bar.grid_forget()
def senddatatobackend():
#print("hi")
input_path = input_folder_entry.get()
output_path = output_folder_entry.get()
# start_processing()
backend(input_path, output_path)
progress_bar.stop()
progress_bar.grid_forget()
def close_application(root):
root.destroy()
exit()
def main():
global input_folder_entry, output_folder_entry, progress_bar, main_frame, root, progress_percent_label
root = tk.Tk()
root.title("PDF Page Orientation & Adding Header")
main_frame = tk.Frame(root, width=50, highlightbackground='blue', highlightthickness=3, borderwidth=2,
relief=tk.GROOVE, background='black')
main_frame.grid(row=0, column=0, padx=100, pady=50, ipadx=100, ipady=50)
input_label = tk.Label(main_frame, text="Select Input Folders:")
input_label.grid(row=4, column=1, padx=(20, 10), sticky="e", pady=(40, 0))
input_folder_entry = tk.Entry(main_frame, width=50)
input_folder_entry.grid(row=4, column=2, columnspan=2, padx=5, pady=(40, 0))
input_button = tk.Button(main_frame, text="Browse", command=browse_folder, pady=2)
input_button.grid(row=4, column=4, pady=(40, 0), padx=(0, 10), sticky="w")
output_label = tk.Label(main_frame, text="Select Output Folders:")
output_label.grid(row=10, column=1, padx=(20, 10), sticky="e", pady=(40, 0))
output_folder_entry = tk.Entry(main_frame, width=50)
output_folder_entry.grid(row=10, column=2, columnspan=2, padx=5, pady=(40, 0))
output_button = tk.Button(main_frame, text="Browse", command=browse_folder_1, pady=2)
output_button.grid(row=10, column=4, padx=(0, 10), pady=(40, 0), sticky="w")
start_button = customtkinter.CTkButton(main_frame, text="start", command=start_processing)
start_button.grid(row=13, column=2, padx=5, pady=20, ipadx=5, ipady=5)
cancel_button = customtkinter.CTkButton(main_frame, text="Cancel", command=cancel_processing)
cancel_button.grid(row=13, column=3, padx=5, pady=20, ipadx=5, ipady=5)
close_button = customtkinter.CTkButton(main_frame, text="close", command=lambda: close_application(root))
close_button.grid(row=14, column=2, columnspan=2, padx=5, pady=5, ipadx=5, ipady=5)
root.mainloop()
if __name__ == "__main__":
main()
As I said above. I tried with what I want is when I click the cancel button its need to check which function is running and needs to stop that execution of that function. and when we clicked close button its need to check if any function is running if its running it's need to ask still process do u want to close the application if the user said yes then its need to stop the processing and close the it . if their is no function running it's just need to close directly