質問編集履歴

3

タイトルをわかりやすいように修正、不要なコードを削除

2019/12/08 12:41

投稿

suger365
suger365

スコア7

test CHANGED
File without changes
test CHANGED
@@ -6,16 +6,26 @@
6
6
 
7
7
 
8
8
 
9
- https://teratail.com/questions/227911
9
+ ~~https://teratail.com/questions/227911~~
10
-
10
+
11
- が必要そうな内容を切り抜いて質問させていただいていると思うのですが
11
+ ~~が必要そうな内容を切り抜いて質問させていただいていると思うのですが
12
-
12
+
13
- 不足していそうなので別で質問させていただきます。
13
+ 不足していそうなので別で質問させていただきます。~~
14
+
15
+ 不足していませんでした。むしろ単純ミスで、これだけ長いコードを公開したせいで分かりにくくなっていました。
16
+
17
+ 解決した内容を記入しておきましたので許して…
18
+
19
+
14
20
 
15
21
 
16
22
 
17
23
  ### 前提・実現したいこと
18
24
 
25
+ ・セルの値が空白かつIDが一致したときに値をセルに入れたい
26
+
27
+
28
+
19
29
  ・入退出管理システムを作っています
20
30
 
21
31
  ・入室のみのログ、退出のみのログはとれるようになりました
@@ -62,436 +72,170 @@
62
72
 
63
73
  が一致した場合はExcelの入室記録のわきに退出記録を入れたいです。
64
74
 
65
-
66
-
67
-
68
-
69
- ### 該当のソースコード
75
+
70
-
71
- (メインとなるプログラム)
72
76
 
73
77
  ```python
74
78
 
75
- import sys
76
-
77
- import tkinter as tk
78
-
79
- import tkinter_Sub_Excel
80
-
81
- import datetime
82
-
83
- from datetime import datetime
84
-
85
- from tkinter import *
86
-
87
-
88
-
89
-
90
-
91
- root = tk.Tk()
92
-
93
-
94
-
95
- #コールバック時の関数指定
96
-
97
- def callback(sv):
98
-
99
- c = sv.get()[0:4]
100
-
101
- #print("c=" ,c)
102
-
103
- sv.set(c)
104
-
105
-
106
-
107
- #文字列検証関数
108
-
109
- def validation(before_word,after_word):
110
-
111
- return((after_word.isdecimal())) and (len(after_word) <= 4) or (len(after_word) ==0 )
112
-
113
-
114
-
115
- #出勤ボタンクリック時のイベント指定
116
-
117
- def InWork(event):
118
-
119
- Status = "出勤"
120
-
121
- EmpNum = Number.get()
122
-
123
- EmpNum_Count = len(EmpNum)
124
-
125
- if EmpNum_Count == 4:
126
-
127
- d_now = str(format(datetime.now().strftime("%Y/%m/%d")))
128
-
129
- t_now = str(format(datetime.now().strftime("%H:%M:%S")))
130
-
131
- print(EmpNum,d_now,t_now,Status)
132
-
133
- tkinter_Sub_Excel.In_Excelwrite(Status,EmpNum)
134
-
135
- Number.delete(0,tk.END)
136
-
137
- res1 = str(format(datetime.now().strftime("%Y/%m/%d %H:%M:%S")))
138
-
139
- res2 = "社員番号"+str(EmpNum)+"の"+Status+"を確認しました"
140
-
141
- Check["text"] = res1 + "\n" + res2
142
-
143
- def clear():
144
-
145
- Check["text"] = ""
146
-
147
- root.after(2000,clear)
148
-
149
- else:
150
-
151
- Check["text"] = "エラー" + "\n" + "社員番号の入力方法が違います"
152
-
153
- Number.delete(0,tk.END)
154
-
155
- def clear():
156
-
157
- Check["text"] = ""
158
-
159
- root.after(2000,clear)
160
-
161
- return
162
-
163
-
164
-
165
- def OutWork(event):
166
-
167
- Status = "退勤"
168
-
169
- EmpNum = Number.get()
170
-
171
- print(type(EmpNum))
172
-
173
- EmpNum_Count = len(EmpNum)
174
-
175
- if EmpNum_Count == 4:
176
-
177
- d_now = str(format(datetime.now().strftime("%Y/%m/%d")))
178
-
179
- t_now = str(format(datetime.now().strftime("%H:%M:%S")))
180
-
181
- print(EmpNum,d_now,t_now,Status)
182
-
183
- tkinter_Sub_Excel.Out_Excelwrite(Status,EmpNum)#ここで呼び出すSub_excelがうまく動かないです
184
-
185
- Number.delete(0,tk.END)
186
-
187
- res1 = str(format(datetime.now().strftime("%Y/%m/%d %H:%M:%S")))
188
-
189
- res2 = "社員番号"+str(EmpNum)+"の"+Status+"確認しました"
190
-
191
- Check["text"] = res1 +"\n"+res2
192
-
193
- def clear():
194
-
195
- Check["text"] = ""
196
-
197
- root.after(2000,clear)
198
-
199
- else:
200
-
201
- Check["text"] = "エラー" + "\n" + "社員番号の入力方法が違います"
202
-
203
- Number.delete(0,tk.END)
204
-
205
- def clear():
206
-
207
- Check["text"] = ""
208
-
209
- root.after(2000,clear)
210
-
211
- return
212
-
213
-
214
-
215
- #トレースした時の何か?
216
-
217
- sv = StringVar()
218
-
219
- sv.trace("w",lambda name , index , mode , sv=sv:callback(sv))
220
-
221
-
222
-
223
- #ウィンドウラベル設定
224
-
225
- root.title(u"勤怠管理システム")
226
-
227
-
228
-
229
- #ウィンドウサイズ変更
230
-
231
- root.geometry("600x400")
232
-
233
-
234
-
235
- #ラベルを設定
236
-
237
- Static1 = tk.Label(text=u"社員番号",font=(u"MS 明朝",20))
238
-
239
- Static1.place(x=50,y=100)
240
-
241
-
242
-
243
- #entryを設定
244
-
245
- Number = tk.Entry(font=("15","25"),textvariable=sv)
246
-
247
- # %sは変更前文字列、%Pは変更後文字列を引数で渡す
248
-
249
- vcmd = (Number.register(validation), "%s" , "%P")
250
-
251
- #validationコマンドを設定
252
-
253
- Number.configure(validate = "key",vcmd = vcmd)
254
-
255
- Number.place(x=175,y=100)
256
-
257
- Number.focus_set()
258
-
259
-
260
-
261
- #buttonを設定
262
-
263
- InEnter = tk.Label(root,text = "出勤",font=("25","40"),bg="blue")
264
-
265
- InEnter.place(x=200,y=175)
266
-
267
- InEnter.bind("<Button-1>", InWork)
268
-
269
-
270
-
271
- OutEnter = tk.Label(root,text = "退勤",font=("25","40"),bg="green")
272
-
273
- OutEnter.place(x=400,y=175)
274
-
275
- OutEnter.bind("<Button-1>", OutWork)
276
-
277
-
278
-
279
- #メッセージ用キャンパスを設定
280
-
281
- CheckArea = tk.Canvas(root,width = 600,height = 300)
282
-
283
- CheckArea.create_rectangle(0,0,600,300,fill = "lightskyblue",outline = "black")
284
-
285
- CheckArea.place(x=0,y=250)
286
-
287
- Check = tk.Label(root,text="",font=(u"MS 明朝",20),fg="black",bg="lightskyblue")
288
-
289
- Check.place(x=110,y=290)
290
-
291
-
292
-
293
-
294
-
295
- #現在時刻を取得
296
-
297
- #現在時刻に切り替え
298
-
299
- def update():
300
-
301
- Date["text"] = "{}".format(datetime.now().strftime("%Y/%m/%d"))
302
-
303
- Time["text"] = "{}".format(datetime.now().strftime("%H:%M:%S"))
304
-
305
- root.after(1,update)
306
-
307
- #時刻をGUIで表示
308
-
309
- Date = tk.Label(root,text="",font=(u"MS 明朝",30),fg="black")
310
-
311
- Time = tk.Label(root,text="",font=(u"MS 明朝",30),fg="black")
312
-
313
- Date.place(x=60,y=25)
314
-
315
- Time.place(x=300,y=25)
316
-
317
- update()
318
-
319
-
320
-
321
-
322
-
323
-
324
-
325
- root.mainloop()
79
+ import openpyxl
80
+
81
+ from datetime import datetime
82
+
83
+
84
+
85
+ def In_Excelwrite(Status,EmpNum):
86
+
87
+ #Excelファイルを開く
88
+
89
+ Inwb = openpyxl.load_workbook(r"C:\python\tkinter\In_LogLIst.xlsx")
90
+
91
+ Inws = Inwb["IN_LIST"]
92
+
93
+
94
+
95
+ #最終行取得など
96
+
97
+ MaxLow = Inwb["IN_LIST"].max_row
98
+
99
+
100
+
101
+ #各種データ
102
+
103
+ d_now = str(format(datetime.now().strftime("%Y/%m/%d %H:%M:%S")))
104
+
105
+ print(EmpNum,d_now,Status)
106
+
107
+ #出勤にデータを入れる
108
+
109
+ Inws.cell(row = MaxLow + 1 , column = 1 ,value = EmpNum)
110
+
111
+ Inws.cell(row = MaxLow + 1 , column = 2 ,value = d_now)
112
+
113
+ Inws.cell(row = MaxLow + 1 , column = 3 ,value = Status)
114
+
115
+
116
+
117
+ #保存する
118
+
119
+ Inwb.save(r"C:\python\tkinter\In_LogLIst.xlsx")
120
+
121
+
122
+
123
+ ##ここからメインファイル用
124
+
125
+ #メインのExcelファイルを開く
126
+
127
+ Mainwb = openpyxl.load_workbook(r"C:\python\tkinter\Main_LogList.xlsx")
128
+
129
+ Mainws = Mainwb["MAIN_LIST"]
130
+
131
+
132
+
133
+ MainMaxLow = Mainwb["MAIN_LIST"].max_row
134
+
135
+ #メインにデータを入れる**(データが入りません)**
136
+
137
+ Mainws.cell(row = MainMaxLow + 1 , column = 1 ,value = EmpNum)
138
+
139
+ Mainws.cell(row = MainMaxLow + 1 , column = 2 ,value = d_now)
140
+
141
+ Mainws.cell(row = MainMaxLow + 1 , column = 3 ,value = Status)
142
+
143
+ #保存する
144
+
145
+ Mainwb.save(r"C:\python\tkinter\Main_LogList.xlsx")
146
+
147
+
148
+
149
+ #処理完了
150
+
151
+ print("処理が完了しました")
152
+
153
+
154
+
155
+ def Out_Excelwrite(Status,EmpNum):
156
+
157
+ #Excelファイルを開く
158
+
159
+ Outwb = openpyxl.load_workbook(r"C:\python\tkinter\Out_LogList.xlsx")
160
+
161
+ Outws = Outwb["OUT_LIST"]
162
+
163
+
164
+
165
+ #最終行取得など
166
+
167
+ MaxLow = Outwb["OUT_LIST"].max_row
168
+
169
+ #各種データ
170
+
171
+ d_now = str(format(datetime.now().strftime("%Y/%m/%d %H:%M:%S")))
172
+
173
+ print(EmpNum,d_now,Status)
174
+
175
+ #退勤にデータを入れる
176
+
177
+ Outws.cell(row = MaxLow + 1 , column = 1 ,value = EmpNum)
178
+
179
+ Outws.cell(row = MaxLow + 1 , column = 2 ,value = d_now)
180
+
181
+ Outws.cell(row = MaxLow + 1 , column = 3 ,value = Status)
182
+
183
+
184
+
185
+ #保存する
186
+
187
+ Outwb.save(r"C:\python\tkinter\Out_LogList.xlsx")
188
+
189
+
190
+
191
+ ##ここからメインファイル用
192
+
193
+ #メインのExcelファイル開く
194
+
195
+ Mainwb = openpyxl.load_workbook(r"C:\python\tkinter\Main_LogList.xlsx")
196
+
197
+ Mainws = Mainwb["MAIN_LIST"]
198
+
199
+
200
+
201
+ MainMaxLow = Mainwb["MAIN_LIST"].max_row
202
+
203
+ #メインにデータを入れる ここ違う
204
+
205
+ for row_num in range(1,Mainws.max_row):
206
+
207
+ ID = Mainws.cell(row = row_num , column = 1).value
208
+
209
+ if str(ID) in EmpNum:
210
+
211
+ Mainws.cell(row = row_num , column = 4 ,value = EmpNum)#ここでvalueを入れている位置が違ったようです
212
+
213
+ Mainws.cell(row = row_num , column = 5 ,value = d_now)
214
+
215
+ Mainws.cell(row = row_num , column = 6 ,value = Status)
216
+
217
+ print("ALLRIGHT")
218
+
219
+ else:
220
+
221
+ print("Error")
222
+
223
+
224
+
225
+ #保存する
226
+
227
+ Mainwb.save(r"C:\python\tkinter\Main_LogList.xlsx")
228
+
229
+
230
+
231
+ #処理完了
232
+
233
+ print("処理が完了しました")
326
234
 
327
235
  ```
328
236
 
329
237
 
330
238
 
331
- (Sub_Excel,呼び出されるほう)
332
-
333
- ```python
334
-
335
- import openpyxl
336
-
337
- from datetime import datetime
338
-
339
-
340
-
341
- def In_Excelwrite(Status,EmpNum):
342
-
343
- #Excelファイルを開く
344
-
345
- Inwb = openpyxl.load_workbook(r"C:\python\tkinter\In_LogLIst.xlsx")
346
-
347
- Inws = Inwb["IN_LIST"]
348
-
349
-
350
-
351
- #最終行取得など
352
-
353
- MaxLow = Inwb["IN_LIST"].max_row
354
-
355
-
356
-
357
- #各種データ
358
-
359
- d_now = str(format(datetime.now().strftime("%Y/%m/%d %H:%M:%S")))
360
-
361
- print(EmpNum,d_now,Status)
362
-
363
- #出勤にデータを入れる
364
-
365
- Inws.cell(row = MaxLow + 1 , column = 1 ,value = EmpNum)
366
-
367
- Inws.cell(row = MaxLow + 1 , column = 2 ,value = d_now)
368
-
369
- Inws.cell(row = MaxLow + 1 , column = 3 ,value = Status)
370
-
371
-
372
-
373
- #保存する
374
-
375
- Inwb.save(r"C:\python\tkinter\In_LogLIst.xlsx")
376
-
377
-
378
-
379
- ##ここからメインファイル用
380
-
381
- #メインのExcelファイルを開く
382
-
383
- Mainwb = openpyxl.load_workbook(r"C:\python\tkinter\Main_LogList.xlsx")
384
-
385
- Mainws = Mainwb["MAIN_LIST"]
386
-
387
-
388
-
389
- MainMaxLow = Mainwb["MAIN_LIST"].max_row
390
-
391
- #メインにデータを入れる**(データが入りません)**
392
-
393
- Mainws.cell(row = MainMaxLow + 1 , column = 1 ,value = EmpNum)
394
-
395
- Mainws.cell(row = MainMaxLow + 1 , column = 2 ,value = d_now)
396
-
397
- Mainws.cell(row = MainMaxLow + 1 , column = 3 ,value = Status)
398
-
399
- #保存する
400
-
401
- Mainwb.save(r"C:\python\tkinter\Main_LogList.xlsx")
402
-
403
-
404
-
405
- #処理完了
406
-
407
- print("処理が完了しました")
408
-
409
-
410
-
411
- def Out_Excelwrite(Status,EmpNum):
412
-
413
- #Excelファイルを開く
414
-
415
- Outwb = openpyxl.load_workbook(r"C:\python\tkinter\Out_LogList.xlsx")
416
-
417
- Outws = Outwb["OUT_LIST"]
418
-
419
-
420
-
421
- #最終行取得など
422
-
423
- MaxLow = Outwb["OUT_LIST"].max_row
424
-
425
- #各種データ
426
-
427
- d_now = str(format(datetime.now().strftime("%Y/%m/%d %H:%M:%S")))
428
-
429
- print(EmpNum,d_now,Status)
430
-
431
- #退勤にデータを入れる
432
-
433
- Outws.cell(row = MaxLow + 1 , column = 1 ,value = EmpNum)
434
-
435
- Outws.cell(row = MaxLow + 1 , column = 2 ,value = d_now)
436
-
437
- Outws.cell(row = MaxLow + 1 , column = 3 ,value = Status)
438
-
439
-
440
-
441
- #保存する
442
-
443
- Outwb.save(r"C:\python\tkinter\Out_LogList.xlsx")
444
-
445
-
446
-
447
- ##ここからメインファイル用
448
-
449
- #メインのExcelファイルを開く
450
-
451
- Mainwb = openpyxl.load_workbook(r"C:\python\tkinter\Main_LogList.xlsx")
452
-
453
- Mainws = Mainwb["MAIN_LIST"]
454
-
455
-
456
-
457
- MainMaxLow = Mainwb["MAIN_LIST"].max_row
458
-
459
- #メインにデータを入れる ここ違う
460
-
461
- for row_num in range(1,Mainws.max_row):
462
-
463
- ID = Mainws.cell(row = row_num , column = 1).value
464
-
465
- if str(ID) in EmpNum:
466
-
467
- Mainws.cell(row = row_num , column = 4 ,value = EmpNum)
468
-
469
- Mainws.cell(row = row_num , column = 5 ,value = d_now)
470
-
471
- Mainws.cell(row = row_num , column = 6 ,value = Status)
472
-
473
- print("ALLRIGHT")
474
-
475
- else:
476
-
477
- print("Error")
478
-
479
-
480
-
481
- #保存する
482
-
483
- Mainwb.save(r"C:\python\tkinter\Main_LogList.xlsx")
484
-
485
-
486
-
487
- #処理完了
488
-
489
- print("処理が完了しました")
490
-
491
- ```
492
-
493
-
494
-
495
239
  ### 試したこと
496
240
 
497
241
  エラーが出ていないので調べようにも調べられずやれたことは少ないですが…

2

タイトルをわかりやすいように修正

2019/12/08 12:41

投稿

suger365
suger365

スコア7

test CHANGED
@@ -1 +1 @@
1
- 室記録は記入されるが退出記録は記載されない
1
+ python + openpyxl 値を検索し、セルが空白の時に値を入れる
test CHANGED
File without changes

1

別に質問していることを追記

2019/12/08 12:14

投稿

suger365
suger365

スコア7

test CHANGED
File without changes
test CHANGED
@@ -4,7 +4,13 @@
4
4
 
5
5
  コードは長いですが本題は退出記録を記載したいだけなのでそれ以外は飛ばしていただいて構いません。
6
6
 
7
+
8
+
9
+ https://teratail.com/questions/227911
10
+
11
+ が必要そうな内容を切り抜いて質問させていただいていると思うのですが
12
+
7
- (修正は)
13
+ 不足していそうなので別で質問させていただきます。
8
14
 
9
15
 
10
16