adding pyq.py

This commit is contained in:
Mateusz Kuc 2021-01-30 14:46:48 +01:00
parent 45e4a461f7
commit 33eee52049
1 changed files with 97 additions and 0 deletions

97
pyq.py Normal file
View File

@ -0,0 +1,97 @@
from tkinter import *
import tkinter as tk
from tkinter import ttk
from tkinter.filedialog import askopenfile, asksaveasfile
from PIL import Image, ImageTk
import pytesseract as tes
#from fpdf import FPDF
import os
import pdfkit
#PDF
path_wkhtmltopdf = r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe'
config = pdfkit.configuration(wkhtmltopdf=path_wkhtmltopdf)
#TESSERACT
tes.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
#TKINTER
root = Tk()
#TKINTER SETTINGS
content = ttk.Frame(root, padding=(3,3,12,12))
frame = ttk.Frame(content, borderwidth=5, relief="ridge", width=200, height=200)
clear = ttk.Button(content, text="Clear textbox",command=lambda:clearTextInput())
ok = ttk.Button(content, text="Okay")
save = ttk.Button(content, text="Save",command=lambda:save_file())
convert = ttk.Button(content,text="Convert to PDF",command=lambda:convert_to_pdf())
#btn for image
browse_text = StringVar()
browse_btn = ttk.Button(content,textvariable=browse_text,command=lambda:open_file())
browse_text.set("Browse")
text_box = Text(content,width=50,height=12)
def clearTextInput():
text_box.delete("1.0","end")
def open_file():
browse_text.set("loading...")
file = askopenfile(parent=root,mode='rb',title="Choose a file",filetype=[("Image file","*.png *.jpg *.jpeg")])
if file:
# create a canvas to show image on
canvas_for_image = Canvas(frame, bg='green', height=200, width=200, borderwidth=0, highlightthickness=0)
canvas_for_image.grid(row=0, column=0, sticky='nesw', padx=0, pady=0)
# create image from image location resize it to 200X200 and put in on canvas
image = Image.open(file)
canvas_for_image.image = ImageTk.PhotoImage(image.resize((200, 200), Image.ANTIALIAS))
canvas_for_image.create_image(0, 0, image=canvas_for_image.image, anchor='nw')
#text box
img_content = tes.image_to_string(image)
text_box.insert(1.0, img_content)
browse_text.set("Browse")
print(img_content)
def save_file():
filename = asksaveasfile(mode='w',defaultextension="*.txt")
if filename is None:
return
text2save = str(text_box.get(1.0,END))
filename.write(text2save)
filename.close()
def convert_to_pdf():
f = askopenfile(mode='r',title="Choose a file",filetype=[("Text file (*.txt)","*.txt")])
pdfkit.from_file(f,'out.pdf')
os.startfile('out.pdf')
# CONTENT REPOSITION
content.grid(column=0, row=0, sticky=(N, S, E, W))
frame.grid(column=0, row=0, columnspan=3, rowspan=2, sticky=(N, S, E, W))
browse_btn.grid(column=1, row=3)
clear.grid(column=2,row=3)
save.grid(column=3, row=3)
convert.grid(column=4,row=3)
text_box.grid(column=3, row=0, columnspan=2, sticky=(N, W), padx=5)
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)
content.columnconfigure(0, weight=3)
content.columnconfigure(1, weight=3)
content.columnconfigure(2, weight=3)
content.columnconfigure(3, weight=1)
content.columnconfigure(4, weight=1)
content.rowconfigure(1, weight=1)
root.mainloop()