居然被推荐了,那就多说两句,一开始的代码是可以直接终端脚本运行的,但是通过py2app打包成可执行文件之后就崩溃,后来一点一点调试才发现,是中文的编码问题。
一开始我的的写文件用的是w,也就是字符串写入
with open(strings_path,'w') as f:
但是这样在最后执行这段代码的时候,就崩溃了,原因是chinese_lan的中文导致的
text = '"' + other_lan + '"' + ' = ' + '"' + chinese_lan + '"' + '\n' f.write(text)
所以后来改为下面这样编码后的才可以
with open(strings_path,'wb+') as f:
text = '"' + other_lan + '"' + ' = ' + '"' + chinese_lan + '"' + '\n' f.write(text.encode('utf-8'))
以下是完整的源码
# 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):
#设置窗口的位置和大小
self.setGeometry(300, 300, 500, 200)
self.setWindowTitle('Excel和多语言')
self.setWindowIcon(QIcon('excelIcon.png'))
global isExcel2Strings
isExcel2Strings = True
global chose_files
chose_files = ''
# 全局布局(纵向)
wlayout = QVBoxLayout()
# 局部布局(横向)
h1 = QHBoxLayout()
h2 = QHBoxLayout()
h3 = QHBoxLayout()
h4 = QHBoxLayout()
#创建一个PushButton
btn = QPushButton('选择文件', self)
btn.clicked.connect(self.showDialog)
btn.resize(btn.sizeHint())#btn.sizeHint()显示默认尺寸
self.lab = QLabel('', self)
h1.addWidget(btn)
h1.addWidget(self.lab)
h1.addStretch()
self.btn1 = QRadioButton('excel转strings')
self.btn1.setChecked(True)
self.btn1.toggled.connect(lambda : self.choseType(self.btn1))
self.btn2 = QRadioButton('strings转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)
self.chineseTF = QLineEdit(self)
validate = QIntValidator()
self.chineseTF.setValidator(validate)#限制只能输入数字
descLab = QLabel('默认从1开始',self)
descLab.setStyleSheet('color:#a2a2a2;')
descLab.setFont(QFont('SansSerif', 12))
h3.addWidget(lab)
h3.addWidget(self.chineseTF)
h3.addWidget(descLab)
h3.addStretch()
btn = QPushButton('开始转换', self)
btn.clicked.connect(self.startConvert)
btn.resize(btn.sizeHint())#btn.sizeHint()显示默认尺寸
h4.addWidget(btn)
h4.addStretch()
wlayout.addLayout(h1)
wlayout.addLayout(h2)
wlayout.addLayout(h3)
wlayout.addLayout(h4)
wlayout.addStretch()
self.setLayout(wlayout)
#显示窗口
self.show()
@classmethod
def showAlert(self,msg):
msg_box = QMessageBox(QMessageBox.Warning, '温馨提示', msg)
msg_box.exec_()
def startConvert(self):
global chose_files
if len(chose_files) == 0:
self.showAlert('请选择项目')
else:
if isExcel2Strings:
global chinese_index
val = self.chineseTF.text()
if len(val) == 0:
self.showAlert('excel转strings,输入中文列数不能为空')
else:
chinese_index = int(val) - 1
self.excel2Strings()
else:
self.strings2Excel()
def choseType(self,btn):
global isExcel2Strings
if btn.text() == "excel转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 != chinese_index:#过滤中文那一栏
arr = chose_files.split('/')
del arr[len(arr) - 1]
strings_path = '/'.join(arr)+('/第%d.strings'%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('文件不存在')
if succeed_flag > 0:
self.showAlert("文件生成成功,请查看导入的文件路径目录")
def strings2Excel(self):
global chose_files
fo = open(chose_files, "rb")
lines = fo.readlines()
wb = xlwt.Workbook(encoding="utf-8") # 创建一个工作薄
sheet = wb.add_sheet('sheet') # 创建一个工作表
sheet.write(0,0,'英文')
sheet.write(0,1,'备注')
sheet.write(0,2,'修改后的英文')
sheet.write(0,3,'说明')
sheet.write(0,4,'繁中')
sheet.write(0,5,'简中')
sheet.write(0,6,'日文')
sheet.write(0,7,'意大利文')
sheet.write(0,8,'德文')
sheet.write(0,9,'西班牙文')
sheet.write(0,10,'法文')
sheet.write(0,11,'葡萄牙文')
sheet.write(0,12,' ')
sheet.write(0,13,'韩文')
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) # 保存excle文件
self.showAlert("文件生成成功,请查看导入的文件路径目录")
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = createWindow()
sys.exit(app.exec_())