본문 바로가기

카테고리 없음

구글 클라우드를 이용하여 번역 프로그램 만들기 (python)

 

 

구글 클라우드를 이용하여 번역 프로그램을 만들었습니다. 

사용하는 방법은 알트 + / 키를 누르면

전체선택 후 잘라낸 다음에 영어로 번역해서 붙여넣기를 하게 됩니다. 

 

참고한 자료는 

아래 유튜브를 참고해서 작성했습니다

https://youtu.be/0t82RAJeMiI

 

구글 공식 참고 사이트

https://cloud.google.com/translate/docs/setup#project

 

 

# 콘솔 출력안되게
# https://wikidocs.net/21952#_2
# pyinstaller --onefile  --windowed  D:\python_work\nado_Coding\7_gpt_python_project\06_google_tran.py
# pip install pyinstaller
# https://aplab.tistory.com/entry/%ED%8C%8C%EC%9D%B4%EC%8D%AC-%EC%8B%A4%ED%96%89%ED%8C%8C%EC%9D%BC-%EB%A7%8C%EB%93%9C%EB%8A%94-2%EA%B0%80%EC%A7%80-%EB%B0%A9%EB%B2%95%EC%9D%80
# https://greendreamtrre.tistory.com/263


# 참고사이트
# https://coding-kindergarten.tistory.com/98

# 위키독스
# https://wikidocs.net/137922
# https://wikidocs.net/151324

# 참고페이지 https://cloud.google.com/translate/docs/setup#project
# 참고유튜브 https://youtu.be/0t82RAJeMiI

from google.cloud import translate_v2 as translate

import keyboard
import pyperclip
import pyautogui
import tkinter as tk


import os

global state_string

# 실행중이어야 함
state_string = "실행"

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = r"E:\key_auth\google\********.json"


def set_keyboad_ctrl(key_a):
    pyautogui.sleep(0.2)
    pyautogui.keyDown('ctrl')
    pyautogui.press(key_a)
    pyautogui.sleep(0.2)
    pyautogui.keyUp('ctrl')


def tranKrEn():
    global state_string

    if state_string == "중지":
        return

    pyautogui.keyUp('ctrl')

    # 전체선택하기
    set_keyboad_ctrl('a')

    # 잘라내기
    set_keyboad_ctrl('x')

    # Get text from clipboard
    stringHangel = pyperclip.paste()
    print(stringHangel)

    # Translate text to English
    client = translate.Client()
    result = client.translate(stringHangel, target_language='en')

    # Put translated text in clipboard
    pyperclip.copy(result['translatedText'])
    print(result['translatedText'])

    # 붙여넣기
    set_keyboad_ctrl('v')

    pyautogui.sleep(0.2)
    pyautogui.press('enter')




def toggle():
    global state_string

    # 지금 중지중인경우
    if state_string == "실행":
        state_string = "중지"
        label.config(text="번역중지")
        pyautogui.alert('번역중지')

    # 지금실행중인 경우
    else:
        state_string = "실행"
        label.config(text="번역실행")
        pyautogui.alert('번역실행')


root = tk.Tk()
root.title("Toggle Example")

label = tk.Label(root, text="번역실행")
label.pack()

toggle_button = tk.Button(root, text="Toggle", command=toggle)
toggle_button.pack()

print('program start')
# Register a hotkey to trigger the function
keyboard.add_hotkey('alt+/', tranKrEn)
keyboard.add_hotkey('alt+.', toggle)

# Add a title
root.title("구글번역")

# Win Size Control
root.geometry('200x80')

root.mainloop()

# Wait for hotkeys to be triggered
# keyboard.wait("esc")  # 사용자가 esc를 누를때까지 프로그램 수행
print('all program end')

 

배치파일 만들기

@echo off
d:
cd D:\python_work\nado_Coding\7_gpt_python_project\
python 06_google_tran.py
pause