-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv2xlsx_gui.py
133 lines (111 loc) · 4.32 KB
/
csv2xlsx_gui.py
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
#-------------------------------------------------------------------------------
# Name: csv2xlsx GUI
# Purpose: csv to xlsx
#
# Author: Ekira
#
# Created: 09/02/2017
# Modified: 12/10/2018
# Copyright: (C) Ekira 2017
# Licence: <GNU Lesser General Public License>
#-------------------------------------------------------------------------------
# -*- coding: utf-8 -*- #
import xlwt, csv, os
import codecs, chardet
import wx
import time
def csv_to_xlsx(filepath, tgtdirpath):
filename = filepath.split('/')[-1].split('\\')[-1]
_filename = filename[:-4]
targetpath = os.path.join(tgtdirpath, _filename + ".xlsx")
while os.path.exists(targetpath):
targetpath = os.path.join(tgtdirpath, _filename + "-" + str(int(time.time())) + ".xlsx")
ret = str()
f = open(filepath, "rb")
encoding = chardet.detect(f.read(1024))["encoding"]
if encoding == "GB2312":
encoding = "gbk"
f.close()
with codecs.open(filepath, 'r', encoding=encoding) as f:
read = csv.reader(f)
workbook = xlwt.Workbook()
sheet = workbook.add_sheet(_filename)
l = 0
for line in read:
r = 0
for i in line:
sheet.write(l, r, i)
r += 1
l += 1
workbook.save(targetpath)
ret = "成功,原始文件编码:%s,文件 %s 转换为 %s\n\r" % (encoding, filepath, targetpath)
return ret
class Csv2XlsWindow(wx.Dialog):
def __init__(self, parent, title, size):
wx.Dialog.__init__(self, parent, title=title, size=size)
self.InitUI()
def InitUI(self):
panel = wx.Panel(self)
panel.SetFont(wx.Font(14, wx.DEFAULT, wx.NORMAL, wx.NORMAL))
p_origin = wx.Button(parent=panel, label="选择文件", pos=(10,10))
p_origin_path = wx.StaticText(parent=panel, label="", pos=(125,15))
p_target = wx.Button(parent=panel, label="保存路径", pos=(10,45))
p_target_path = wx.StaticText(parent=panel, label="", pos=(125,50))
p_run = wx.Button(parent=panel, label="开始转换", pos=(310,80))
p_log = wx.TextCtrl(parent=panel, pos=(10,115), size=(465, 240), style=wx.TE_READONLY | wx.TE_MULTILINE)
self.panel = panel
self.p_origin = p_origin
self.p_target = p_target
self.p_run = p_run
self.p_origin_path = p_origin_path
self.p_target_path = p_target_path
self.p_log = p_log
self.paths = list()
self.tgtpath = str()
self.Bind(wx.EVT_BUTTON, self.OnOriginButton, p_origin)
self.Bind(wx.EVT_BUTTON, self.OnTargetButton, p_target)
self.Bind(wx.EVT_BUTTON, self.OnRunButton, p_run)
def OnOriginButton(self, event):
filesFilter = "所有文件|*.*|Csv (*.csv)|*.csv"
dlg = wx.FileDialog(parent=self.panel, message="选择原始文件", wildcard=filesFilter, style=wx.FD_OPEN|wx.FD_MULTIPLE)
dialogResult = dlg.ShowModal()
if dialogResult != wx.ID_OK:
dlg.Destroy()
return
paths = dlg.GetPaths()
self.p_origin_path.SetLabel('')
pathstr = str()
self.paths.clear()
for path in paths:
pathstr += path + "|"
self.paths.append(path)
self.p_origin_path.SetLabel(pathstr)
dlg.Destroy()
def OnTargetButton(self, event):
dlg = wx.DirDialog (parent=self.panel, message="选择保存路径", style=wx.DD_DEFAULT_STYLE | wx.DD_DIR_MUST_EXIST)
dialogResult = dlg.ShowModal()
if dialogResult != wx.ID_OK:
dlg.Destroy()
return
path = dlg.GetPath()
self.p_target_path.SetLabel(path)
self.tgtpath = path
dlg.Destroy()
def OnRunButton(self, event):
paths = self.paths
tgtpath = self.tgtpath
log = self.p_log
if len(paths) == 0 or not os.path.exists(tgtpath):
return
self.p_origin.Enable(False)
self.p_target.Enable(False)
self.p_run.Enable(False)
for path in paths:
try:
ret = csv_to_xlsx(path, tgtpath)
log.AppendText(ret)
except Exception as e:
log.AppendText("===转换失败===%s\n\r" % path)
self.p_origin.Enable(True)
self.p_target.Enable(True)
self.p_run.Enable(True)