# https://www.globalsino.com/ICs/page4853.html
# Copy text into clipboard and then it can be extracted to a text file and printed by a click

from tkinter import Tk, Label, RAISED
import pyperclip

def updateClipboard():
    clipped_contents = pyperclip.paste()
    Clipping_process(clipped_contents=clipped_contents)
    root.after(ms=100, func=updateClipboard)

def Clipping_process(clipped_contents):
    cliptextCleaned = cleanClipText(clipped_contents=clipped_contents)
    label["text"] = cliptextCleaned

def cleanClipText(clipped_contents):
    #Removing all characters > 65535 (that's the range for TCL (Tool command Language) in tkinter)
    clipped_contents = "".join([c for c in clipped_contents if ord(c) <= 65535])
    return clipped_contents

def Click_to_print_txt(labelElem):
    labelText = labelElem["text"]
    print(labelText)
    pyperclip.copy(labelText)
    with open ('C:\GlobalSino\images\clipboard_to_webpage.txt', '+a') as output:
      try:
        output.write(labelText)
        output.write("\n\n-----------------------\n")
        output.write("-----------------------\n")
      except:
        output.write(labelText)
        output.write("\n\n-----------------------\n")
        output.write("-----------------------\n")

if __name__ == '__main__':
    root = Tk()
    label = Label(root, text="", cursor="plus", relief=RAISED, pady=5,  wraplength=500)
    label.bind("<Button-1>", lambda event, labelElem=label: Click_to_print_txt(labelElem))
    label.pack()
    updateClipboard()
    root.mainloop()
