etherbegさんの解決案を組み込み、以下のプログラムとなり、上手くいきました。
python
1 import pandas as pd
2 from openpyxl import Workbook
3 import glob
4 import datetime
5
6 today = datetime . date . today ( )
7 year = today . year
8 month = today . month
9
10 input_value = input ( "{}年{:02}月分の集計を行います。Enterを押してください。 (別の月を集計する場合は6桁で年月を入力し、Enterを押してください) > " . format ( year , month ) )
11 if not input_value :
12 input_value = "{}{:02}" . format ( year , month )
13
14 file_list1 = glob . glob ( '//192.168.3.141/pi/mydata' + input_value + '*' )
15 file_list2 = glob . glob ( '//192.168.3.140/pi/mydata' + input_value + '*' )
16 file_list3 = glob . glob ( '//192.168.3.139/pi/mydata' + input_value + '*' )
17 file_list4 = glob . glob ( '//192.168.3.135/pi/mydata' + input_value + '*' )
18 file_list5 = glob . glob ( '//192.168.3.132/pi/mydata' + input_value + '*' )
19 file_list6 = glob . glob ( '//192.168.3.131/pi/mydata' + input_value + '*' )
20 file_list7 = glob . glob ( '//192.168.3.134/pi/mydata' + input_value + '*' )
21
22 g = '三課伸線稼働率(全機械)'
23 monthly_file = g + f' { input_value } .xlsx'
24
25 rcd1 = { '日付' : [ ] , '稼働時間(分)' : [ ] , '無人稼働時間(分)' : [ ] , '停止時間(分)' : [ ] , 'トータル時間(分)' : [ ] , '稼働率(%)' : [ ] }
26
27 for file1 in file_list1 :
28 a1 = pd . read_csv ( file1 )
29 d1 = ( file1 [ 25 : 33 ] )
30 x1 = a1 . iloc [ : , 0 ] . count ( )
31 y1 = a1 . iloc [ : , 1 ] . sum ( )
32 z1 = a1 . iloc [ : , 2 ] . sum ( )
33 u1 = x1 - y1 - z1
34 w1 = round ( y1 / x1 * 100 , 1 )
35 rcd1 [ '日付' ] . append ( d1 )
36 rcd1 [ '稼働時間(分)' ] . append ( y1 )
37 rcd1 [ '無人稼働時間(分)' ] . append ( z1 )
38 rcd1 [ '停止時間(分)' ] . append ( u1 )
39 rcd1 [ 'トータル時間(分)' ] . append ( x1 )
40 rcd1 [ '稼働率(%)' ] . append ( w1 )
41
42 df1 = pd . DataFrame ( rcd1 )
43
44 rcd2 = { '日付' : [ ] , '稼働時間(分)' : [ ] , '無人稼働時間(分)' : [ ] , '停止時間(分)' : [ ] , 'トータル時間(分)' : [ ] , '稼働率(%)' : [ ] }
45
46 for file2 in file_list2 :
47 a2 = pd . read_csv ( file2 )
48 d2 = ( file2 [ 25 : 33 ] )
49 x2 = a2 . iloc [ : , 0 ] . count ( )
50 y2 = a2 . iloc [ : , 1 ] . sum ( )
51 z2 = a2 . iloc [ : , 2 ] . sum ( )
52 u2 = x2 - y2 - z2
53 w2 = round ( y2 / x2 * 100 , 1 )
54 rcd2 [ '日付' ] . append ( d2 )
55 rcd2 [ '稼働時間(分)' ] . append ( y2 )
56 rcd2 [ '無人稼働時間(分)' ] . append ( z2 )
57 rcd2 [ '停止時間(分)' ] . append ( u2 )
58 rcd2 [ 'トータル時間(分)' ] . append ( x2 )
59 rcd2 [ '稼働率(%)' ] . append ( w2 )
60
61 df2 = pd . DataFrame ( rcd2 )
62
63 rcd3 = { '日付' : [ ] , '稼働時間(分)' : [ ] , '無人稼働時間(分)' : [ ] , '停止時間(分)' : [ ] , 'トータル時間(分)' : [ ] , '稼働率(%)' : [ ] }
64
65 for file3 in file_list3 :
66 a3 = pd . read_csv ( file3 )
67 d3 = ( file3 [ 25 : 33 ] )
68 x3 = a3 . iloc [ : , 0 ] . count ( )
69 y3 = a3 . iloc [ : , 1 ] . sum ( )
70 z3 = a3 . iloc [ : , 2 ] . sum ( )
71 u3 = x3 - y3 - z3
72 w3 = round ( y3 / x3 * 100 , 1 )
73 rcd3 [ '日付' ] . append ( d3 )
74 rcd3 [ '稼働時間(分)' ] . append ( y3 )
75 rcd3 [ '無人稼働時間(分)' ] . append ( z3 )
76 rcd3 [ '停止時間(分)' ] . append ( u3 )
77 rcd3 [ 'トータル時間(分)' ] . append ( x3 )
78 rcd3 [ '稼働率(%)' ] . append ( w3 )
79
80 df3 = pd . DataFrame ( rcd3 )
81
82 rcd4 = { '日付' : [ ] , '稼働時間(分)' : [ ] , '無人稼働時間(分)' : [ ] , '停止時間(分)' : [ ] , 'トータル時間(分)' : [ ] , '稼働率(%)' : [ ] }
83
84 for file4 in file_list4 :
85 a4 = pd . read_csv ( file4 )
86 d4 = ( file4 [ 25 : 33 ] )
87 x4 = a4 . iloc [ : , 0 ] . count ( )
88 y4 = a4 . iloc [ : , 1 ] . sum ( )
89 z4 = a4 . iloc [ : , 2 ] . sum ( )
90 u4 = x4 - y4 - z4
91 w4 = round ( y4 / x4 * 100 , 1 )
92 rcd4 [ '日付' ] . append ( d4 )
93 rcd4 [ '稼働時間(分)' ] . append ( y4 )
94 rcd4 [ '無人稼働時間(分)' ] . append ( z4 )
95 rcd4 [ '停止時間(分)' ] . append ( u4 )
96 rcd4 [ 'トータル時間(分)' ] . append ( x4 )
97 rcd4 [ '稼働率(%)' ] . append ( w4 )
98
99 df4 = pd . DataFrame ( rcd4 )
100
101 rcd5 = { '日付' : [ ] , '稼働時間(分)' : [ ] , '無人稼働時間(分)' : [ ] , '停止時間(分)' : [ ] , 'トータル時間(分)' : [ ] , '稼働率(%)' : [ ] }
102
103 for file5 in file_list5 :
104 a5 = pd . read_csv ( file5 )
105 d5 = ( file5 [ 25 : 33 ] )
106 x5 = a5 . iloc [ : , 0 ] . count ( )
107 y5 = a5 . iloc [ : , 1 ] . sum ( )
108 z5 = a5 . iloc [ : , 2 ] . sum ( )
109 u5 = x5 - y5 - z5
110 w5 = round ( y5 / x5 * 100 , 1 )
111 rcd5 [ '日付' ] . append ( d5 )
112 rcd5 [ '稼働時間(分)' ] . append ( y5 )
113 rcd5 [ '無人稼働時間(分)' ] . append ( z5 )
114 rcd5 [ '停止時間(分)' ] . append ( u5 )
115 rcd5 [ 'トータル時間(分)' ] . append ( x5 )
116 rcd5 [ '稼働率(%)' ] . append ( w5 )
117 df5 = pd . DataFrame ( rcd5 )
118
119 rcd6 = { '日付' : [ ] , '稼働時間(分)' : [ ] , '無人稼働時間(分)' : [ ] , '停止時間(分)' : [ ] , 'トータル時間(分)' : [ ] , '稼働率(%)' : [ ] }
120
121 for file6 in file_list6 :
122 a6 = pd . read_csv ( file6 )
123 d6 = ( file6 [ 25 : 33 ] )
124 x6 = a6 . iloc [ : , 0 ] . count ( )
125 y6 = a6 . iloc [ : , 1 ] . sum ( )
126 z6 = a6 . iloc [ : , 2 ] . sum ( )
127 u6 = x6 - y6 - z6
128 w6 = round ( y6 / x6 * 100 , 1 )
129 rcd6 [ '日付' ] . append ( d6 )
130 rcd6 [ '稼働時間(分)' ] . append ( y6 )
131 rcd6 [ '無人稼働時間(分)' ] . append ( z6 )
132 rcd6 [ '停止時間(分)' ] . append ( u6 )
133 rcd6 [ 'トータル時間(分)' ] . append ( x6 )
134 rcd6 [ '稼働率(%)' ] . append ( w6 )
135
136 df6 = pd . DataFrame ( rcd6 )
137
138 rcd7 = { '日付' : [ ] , '稼働時間(分)' : [ ] , '無人稼働時間(分)' : [ ] , '停止時間(分)' : [ ] , 'トータル時間(分)' : [ ] , '稼働率(%)' : [ ] }
139
140 for file7 in file_list7 :
141 a7 = pd . read_csv ( file7 )
142 d7 = ( file7 [ 25 : 33 ] )
143 x7 = a7 . iloc [ : , 0 ] . count ( )
144 y7 = a7 . iloc [ : , 1 ] . sum ( )
145 z7 = a7 . iloc [ : , 2 ] . sum ( )
146 u7 = x7 - y7 - z7
147 w7 = round ( y7 / x7 * 100 , 1 )
148 rcd7 [ '日付' ] . append ( d7 )
149 rcd7 [ '稼働時間(分)' ] . append ( y7 )
150 rcd7 [ '無人稼働時間(分)' ] . append ( z7 )
151 rcd7 [ '停止時間(分)' ] . append ( u7 )
152 rcd7 [ 'トータル時間(分)' ] . append ( x7 )
153 rcd7 [ '稼働率(%)' ] . append ( w7 )
154
155 df7 = pd . DataFrame ( rcd7 )
156
157 try :
158 df_m1 = pd . read_excel ( '//192.168.3.225/Share\石岡工場/INV推進室\機械導入PJ/IOT/' + monthly_file , sheet_name = 0 , dtype = { '日付' : str } )
159 df_output1 = pd . merge ( df1 , df_m1 [ [ '日付' , 'コメント' ] ] , on = '日付' , how = 'outer' )
160 except FileNotFoundError :
161 df_output1 = df1 . copy ( )
162 df_output1 [ 'コメント' ] = None
163
164 try :
165 df_m2 = pd . read_excel ( '//192.168.3.225/Share\石岡工場/INV推進室\機械導入PJ/IOT/' + monthly_file , sheet_name = 1 , dtype = { '日付' : str } )
166 df_output2 = pd . merge ( df2 , df_m2 [ [ '日付' , 'コメント' ] ] , on = '日付' , how = 'outer' )
167 except FileNotFoundError :
168 df_output2 = df2 . copy ( )
169 df_output2 [ 'コメント' ] = None
170
171 try :
172 df_m3 = pd . read_excel ( '//192.168.3.225/Share\石岡工場/INV推進室\機械導入PJ/IOT/' + monthly_file , sheet_name = 2 , dtype = { '日付' : str } )
173 df_output3 = pd . merge ( df3 , df_m3 [ [ '日付' , 'コメント' ] ] , on = '日付' , how = 'outer' )
174 except FileNotFoundError :
175 df_output3 = df3 . copy ( )
176 df_output3 [ 'コメント' ] = None
177
178 try :
179 df_m4 = pd . read_excel ( '//192.168.3.225/Share\石岡工場/INV推進室\機械導入PJ/IOT/' + monthly_file , sheet_name = 3 , dtype = { '日付' : str } )
180 df_output4 = pd . merge ( df4 , df_m4 [ [ '日付' , 'コメント' ] ] , on = '日付' , how = 'outer' )
181 except FileNotFoundError :
182 df_output4 = df4 . copy ( )
183 df_output4 [ 'コメント' ] = None
184
185 try :
186 df_m5 = pd . read_excel ( monthly_file , sheet_name = 4 , dtype = { '日付' : str } )
187 df_output5 = pd . merge ( df5 , df_m5 [ [ '日付' , 'コメント' ] ] , on = '日付' , how = 'outer' )
188 except FileNotFoundError :
189 df_output5 = df5 . copy ( )
190 df_output5 [ 'コメント' ] = None
191
192 try :
193 df_m6 = pd . read_excel ( '//192.168.3.225/Share\石岡工場/INV推進室\機械導入PJ/IOT/' + monthly_file , sheet_name = 5 , dtype = { '日付' : str } )
194 df_output6 = pd . merge ( df6 , df_m6 [ [ '日付' , 'コメント' ] ] , on = '日付' , how = 'outer' )
195 except FileNotFoundError :
196 df_output6 = df6 . copy ( )
197 df_output6 [ 'コメント' ] = None
198
199 try :
200 df_m7 = pd . read_excel ( '//192.168.3.225/Share\石岡工場/INV推進室\機械導入PJ/IOT/' + monthly_file , sheet_name = 6 , dtype = { '日付' : str } )
201 df_output7 = pd . merge ( df7 , df_m7 [ [ '日付' , 'コメント' ] ] , on = '日付' , how = 'outer' )
202 except FileNotFoundError :
203 df_output7 = df7 . copy ( )
204 df_output7 [ 'コメント' ] = None
205
206
207 with pd . ExcelWriter ( '//192.168.3.225/Share\石岡工場/INV推進室\機械導入PJ/IOT/' + monthly_file ) as writer :
208 df_output1 . to_excel ( writer , sheet_name = 'D31' , index = False )
209 df_output2 . to_excel ( writer , sheet_name = 'D32' , index = False )
210 df_output3 . to_excel ( writer , sheet_name = 'D33' , index = False )
211 df_output4 . to_excel ( writer , sheet_name = 'D34' , index = False )
212 df_output5 . to_excel ( writer , sheet_name = 'D35' , index = False )
213 df_output6 . to_excel ( writer , sheet_name = 'D36' , index = False )
214 df_output7 . to_excel ( writer , sheet_name = 'D37' , index = False )