#!python # -*- mode: python; Encoding: utf-8; coding: utf-8 -*- # Last updated: <2022/04/24 09:27:50 +0900> u""" Appli window activate. copy and paste. with tkinter. 1. Activate the Adobe Acrobat Reader DC window and send Ctrl+C. 2. Replace CRLF in clipboard with space. 3. Activate the DeepL Desktop window and send Ctrl+A, Delete, Ctrl+V * Windows10 x64 21H2 + Python 3.9.12 64bit + PyAutoGUI 0.9.53 + PyGetWindow 0.0.9 """ import tkinter as tk import pyautogui as ag import pygetwindow as gw import pyperclip import time pdfviewer = "Reader DC" deepl = "DeepL " def activate_window(w): """Activate appli window.""" try: w.activate() except Exception: w.minimize() w.restore() time.sleep(0.25) def get_window(name): hwnds = gw.getWindowsWithTitle(name) w = None if hwnds != []: w = hwnds[0] else: print("Error: Not found %s" % name) return w def copy_and_paste(): w = get_window(pdfviewer) if w is None: return False activate_window(w) ag.hotkey("ctrl", "c") time.sleep(0.3) # replace CRLF to space txt = pyperclip.paste() newtxt = " ".join(txt.splitlines()) # newtxt = txt.replace("\n", " ") pyperclip.copy(newtxt) # print(newtxt) w = get_window(deepl) if w is None: return False activate_window(w) ag.hotkey("ctrl", "a") time.sleep(0.2) ag.hotkey("delete") time.sleep(0.2) ag.hotkey("ctrl", "v") time.sleep(0.2) return True class App(tk.Frame): def __init__(self, master=None): tk.Frame.__init__(self, master) self.pack() btn1 = tk.Button(self, text="Translation", command=lambda: self.trans()) btn1.pack(side=tk.LEFT, ipadx=60, ipady=8, padx=6, pady=6) btn1 = tk.Button(self, text="Exit", command=self.master.destroy) btn1.pack(side=tk.LEFT, padx=6, pady=6) def trans(self): if not copy_and_paste(): self.quit def main(): root = tk.Tk() root.title("PDF trans") app = App(master=root) app.mainloop() # root.destroy() if __name__ == "__main__": main()