forked from HaruhiFanClub/Excel2RpyScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
146 lines (122 loc) · 6.19 KB
/
app.py
File metadata and controls
146 lines (122 loc) · 6.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import base64
from io import BytesIO
from tkinter import Tk, Text, PhotoImage, Canvas
from tkinter.messagebox import showerror, showinfo
from tkinter.ttk import Frame, Style, Entry, Combobox, Button, Label
from tkinter import filedialog
from corelib.exception import ConvertException, SaveFileException
from handler.converter import Converter
from tools.image_data import *
class Application_ui(Frame):
#这个类仅实现界面生成功能,具体事件处理代码在子类Application中。
def __init__(self, master=None):
Frame.__init__(self, master)
self.master.title('Excel转化Rpy工具')
self.master.geometry('600x343')
self.createWidgets()
def createWidgets(self):
self.top = self.winfo_toplevel()
self.style = Style()
self.bkg_gif = PhotoImage(data=base64.b64decode(back_ground_gif_data))
self.background_label = Label(self.top, image=self.bkg_gif)
self.background_label.place(x=0, y=0, relwidth=1, relheight=1)
self.Text = Text(self.top, font=('宋体',9))
self.Text.place(relx=0.066, rely=0.07, relwidth=0.869, relheight=0.563)
self.saveAddr = Entry(self.top, font=('宋体',9))
self.saveAddr.place(relx=0.355, rely=0.84, relwidth=0.409, relheight=0.052)
self.ComboList = ['源文件目录','自定义目录']
self.Combo = Combobox(self.top, values=self.ComboList, font=('宋体',9), state='readonly')
self.Combo.place(relx=0.184, rely=0.84, relwidth=0.146, relheight=0.058)
self.Combo.set(self.ComboList[0])
self.Combo.bind('<<ComboboxSelected>>', self.comboEvent)
self.style.configure('InputButton.TButton',font=('宋体',9))
self.InputButton = Button(self.top, text='浏览', command=self.InputButton_Cmd, style='InputButton.TButton')
self.InputButton.place(relx=0.184, rely=0.7, relwidth=0.133, relheight=0.073)
self.Haruhi_gif = PhotoImage(data=base64.b64decode(haruhi_gif_data))
self.style.configure('ConvertButton.TButton',font=('宋体',9))
self.ConvertButton = Button(self.top, image=self.Haruhi_gif, command=self.ConvertButton_Cmd, style='ConvertButton.TButton')
self.ConvertButton.place(relx=0.788, rely=0.7, relwidth=0.146, relheight=0.236)
self.style.configure('OutputLabel.TLabel',anchor='w', font=('宋体',9))
self.OutputLabel = Label(self.top, text='保存目录:', style='OutputLabel.TLabel')
self.OutputLabel.place(relx=0.066, rely=0.84, relwidth=0.107, relheight=0.05)
self.style.configure('InputLabel.TLabel',anchor='w', font=('宋体',9))
self.InputLabel = Label(self.top, text='输入设置:', style='InputLabel.TLabel')
self.InputLabel.place(relx=0.066, rely=0.723, relwidth=0.107, relheight=0.05)
class Application(Application_ui):
#这个类实现具体的事件处理回调函数。界面生成代码在Application_ui中。
def __init__(self, master=None):
Application_ui.__init__(self, master)
def convert(self, input_path, output_path):
c = Converter(input_path)
texts = c.generate_rpy_elements()
try:
with open(output_path, 'w', encoding='utf-8') as f:
for k, v in c.role_name_mapping.items():
f.write(v.render() + "\n")
f.write("define narrator_nvl = Character(None, kind=nvl)\n")
f.write("define narrator_adv = Character(None, kind=adv)\n")
f.write("\nlabel start:\n")
for text in texts:
for t in text.triggers:
f.write(" "*4 + t.render() + "\n")
f.write((" "*4 + text.render() + "\n"))
except FileNotFoundError:
raise SaveFileException("保存目录不存在")
def checkEqual(self, iterator):
iterator = iter(iterator)
try:
first = next(iterator)
except StopIteration:
return False
return all(first == rest for rest in iterator)
def getFileName(self, path):
return path.split('/')[-1].split('.')[0]
def getTlist(self):
Tlist=self.Text.get('1.0', 'end').split('\n')
Tlist=[value.strip() for value in Tlist]
Tlist=[value for value in Tlist if value]
return Tlist
def getOriPath(self):
paths=list()
for path in self.getTlist():
paths.append('/'.join(path.split('/')[0:-1]))
if self.checkEqual(paths):
return paths[0]
else:
showerror("获取错误", "未设置输入或源文件不在同一目录下!")
return ''
def comboEvent(self, *arg):
if self.Combo.get()=='源文件目录':
if self.saveAddr.get(): self.saveAddr.delete('0','end')
self.saveAddr.insert('0',self.getOriPath())
elif self.Combo.get()=='自定义目录':
file_path = filedialog.askdirectory(title=u'保存文件到文件夹')
if self.saveAddr.get(): self.saveAddr.delete('0','end')
self.saveAddr.insert('0',file_path)
def InputButton_Cmd(self, event=None):
file_paths = filedialog.askopenfilenames(title=u'选择文件', filetypes=[("Excel-2007 file", "*.xlsx"),("Excel-2003 file", "*.xls"),("all","*.*")])
#print('打开文件:', file_paths)
for line in file_paths:
self.Text.insert('0.0',line+'\n')
self.comboEvent()
def ConvertButton_Cmd(self, event=None):
success_flag = True
for path in self.getTlist():
try:
self.convert(path, self.saveAddr.get()+'/'+self.getFileName(path)+'.rpy')
except ConvertException as err:
success_flag = False
showerror("转换错误", err.msg)
if success_flag:
showinfo("转换成功", "转换完成")
self.saveAddr.delete('0','end')
self.Text.delete('0.0','end')
if __name__ == "__main__":
top = Tk()
# top.iconbitmap('sos.ico')
top.iconphoto(False, PhotoImage(data=base64.b64decode(haruhi_gif_data)))
Application(top).mainloop()
try: top.destroy()
except: pass