質問編集履歴

2

情報追加

2020/12/11 01:21

投稿

3939TJ55
3939TJ55

スコア109

test CHANGED
File without changes
test CHANGED
@@ -31,3 +31,281 @@
31
31
  (base) C:\Users\Desktop\sample>
32
32
 
33
33
  ```
34
+
35
+
36
+
37
+
38
+
39
+ GUIを使用したアプリケーションで、データを読み込み加工、CSVを作成します。
40
+
41
+
42
+
43
+ ```python
44
+
45
+ #ライブラリ
46
+
47
+ import tkinter as tk
48
+
49
+ import tkinter.messagebox as tkm
50
+
51
+ from tkinter import filedialog
52
+
53
+ import pandas as pd
54
+
55
+ import datetime as dt
56
+
57
+ import cx_Oracle
58
+
59
+
60
+
61
+
62
+
63
+ USERID = "00000"
64
+
65
+ PASSWORD = "00000"
66
+
67
+ DESTINATION = "00000"
68
+
69
+
70
+
71
+ #時刻
72
+
73
+ now = dt.datetime.now()
74
+
75
+ time = now.strftime('%Y%m%d-%H%M%S')
76
+
77
+
78
+
79
+ def OpenFileDlg(tbox):
80
+
81
+ ftype = [('','*')]
82
+
83
+ dir = '.'
84
+
85
+ # ファイルダイアログ表示
86
+
87
+ filename = filedialog.askopenfilename(filetypes = ftype, initialdir = dir)
88
+
89
+ # ファイルパスをテキストボックスに表示
90
+
91
+ tbox.insert(0, filename)
92
+
93
+
94
+
95
+ #ボタン関数
96
+
97
+ def btn_click():
98
+
99
+ cc=str(txt1.get())
100
+
101
+ sc=str(txt2.get())
102
+
103
+ SQL="select MAX(TNPMN) from Hstst WHERE KOKC = '" + cc + "'"
104
+
105
+ SQL2 = "select MAX(SYIMN1),MAX(syimn2) from HENSYIMST WHERE SYIC = '" + sc + "'"
106
+
107
+
108
+
109
+
110
+
111
+ if not len(cc) == 6 :
112
+
113
+ tkm.showerror("入力エラー", "顧客番号は6桁です")
114
+
115
+ return
116
+
117
+ elif not len(sc) == 7 :
118
+
119
+ tkm.showerror("入力エラー", "社員番号は7桁です")
120
+
121
+ return
122
+
123
+ elif file_text.get() == '':
124
+
125
+ tkm.showerror('エラー','データファイルを指定してください')
126
+
127
+ return
128
+
129
+ else:
130
+
131
+ with cx_Oracle.connect(USERID, PASSWORD, DESTINATION) as connection:
132
+
133
+ with connection.cursor() as cursor:
134
+
135
+ for row in cursor.execute(SQL):
136
+
137
+ if row == (None,):
138
+
139
+ tkm.showerror("エラー", "顧客番号に問題があります")
140
+
141
+ return
142
+
143
+ else:
144
+
145
+ row_replace = [rows.replace(' ', '')for rows in row]
146
+
147
+ kokyac = ''.join(row_replace)
148
+
149
+ with cx_Oracle.connect(USERID, PASSWORD, DESTINATION) as connection:
150
+
151
+ with connection.cursor() as cursor:
152
+
153
+ for row in cursor.execute(SQL2):
154
+
155
+ if row == (None,None):
156
+
157
+ tkm.showerror("エラー", "社員番号に問題があります")
158
+
159
+ return
160
+
161
+ else:
162
+
163
+ row_replace = [rows.replace(' ', '')for rows in row]
164
+
165
+ syain = ''.join(row_replace)
166
+
167
+ ques = tkm.askokcancel("確認", "店名:"+kokyac+"様\n""処理者:"+syain+"\nでCSVを出力しますか?")
168
+
169
+ if ques == False:
170
+
171
+ return
172
+
173
+
174
+
175
+ f = open(file_text.get(), 'r')
176
+
177
+ colspecs = [(22,25), (25,28),(28,30),(30,32),(22,32),(47,51),(22,28)]
178
+
179
+ names = ["SCAN", now.strftime("%Y%m%d"), now.strftime('%H%M%S'),now.strftime("%Y%m%d-%H%M%S"),sc,cc," "]
180
+
181
+ df = pd.read_fwf(f, skiprows=1,header=None, names=names,colspecs=colspecs,dtype=object)
182
+
183
+ df = df.dropna(subset=["SCAN"])
184
+
185
+
186
+
187
+ f = open(file_text.get(), 'r')
188
+
189
+ colspecss = [(51,54),(54,57),(57,59),(59,61),(51,61),(72,76),(51,57)]
190
+
191
+ dfA = pd.read_fwf(f,header=None, names=names, colspecs=colspecss,dtype=object)
192
+
193
+ dfA = dfA.dropna(subset=["SCAN"])
194
+
195
+ df_concat = pd.concat([df, dfA])
196
+
197
+ df_concat[cc]=df_concat[cc].astype(int)
198
+
199
+ df_concat[cc] = df_concat.groupby(sc)[cc].transform('sum')
200
+
201
+ df_sum = df_concat.drop_duplicates(sc)
202
+
203
+
204
+
205
+ trk = df_concat[" "]
206
+
207
+ trk = ','.join(map(str,trk))
208
+
209
+ SQL3 = "SELECT RHB,TRKH FROM ShnZok WHERE RHB IN" "(" + trk + ")"
210
+
211
+ dfC = pd.DataFrame()
212
+
213
+
214
+
215
+ with cx_Oracle.connect(USERID, PASSWORD, DESTINATION) as connection:
216
+
217
+ with connection.cursor() as cursor:
218
+
219
+ for row in cursor.execute(SQL3):
220
+
221
+ dfC = dfC.append([row])
222
+
223
+ dfC=dfC.reset_index(drop=True)
224
+
225
+ dfD = pd.merge(df_sum,dfC,left_on = " ",right_on=0)
226
+
227
+ drop_col = [sc," ", 0]
228
+
229
+ df_result = dfD.drop(drop_col, axis=1)
230
+
231
+ df_result = df_result[["SCAN", now.strftime("%Y%m%d"), now.strftime('%H%M%S'),now.strftime("%Y%m%d-%H%M%S"),1,cc]]
232
+
233
+ df_result = df_result.rename(columns={1: sc})
234
+
235
+ df_result[" "] = ""
236
+
237
+
238
+
239
+ df_result.to_csv(cc+"_{}.csv".format(time), index= False,encoding="shift-jis")
240
+
241
+ tkm.showinfo("情報", "CSVを保存しました")
242
+
243
+
244
+
245
+ #画面表示
246
+
247
+ root = tk.Tk()
248
+
249
+ root.geometry('500x400+600+300')
250
+
251
+ root.title('入力画面')
252
+
253
+
254
+
255
+ #顧客番号
256
+
257
+ lbl1 = tk.Label(text='顧客番号',font=(u'MS ゴシック', 11, 'bold'))
258
+
259
+ lbl1.place(x=85, y=145)
260
+
261
+ txt1 = tk.Entry(width=30)
262
+
263
+ txt1.place(x=160, y=150)
264
+
265
+
266
+
267
+ #社員番号
268
+
269
+ lbl2 = tk.Label(text='社員番号',font=(u'MS ゴシック', 11, 'bold'))
270
+
271
+ lbl2.place(x=85, y=175)
272
+
273
+ txt2 = tk.Entry(width=30)
274
+
275
+ txt2.place(x=160, y=180)
276
+
277
+
278
+
279
+ # Excelファイルダイアログ
280
+
281
+ label = tk.Label(root, text='データファイル',font=(u'MS ゴシック', 10, 'bold'))
282
+
283
+ label.place(x=100, y=95)
284
+
285
+ file_text = tk.Entry(root, width=40)
286
+
287
+ file_text.place(x=100, y=115)
288
+
289
+ fdlg_button = tk.Button(root, text='ファイル選択', command = lambda: OpenFileDlg(file_text) )
290
+
291
+ fdlg_button.place(x=360, y=110)
292
+
293
+
294
+
295
+ #ボタン
296
+
297
+ btn = tk.Button(root, text='CSV出力',width=20, font=("Menlo",11),command=btn_click)
298
+
299
+ btn.place(x=155, y=220)
300
+
301
+
302
+
303
+ btn1 = tk.Button(root, text="終了", width=20, font=("Menlo",11),command=root.destroy)
304
+
305
+ btn1.place(x=155, y=250)
306
+
307
+
308
+
309
+ root.mainloop() #表示保持
310
+
311
+ ```

1

修正

2020/12/11 01:21

投稿

3939TJ55
3939TJ55

スコア109

test CHANGED
File without changes
test CHANGED
@@ -28,6 +28,6 @@
28
28
 
29
29
 
30
30
 
31
- (base) C:\Users\j-takayanagi\Desktop\sample>
31
+ (base) C:\Users\Desktop\sample>
32
32
 
33
33
  ```