jupyter notebook
エクセルシートを python から読み書きする例。
名簿ファイルを読み取り、辞書オブジェクトに保存する。
---------------------------------------------------------------------------------------------------
import openpyxl as px # excel操作用ライブラリ
userListFile = '名簿.xlsx' # ユーザ名簿ファイル
today = "20170411" # 毎回指定する。デフォルト値はスクリプトを実行する日
wb = px.load_workbook(userListFile)
ws = wb.active
nameList = {} # 辞書オブジェクトの作成
start_row = 3
for line in range(start_row, ws.max_row+1): # 最大値を含む場合は +1 が必要
id = ws.cell(row=line, column=1).value
#print(id)
user = ws.cell(row=line, column=2).value
#print(user)
nameList[id] = user
fileName = today + "_" + id + "_" + user + ".xlsx"
print(fileName)
----------------------------------------------------------------------------------------------------
また、カレントディレクトリのファイルリストを取得するサンプルは以下の通り。
-------------------------------------------------------------------------------------------------------
import os # ファイル・ディレクトリ操作
targetDir = "./" # 対象となるディレクトリのパスを指定
numX = 28
fileList = os.listdir(targetDir) # カレントディレクトリのファイル一覧
if 5 != len(fileList):
print ('ファイル数が違います。')
else:
print('ファイル数は正しいです。')
-------------------------------------------------------------------------------------------------------