# coding=utf-8 import xlwt import os import re import xlrd from PyQt5.QtWidgets import * from PyQt5.QtCore import Qt, showbase import sys from PyQt5.QtGui import (QIcon,QFont,QColor, QIntValidator, QValidator) class createWindow(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): # Set the window's position and size self.setgeometry (300, 300, 500, 200) self.setwindowTitle ('Excel and multilingual ') self.setwindowIcon (QIcon(' excelicon.png ')) global isExcel2Strings isExcel2Strings Chose_files chose_Files = "# Global layout (vertical) wLayout = QVBoxLayout() # Local layout (horizontal) h1 = QHBoxLayout() h2 = QHBoxLayout() h3 = QHBoxLayout() h4 = QHBoxLayout() Resize (btn.sizeHint())#btn.sizeHint() displays the default size self.lab = QLabel('', Self) h1.addWidget(BTN) h1.addWidget(self.lab) h1.addstretch () self.btn1 = QRadioButton(' Excel to Strings ') self.btn1.setChecked(True) self.btn1.toggled.connect(lambda : Self.chosetype (self.btn1)) self.btn2 = QRadioButton('strings to excel') self.btn2.setchecked (False) self.btn2.toggled.connect(lambda : Self.chosetype (self.btn2)) h2. AddWidget (self.btn1) h2. AddWidget (self.btn2) h2. AddStretch () lab = QLabel(' self.btn2 ',self) Self. ChineseTF = QLineEdit (self) validate = QIntValidator () the self. ChineseTF. SetValidator (validate) can only input Numbers descLab = # limitation QLabel(' defaults to 1 ',self) desclab. setStyleSheet('color:# a2a2A2; ') descLab.setFont(QFont('SansSerif', AddWidget (lab) h3.addWidget(self.chinesETF) h3.addWidget(descLab) h3.addStretch() BTN = QPushButton(' start to convert ', Resize (btn.sizeHint())#btn.sizeHint() displays the default size h4.addWidget(BTN) h4.addStretch() wlayout.addLayout(h1) wlayout.addLayout(h2) wlayout.addLayout(h3) wlayout.addLayout(h4) Self.show () @classmethod def showAlert(self, MSG): Exec_ () def startConvert(self): Global chose_files if len(chose_files) == 0: self.showAlert(' please select item ') else: if isExcel2Strings: Global chinese_index val = self.chinesetf.text () if len(val) == 0: self.showalert (' Excel to strings ') else: self.chinese_index val = self.chinesetf.text () chinese_index = int(val) - 1 self.excel2Strings() else: self.strings2Excel() def choseType(self,btn): Global isExcel2Strings if BTN. Text () = = "excel turned strings" : isExcel2Strings = True self. ChineseTF. SetEnabled (True) else: isExcel2Strings = False self.chineseTF.setEnabled(False) def showDialog(self): fname = QFileDialog.getOpenFileName(self, 'Open file', '~/Desktop') if fname[0]: filePath = fname[0] self.lab.setText(filePath) global chose_files chose_files = filePath def excel2Strings(self): global chose_files table = xlrd.open_workbook(chose_files) for sheet in table.sheets(): self.dealTable(sheet) def dealTable(self,sheet): rows = sheet.nrows columns = sheet.ncols global chose_files succeed_flag = 0 for j in range(columns): if j ! Arr = chose_files.split('/') del arr[len(arr) -1] strings_path = '/'. Join (arr)+('/ th % d.trings '%j) with open(strings_path,'wb+') as f: for I in range(rows): if I > 0: row = sheet.row_values(i) other_lan = row[j].strip() chinese_lan = row[chinese_index].strip() has_other_lan = False if len(other_lan) > 0 and len(chinese_lan) > 0: has_other_lan = True succeed_flag = succeed_flag + 1 if '"' in other_lan: other_lan = other_lan.replace('"','') if '"' in chinese_lan: chinese_lan = chinese_lan.replace('"','') text = '"' + other_lan + '"' + ' = ' + '"' + chinese_lan + '"' + '\n' f.write(text.encode('utf-8')) if has_other_lan == False: if os.path.exists(strings_path): os.remove(strings_path) else: Self. showAlert(' file not found ') if succeed_flag > 0: self.showAlert(" file generated successfully, please check the path of the file. ") def strings2Excel(self): global chose_files fo = open(chose_files, Readlines () wb = xlwt.workbook (encoding=" utF-8 ") # create a worksheet = wb.add_sheet('sheet') # create a worksheet Sheet. Write (0, 0, 'English') sheet. Write (0, 1, 'remarks') sheet. Write (0, 2,' the revised English) sheet. Write (0, 3, 'description') sheet. Write (0, 4, 'in the numerous') Sheet. Write (0, 5, 'Jane') sheet. Write (0, 6, 'Japanese') sheet. Write (0, 7, 'Italian') sheet. Write (0, 8, 'German') sheet. Write (0, 9 'es') Sheet. Write (0, 10, 'French') sheet. Write (0, 11, 'in Portuguese) sheet. Write (0, 12,' ') sheet. Write (0, 13, 'Korean) Sheet. write(0,14,' 新 的英文= 中 文 ') count = 1 for idx1,singleLine in enumerate(lines): line = singleLine.decode('utf-8') if u'//' not in line : match = re.match('^/.*/$',line) if not match: arr = line.split('=') if len(arr) > 1: english = arr[0] chinese = arr[1] english = english.replace('"','').strip() chinese = chinese.replace('"','').strip() sheet.write(count,0,english) sheet.write(count,1,chinese) count += 1 else: continue else: continue arr = chose_files.split('/') del arr[len(arr) - 1] strings_path = '/'.join(arr)+'/conver.xls' Wb. save(strings_path) # Save excle file self.showAlert(" file generated successfully, please check the imported file path directory ") if __name__ == '__main__': app = QApplication(sys.argv) ex = createWindow() sys.exit(app.exec_())Copy the code