暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

tran.py 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import time
  2. from googletrans import Translator
  3. from PyQt5.QtCore import QThread, pyqtSignal
  4. from PyQt5.QtWidgets import (QApplication, QDialog,
  5. QProgressBar, QPushButton,QLabel)
  6. from PyQt5 import QtWidgets
  7. from PyQt5.QtCore import *
  8. import logging
  9. import ctypes, sys
  10. import chardet
  11. def is_admin():
  12. try:
  13. return ctypes.windll.shell32.IsUserAnAdmin()
  14. except:
  15. return False
  16. if is_admin():
  17. print(is_admin())
  18. pass
  19. else:
  20. if sys.version_info[0] == 3:
  21. ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
  22. logging.basicConfig(level=logging.ERROR, filename='py-tran-log.txt', filemode='w',
  23. format='[%(asctime)s %(levelname)-8s] %(message)s',
  24. datefmt='%Y%m%d %H:%M:%S',
  25. )
  26. try:
  27. translator = Translator(service_urls=['translate.googleapis.com'])
  28. try:
  29. with open("./C.txt","rb") as k:
  30. dd = chardet.detect(k.read())
  31. print(dd['encoding'])
  32. file3 = open('./C.txt','r',encoding=str(dd['encoding']))
  33. try:
  34. data3 = file3.readlines()[0]
  35. except:
  36. data3='是繁體中文'
  37. file3.close()
  38. except Exception as e:
  39. data3 = '是繁體中文'
  40. logging.error(e)
  41. print(data3)
  42. try:
  43. file1 = open('./A.txt','r',encoding=dd['encoding'])
  44. try:
  45. data1 = file1.read()
  46. except Exception as e:
  47. logging.error(e)
  48. data1 = 'None'
  49. file1.close()
  50. except Exception as e:
  51. logging.error(e)
  52. data1 = 'None'
  53. print(data1)
  54. if data3=='是繁體中文' and data1 != 'None':
  55. result = translator.translate(data1,dest='id').text
  56. result2 = translator.translate(data1,dest='en').text
  57. file2 = open('./ch.txt','w+',encoding= 'utf-8')
  58. file2.write(data1)
  59. file2.close()
  60. file4 = open('./en.txt','w+',encoding= 'utf-8')
  61. file4.write(result2)
  62. file4.close()
  63. file5 = open('./in.txt','w+',encoding= 'utf-8')
  64. file5.write(result)
  65. file5.close()
  66. elif data3=='不是繁體中文' and data1 != 'None':
  67. result = translator.translate(data1,dest='zh-tw').text
  68. result2 = translator.translate(data1,dest='en').text
  69. file2 = open('./in.txt','w+',encoding= 'utf-8')
  70. file2.write(data1)
  71. file2.close()
  72. file4 = open('./ch.txt','w+',encoding= 'utf-8')
  73. file4.write(result)
  74. file4.close()
  75. file5 = open('./en.txt','w+',encoding= 'utf-8')
  76. file5.write(result2)
  77. file5.close()
  78. else:
  79. app2 = QtWidgets.QApplication([])
  80. error_dialog = QtWidgets.QErrorMessage()
  81. error_dialog.setWindowTitle('翻譯失敗')
  82. error_dialog.showMessage('翻譯失敗請重新嘗試!! / Terjemahan gagal, silakan coba lagi!!')
  83. app2.exec_()
  84. except Exception as e:
  85. app2 = QtWidgets.QApplication([])
  86. error_dialog = QtWidgets.QErrorMessage()
  87. error_dialog.setWindowTitle('ERROR')
  88. error_dialog.showMessage('翻譯失敗請重新嘗試!! / Terjemahan gagal, silakan coba lagi!!')
  89. logging.error(e)
  90. app2.exec_()