#Translation


Translation Project

The program accepts a file (txt, rtf, docx, pdf) or text written in and returns a translated copy of it. (UI in video attached).


import sys import os import docx2txt import PyPDF2 from google.cloud import translate, speech from google.cloud.speech import enums from google.cloud.speech import types from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import QFileDialog from dialog import Ui_Dialog #Creates the class and initializes the main window and buttons. class AppWindow(Ui_Dialog): def __init__(self, dialog): Ui_Dialog.__init__(self) self.filename = "" self.txtname = "" self.langChosen = "" self.textg = "" self.c = "" self.setupUi(dialog) self.langSelected() self.bb.clicked.connect(self.openFile) self.re.clicked.connect(self.translation) self.pushButton.clicked.connect(self.copyText) #Code used to open a file def openFile(self): options = QFileDialog.Options() self.filename, _ = QFileDialog.getOpenFileName(options=options) self.textc() #Asks user to select a language from a dropdown menu and saves it def langSelected(self): os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="Translate-9130762d473c (2).json" # Instantiates a client translate_client = translate.Client() results = translate_client.get_languages() line = "" for i in range(0, len(results)): line += u'{name} ({language})'.format(**results[i]) + ", " self.comboBox.addItem(line) line = "" self.comboBox.activated[str].connect(self.onActivated) def onActivated(self, text): print("hello") self.langChosen = text #Translates and prints new file in language selected using the dropdown menu. def textc(self): typeOfFile = self.filename print(typeOfFile) if typeOfFile.endswith(".pdf"): pdfFileObj = open(typeOfFile, 'rb') print(pdfFileObj) pdf = PyPDF2.PdfFileReader(pdfFileObj) totalPages = pdf.getNumPages() print(totalPages) p_text = "" for page in range(totalPages): p = pdf.getPage(page) p_text += p.extractText() self.fileText.setText(p_text) self.textg = p_text elif typeOfFile.endswith(".txt"): txtFileObj = open(typeOfFile, 'rb') eachLine = "" for line in txtFileObj: print(line) eachLine += line self.fileText.setText(eachLine) self.textg = eachLine elif typeOfFile.endswith(".rtf"): rtfFileObj = open(typeOfFile, 'rb') eachLine1 = "" for line1 in rtfFileObj: print(line1) eachLine1 += line1 self.fileText.setText(eachLine1) self.textg = eachLine1 elif typeOfFile.endswith(".docx"): text = docx2txt.process(str(typeOfFile)) self.fileText.setText(text) self.textg = text else: print("Sorry, Not a supported file type at the moment.") def copyText(self): options2 = QFileDialog.Options() self.txtname, _ = QFileDialog.getOpenFileName(options=options2) self.copyFile() def copyFile(self): #myfile = self.txtname myText = self.textEdit.toPlainText() print(myText) with open(self.txtname, 'a') as f: f.write(myText) def translation(self): #Asks user to enter on of the target provided and translates the file (pdf so far) translate_client = translate.Client() target = self.langChosen typeOfFile = self.filename print(target) print(typeOfFile) start = target.rfind("(") end = target.rfind(")") choice = "" for i in range(start+1, end): choice += target[i] print(choice) if typeOfFile.endswith(".pdf"): newFile = translate_client.translate(self.textg, target_language=choice) self.textEdit.setText(u'Translation: {}'.format(newFile['translatedText'])) elif typeOfFile.endswith(".txt"): newFile2 = translate_client.translate(self.textg, target_language=choice) self.textEdit.setText(u'Translation: {}'.format(newFile2['translatedText'])) elif typeOfFile.endswith(".rtf"): newFile3 = translate_client.translate(self.textg, target_language=choice) self.textEdit.setText(u'Translation: {}'.format(newFile3['translatedText'])) elif typeOfFile.endswith("docx"): newFile1 = translate_client.translate(self.textg, target_language=choice) self.textEdit.setText(u'Translation: {}'.format(newFile1['translatedText'])) app = QtWidgets.QApplication(sys.argv) dialog = QtWidgets.QDialog() prog = AppWindow(dialog) dialog.show() sys.exit(app.exec_())
1