Python(openpyxl)で、Excelの最大値・最小値を取得するコードをご紹介します。
max関数・min関数を使用して、指定列または複数列から最大値と最小値を求めます。
ぜひ、お試しください。
最大値・最小値を取得(指定列)
以下のコードを実行すると、特定列から最大値・最小値を取得します。
import openpyxl
wb = openpyxl.load_workbook('C:/Users/xxx/Desktop/test/test.xlsx')
ws = wb['Sheet1']
#シートの最終行を取得
maxRow = ws.max_row
#配列宣言
My_Value = []
#行ループ
for i in range(2,maxRow):
#B列を配列へ格納
My_Value.append(ws.cell(i,2).value)
print(max(My_Value))
print(min(My_Value))
コード実行後
エクセルシート内の指定列の中から、最大値と最小値を求めます。
最大値・最小値を取得(複数列)
以下のコードを実行すると、複数の列から最大値・最小値を取得します。
import openpyxl
wb = openpyxl.load_workbook('C:/Users/xxx/Desktop/test/test.xlsx')
ws = wb['Sheet1']
#シートの最終行を取得
maxRow = ws.max_row + 1
#シートの最大列を取得
maxClm = ws.max_column + 1
#配列宣言
My_Value = []
#列ループ
for j in range(2,maxClm):
#行ループ
for i in range(2,maxRow):
#値を配列へ格納
My_Value.append(ws.cell(i,j).value)
print(max(My_Value))
print(min(My_Value))
コード実行後
エクセルシート内の複数列の中から、最大値と最小値を求めます。
この記事がお役に立ちますと幸いです。

【Python】エクセル処理を自動化:超便利28選
...