回答編集履歴
1
実際にopenpyxlを落として、実行して結果を出してみました。
test
CHANGED
@@ -93,3 +93,87 @@
|
|
93
93
|
# wb.save(excelname)
|
94
94
|
|
95
95
|
```
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
### 追記(やってみた)
|
100
|
+
|
101
|
+
ご質問はfor文をまとめる方法で合っていますか?
|
102
|
+
|
103
|
+
これだと成功しました。
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
```python3
|
108
|
+
|
109
|
+
import openpyxl
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
if __name__ == '__main__':
|
114
|
+
|
115
|
+
sheet_name1 = "Sheet1"
|
116
|
+
|
117
|
+
sheet_name2 = "Sheet2"
|
118
|
+
|
119
|
+
sheet_name3 = "Sheet3"
|
120
|
+
|
121
|
+
sheet_name4 = "Sheet4"
|
122
|
+
|
123
|
+
wb = openpyxl.load_workbook("Sample.xlsx")
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
ss1 = [sheet_name1, sheet_name4]
|
128
|
+
|
129
|
+
ss2 = [sheet_name2, sheet_name3]
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
hiddenlist1 = ['F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U']
|
134
|
+
|
135
|
+
hiddenlist2 = ['G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U']
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
for ws in wb.sheetnames:
|
140
|
+
|
141
|
+
if ws in ss1:
|
142
|
+
|
143
|
+
print(f"ss1: {ws}")
|
144
|
+
|
145
|
+
# for col in hiddenlist1:
|
146
|
+
|
147
|
+
# ws.column_dimensions[col].hidden = True
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
elif ws in ss2:
|
152
|
+
|
153
|
+
print(f"ss2 {ws}")
|
154
|
+
|
155
|
+
# for col in hiddenlist2:
|
156
|
+
|
157
|
+
# ws.column_dimensions[col].hidden = True
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
# wb.save(excelname)
|
162
|
+
|
163
|
+
```
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
### 実行結果
|
168
|
+
|
169
|
+
```shell
|
170
|
+
|
171
|
+
ss1: Sheet1
|
172
|
+
|
173
|
+
ss2 Sheet2
|
174
|
+
|
175
|
+
ss2 Sheet3
|
176
|
+
|
177
|
+
ss1: Sheet4
|
178
|
+
|
179
|
+
```
|