回答編集履歴
2
あ
test
CHANGED
@@ -8,47 +8,23 @@
|
|
8
8
|
|
9
9
|
```
|
10
10
|
|
11
|
-
|
11
|
+
日報
|
12
12
|
|
13
|
-
|--
|
13
|
+
|-- 日報6.17.xlsx
|
14
14
|
|
15
|
-
|--
|
15
|
+
|-- 日報6.18.xlsx
|
16
16
|
|
17
|
-
`--
|
17
|
+
`-- 日報6.19.xlsx
|
18
18
|
|
19
19
|
```
|
20
20
|
|
21
21
|
|
22
22
|
|
23
|
-
`
|
23
|
+
`日報` というディレクトリ以下に以下の xlsx ファイルがあり、B14 ~ L28、B34 ~ L48 にデータが入っています。
|
24
24
|
|
25
25
|
|
26
26
|
|
27
|
-
|
27
|
+
![イメージ説明](13d43e15597c7d04fb206c450032d299.png)
|
28
|
-
|
29
|
-
DATA
|
30
|
-
|
31
|
-
1
|
32
|
-
|
33
|
-
2
|
34
|
-
|
35
|
-
3
|
36
|
-
|
37
|
-
4
|
38
|
-
|
39
|
-
5
|
40
|
-
|
41
|
-
6
|
42
|
-
|
43
|
-
7
|
44
|
-
|
45
|
-
8
|
46
|
-
|
47
|
-
9
|
48
|
-
|
49
|
-
10
|
50
|
-
|
51
|
-
```
|
52
28
|
|
53
29
|
|
54
30
|
|
@@ -74,9 +50,15 @@
|
|
74
50
|
|
75
51
|
import glob
|
76
52
|
|
53
|
+
import locale
|
54
|
+
|
77
55
|
import os
|
78
56
|
|
79
57
|
import re
|
58
|
+
|
59
|
+
import time
|
60
|
+
|
61
|
+
from datetime import datetime
|
80
62
|
|
81
63
|
|
82
64
|
|
@@ -84,25 +66,17 @@
|
|
84
66
|
|
85
67
|
|
86
68
|
|
69
|
+
locale.setlocale(locale.LC_ALL, '')
|
70
|
+
|
71
|
+
|
72
|
+
|
87
73
|
# エクセルファイル一覧を取得する。
|
88
74
|
|
89
|
-
xlsx_files = os.path.join('
|
75
|
+
xlsx_files = os.path.join('日報', '*.xlsx')
|
90
76
|
|
91
77
|
xlsx_paths = [path for path in sorted(glob.glob(xlsx_files))]
|
92
78
|
|
93
|
-
print(xlsx_paths)
|
94
79
|
|
95
|
-
# ['reports/report_6.17.xlsx', 'reports/report_6.18.xlsx', 'reports/report_6.19.xlsx']
|
96
|
-
|
97
|
-
```
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
## 各シートからデータを読み込み、出力用のワークブックに値をコピーする。
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
```python
|
106
80
|
|
107
81
|
out_wb = xl.Workbook() # 新しい workbook を作成する。
|
108
82
|
|
@@ -110,7 +84,35 @@
|
|
110
84
|
|
111
85
|
|
112
86
|
|
87
|
+
row_offset = 1
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
# ヘッダー
|
92
|
+
|
93
|
+
headers = ['タイトル', '更新日時', 'B', 'C', 'D', 'E', 'F',
|
94
|
+
|
95
|
+
'G', 'H', 'I', 'J', 'K', 'L']
|
96
|
+
|
113
|
-
for
|
97
|
+
for c, label in enumerate(headers, 1):
|
98
|
+
|
99
|
+
out_ws.cell(row=row_offset, column=c).value = label
|
100
|
+
|
101
|
+
row_offset += 1
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
for path in xlsx_paths:
|
106
|
+
|
107
|
+
print('reading... ', path)
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
# 更新日時を取得
|
112
|
+
|
113
|
+
datetime = datetime.fromtimestamp(os.path.getmtime(path))
|
114
|
+
|
115
|
+
modified = '{0:%Y年%m月%d日 %H時%M分}'.format(datetime)
|
114
116
|
|
115
117
|
# Excel ファイルを読み込む。
|
116
118
|
|
@@ -120,29 +122,43 @@
|
|
120
122
|
|
121
123
|
# ファイル名から日付部分を取得
|
122
124
|
|
123
|
-
matches = re.search(r'
|
125
|
+
matches = re.search(r'日報(\d{1,2}).(\d{1,2})', os.path.basename(path))
|
124
126
|
|
125
127
|
month, date = matches.groups()
|
126
128
|
|
127
129
|
title = '{}/{}'.format(month, date)
|
128
130
|
|
129
|
-
print(title)
|
130
131
|
|
131
|
-
# A2 ~ A11 の値を取得
|
132
132
|
|
133
|
-
|
133
|
+
# 値をコピー B14:L28
|
134
134
|
|
135
|
+
out_ws.cell(row=row_offset, column=1).value = title # タイトル
|
136
|
+
|
137
|
+
out_ws.cell(row=row_offset, column=2).value = modified # 修正日時
|
138
|
+
|
139
|
+
for r, rows in enumerate(ws['B14:L28']): # B14 ~ L28 の値
|
140
|
+
|
141
|
+
for c, cell in enumerate(rows):
|
142
|
+
|
143
|
+
out_ws.cell(row=row_offset + r, column=3 + c).value = cell.value
|
144
|
+
|
135
|
-
|
145
|
+
row_offset += 15
|
136
146
|
|
137
147
|
|
138
148
|
|
139
|
-
# 値をコピー
|
149
|
+
# 値をコピー B34:L48
|
140
150
|
|
141
|
-
out_ws.cell(row=row, column=1).value = title # タイトル
|
151
|
+
out_ws.cell(row=row_offset, column=1).value = title # タイトル
|
142
152
|
|
143
|
-
|
153
|
+
out_ws.cell(row=row_offset, column=2).value = modified # 修正日時
|
144
154
|
|
155
|
+
for r, row in enumerate(ws['B34:L48']): # B34 ~ L48 の値
|
156
|
+
|
157
|
+
for c, cell in enumerate(row):
|
158
|
+
|
145
|
-
out_ws.cell(row=row, column=
|
159
|
+
out_ws.cell(row=row_offset + r, column=3 + c).value = cell.value
|
160
|
+
|
161
|
+
row_offset += 15
|
146
162
|
|
147
163
|
|
148
164
|
|
@@ -154,12 +170,4 @@
|
|
154
170
|
|
155
171
|
|
156
172
|
|
157
|
-
```output
|
158
|
-
|
159
|
-
6
|
173
|
+
![イメージ説明](4f0b2b5feedbf54d42ea7b693568e2b0.png)
|
160
|
-
|
161
|
-
6/18 1 2 3 4 5 6 7 8 9 10
|
162
|
-
|
163
|
-
6/19 1 2 3 4 5 6 7 8 9 10
|
164
|
-
|
165
|
-
```
|
1
a
test
CHANGED
@@ -6,35 +6,47 @@
|
|
6
6
|
|
7
7
|
|
8
8
|
|
9
|
+
```
|
10
|
+
|
11
|
+
reports
|
12
|
+
|
9
|
-
|
13
|
+
|-- report_6.17.xlsx
|
14
|
+
|
15
|
+
|-- report_6.18.xlsx
|
16
|
+
|
17
|
+
`-- report_6.19.xlsx
|
18
|
+
|
19
|
+
```
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
`reports` というディレクトリ以下に以下の xlsx ファイルがあり、A列には下記のデータが1~11行に格納されています。
|
10
24
|
|
11
25
|
|
12
26
|
|
13
27
|
```excel
|
14
28
|
|
15
|
-
|
29
|
+
DATA
|
16
30
|
|
17
|
-
1
|
31
|
+
1
|
18
32
|
|
19
|
-
2
|
33
|
+
2
|
20
34
|
|
21
|
-
3
|
35
|
+
3
|
22
36
|
|
23
|
-
4
|
37
|
+
4
|
24
38
|
|
25
|
-
5
|
39
|
+
5
|
26
40
|
|
27
|
-
6
|
41
|
+
6
|
28
42
|
|
29
|
-
7
|
43
|
+
7
|
30
44
|
|
31
|
-
8
|
45
|
+
8
|
32
46
|
|
33
|
-
9
|
47
|
+
9
|
34
48
|
|
35
|
-
10
|
49
|
+
10
|
36
|
-
|
37
|
-
|
38
50
|
|
39
51
|
```
|
40
52
|
|
@@ -44,52 +56,110 @@
|
|
44
56
|
|
45
57
|
|
46
58
|
|
59
|
+
ファイル一覧を取得する。
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
1. glob.glob() でファイル一覧を取得する。
|
64
|
+
|
65
|
+
2. sorted() でソートする。
|
66
|
+
|
67
|
+
※ 06月12日のように2桁で揃えられていない場合、辞書順ソートの場合、6月12日より12月1日のほうが先にきます。
|
68
|
+
|
69
|
+
※ 自然順ソートをしたい場合は、[こちらの回答](https://teratail.com/questions/149343) を見てください。
|
70
|
+
|
71
|
+
|
72
|
+
|
47
73
|
```python
|
74
|
+
|
75
|
+
import glob
|
76
|
+
|
77
|
+
import os
|
78
|
+
|
79
|
+
import re
|
80
|
+
|
81
|
+
|
48
82
|
|
49
83
|
import openpyxl as xl
|
50
84
|
|
51
85
|
|
52
86
|
|
87
|
+
# エクセルファイル一覧を取得する。
|
88
|
+
|
53
|
-
xlsx_path
|
89
|
+
xlsx_files = os.path.join('reports', '*.xlsx')
|
90
|
+
|
91
|
+
xlsx_paths = [path for path in sorted(glob.glob(xlsx_files))]
|
92
|
+
|
93
|
+
print(xlsx_paths)
|
94
|
+
|
95
|
+
# ['reports/report_6.17.xlsx', 'reports/report_6.18.xlsx', 'reports/report_6.19.xlsx']
|
96
|
+
|
97
|
+
```
|
54
98
|
|
55
99
|
|
56
100
|
|
57
|
-
# Excel ファイルを読み込む。
|
58
|
-
|
59
|
-
|
101
|
+
## 各シートからデータを読み込み、出力用のワークブックに値をコピーする。
|
60
|
-
|
61
|
-
ws = wb.active
|
62
102
|
|
63
103
|
|
64
104
|
|
65
|
-
|
105
|
+
```python
|
66
106
|
|
67
|
-
|
107
|
+
out_wb = xl.Workbook() # 新しい workbook を作成する。
|
68
108
|
|
69
|
-
|
109
|
+
out_ws = out_wb.active
|
70
110
|
|
71
|
-
|
72
111
|
|
73
|
-
# ヘッダーをコピー
|
74
112
|
|
75
|
-
|
113
|
+
for row, path in enumerate(xlsx_paths, 1):
|
76
114
|
|
77
|
-
|
115
|
+
# Excel ファイルを読み込む。
|
116
|
+
|
117
|
+
wb = xl.load_workbook(path, data_only=True)
|
118
|
+
|
119
|
+
ws = wb.active
|
120
|
+
|
121
|
+
# ファイル名から日付部分を取得
|
122
|
+
|
123
|
+
matches = re.search(r'report_(\d{1,2}).(\d{1,2})', os.path.basename(path))
|
124
|
+
|
125
|
+
month, date = matches.groups()
|
126
|
+
|
127
|
+
title = '{}/{}'.format(month, date)
|
128
|
+
|
129
|
+
print(title)
|
130
|
+
|
131
|
+
# A2 ~ A11 の値を取得
|
132
|
+
|
133
|
+
data = [ws.cell(row=row, column=1).value for row in range(2, 12)]
|
134
|
+
|
135
|
+
print(data)
|
136
|
+
|
137
|
+
|
78
138
|
|
79
139
|
# 値をコピー
|
80
140
|
|
81
|
-
|
141
|
+
out_ws.cell(row=row, column=1).value = title # タイトル
|
82
142
|
|
143
|
+
for col, val in enumerate(data, 1): # 値をB列以降にコピー
|
144
|
+
|
83
|
-
|
145
|
+
out_ws.cell(row=row, column=col + 1).value = val
|
84
146
|
|
85
147
|
|
86
148
|
|
87
|
-
|
149
|
+
# 保存する。
|
88
150
|
|
89
|
-
|
151
|
+
out_wb.save('output.xlsx')
|
90
|
-
|
91
|
-
new_wb.save(xlsx_path)
|
92
|
-
|
93
|
-
print('saving...', xlsx_path)
|
94
152
|
|
95
153
|
```
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
```output
|
158
|
+
|
159
|
+
6/17 1 2 3 4 5 6 7 8 9 10
|
160
|
+
|
161
|
+
6/18 1 2 3 4 5 6 7 8 9 10
|
162
|
+
|
163
|
+
6/19 1 2 3 4 5 6 7 8 9 10
|
164
|
+
|
165
|
+
```
|