from tkinter import Tk, Text, ttk

# written by Eddie Vanda - no responsibility taken 
# but please report any problems to eddie.vanda@rmit.edu.au

tk = Tk()
tk.title("Vet Text")
tk.geometry('500x400')

text0 = Text(tk, height=3, width=60) # for instructions
text1 = Text(tk, height=8, width=60) # text input
text2 = Text(tk, height=8, width=60) # text output

text0.insert('1.0', 'Paste text into first text area')
text0.insert('2.0', '\nThen press the copy button')
text0.insert('3.0', '\n Text will be cleared of non ascii characters!')

# validate char
# return same or replaced char or 0 if not replacable 
def validateCharacter(c, ln, cn):
    i = ord(c)
    if i > 127:
      if i == 8220 or i == 8221:
        c = '"'
      elif i == 160:
        c = ' '
      else:
        # report unknown input char out of range 
        print (str(ln) + "." + str(cn) + ": " + c + " -> " + str(i))
        c = chr(0)
 
    return c


# callback() is called by pressing the button
def callback():
  #print("diagnostic: callback")
  text_content = text1.get('1.0','end')
  ln = 1 # line number (starts at 1)
  cn = 0 # column number (starts at 0)

  for c in text_content:
    if c == '\n': # track line and column numbers
        ln += 1
        cn = 0
    c = validateCharacter(c, ln, cn)
    if ord(c) != 0:
      text2.insert('end', c)
    cn += 1

exit_button = ttk.Button(tk, text='COPY', command=callback)

text0.pack()
text1.pack()
exit_button.pack()
text2.pack()

tk.mainloop()