Pythonでエクセルを操作・月ごとの値を足して出力
- hiro2studio
- 2021年2月1日
- 読了時間: 1分
日付ごとのデータがある場合に月ごとの値にまとめたい時にあったら便利かも?
入力エクセルの値は「=rand()*10」です
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import openpyxl as ex
#-------------------------------------------------
## main ###
#-------------------------------------------------
if __name__=='__main__':
file_name = 'datas.xlsx'
# 数式でなく値の取得
excelBook = ex.load_workbook(file_name, data_only=True)
ws = excelBook.worksheets[0]
# 12ヶ月
month_add = [0]*12
for row in ws.rows:
addrs = []
value = []
cnt = 0
for cell in row:
# 日付データの取得
if cnt == 0:
print(cell.value)
edata = cell.value
# 同じ月の値は足し込む
else:
print('{:.0f}'.format(cell.value))
month_add[edata.month-1] = month_add[edata.month-1] + cell.value
cnt = cnt + 1
for i in range(12-1):
print(month_add[i])

出力
22.825126110408867
30.516247629691772
17.34606219207651
10.562336500512629
31.800520270381575
28.390228413590236
16.973419788456823
16.390570436497235
9.680111903055767
5.823377786054138
9.39729333362195
Comments