```python
'''
Descripttion:
version: 请写项目版本
Author: @cdm
Date: 2021-12-30 11:07:51
LastEditors: @cdm
LastEditTime: 2021-12-31 13:50:37
No pains no gains
'''
import threading
import tkinter
from tkinter import filedialog, scrolledtext, END
from openpyxl import Workbook,load_workbook
from openpyxl.styles import *
import os
import warnings
# file_name()
###############################################################################
class DirDialog:
def __init__(self):
self.fileList = []
self.result = ""
mac = tkinter.Tk() # 初始化Tk()创建Mac窗口对象
mac.title('excel2Interface')
mac.geometry("600x500+22+83")
self.label3 = tkinter.Label(mac, text="一键转换excel成为interface格式, Civil制作")
self.label3.place(relx=0.05, rely=0.01)
self.label = tkinter.Label(mac, text="当前选择路径")
self.label.place(relx = 0.1, rely = 0.8)
self.button1 = tkinter.Button(mac, text="选择", command=self.selectDir)
self.button1.place(relx=0.8, rely=0.8)
self.label1 = tkinter.Label(mac, text="文件保存路径")
self.label1.place(relx=0.1, rely=0.9)
self.editor1 = tkinter.Entry(mac, bd=1, width=40)
self.editor1.place(relx=0.3, rely=0.9)
self.buttonOut = tkinter.Button(mac, text="选择保存地址", command=self.saveDir)
self.buttonOut.place(relx=0.8, rely=0.9)
self.button2 = tkinter.Button(mac, text="开始转换", command=self.startParseExcel)
self.button2.place(relx=0.9, rely=0.8)
self.editor = tkinter.Entry(mac,bd = 1, width=40)
self.editor.place(relx = 0.3, rely = 0.8)
self.log = scrolledtext.ScrolledText(width=60,height=20,)
self.log.place(relx = 0.1, rely = 0.1)
mac.mainloop()
def saveDir(self):
folderpath = tkinter.filedialog.askdirectory()
self.editor1.insert(0 ,folderpath)
def selectDir(self):
folderpath = tkinter.filedialog.askdirectory()
self.file_name(folderpath)
self.editor.insert(0 ,folderpath)
self.editor1.insert(0, folderpath)
def startThread(self):
t1 = threading.Thread(self.startParseExcel())
t1.start()
def startParseExcel(self):
if self.editor1.get() == "":
self.log.insert('end', "输出文件不能为空")
# 第一次打开清空下
with open(self.editor1.get() + "/data.ts", 'w+') as f:
f.truncate()
for file in self.fileList:
# if not os.access(file, os.R_OK):
# self.log.insert("end", "%s表格已被打开,转换失败\n" % file)
# self.log.see(END);
# continue
self.parseExcel(file);
self.log.insert("end", "开始转换%s表格\n"%file)
def saveJson(self, json):
with open(self.editor1.get() + "/data.ts", 'a+', encoding='utf-8') as fs:
fs.write(json)
self.log.insert("end", "转换完毕\n")
self.log.see(END);
def parseExcel(self, file):
# 开始转换
warnings.filterwarnings('ignore')
print(file);
wb = load_workbook(self.editor.get() + "/" + file)
book = wb.active
interfaceName = os.path.splitext(file)[0]
type_str = '\n\n export interface %sCfg {\n'%interfaceName;
note = "";
for row in book.iter_rows(min_row=2, max_col=book.max_column, max_row=2):
for idx, cell in enumerate(row):
# print(row1)
if cell.value.endswith(".p"):
type_str += "\t" + cell.value[0: -2];
elif cell.value.endswith(')'):
type_str += "\t" + cell.value[1: -1];
else:
type_str += "\t%s: " % cell.value;
note = book.cell(row=3, column=idx + 1).value;
if note == "numberArr":
note = "number[]"
elif note == 'stringArr':
note = "string[]"
elif note.startswith("function"):
note = "string";
type_str += note + ";";
type_str += " //" + book.cell(row=1, column=idx + 1).value;
type_str += '\n';
if idx + 1 == book.max_column:
type_str += "}"
print(type_str)
# self.result += type_str;
self.saveJson(type_str);
def file_name(self, file_dir="./"):
self.fileList = []
for root, dirs, files in os.walk(file_dir):
for file in files:
res = os.path.splitext(file);
if res[1].endswith('.xlsx'):
self.log.insert("end", "扫描出文件{fileName}\n".format(fileName = file))
self.fileList.append(file)
self.log.see(END);
# print(file) # 当前路径下所有非目录子文件
###############################################################################
if __name__ == '__main__':
app = DirDialog()
需要自取
|