私は作業中のコードを持っていますが、約35の.xlsxファイルをループして、J列の値を読み込み(セル値を辞書と比較するなど)、比較します。Python openpyxl loop through〜フォルダ内のexcelファイル
基本的には、a)列Jのどこかのセルで人の名前を見つけて、列Aのオフセット日付を調べる電子メール通知システムです。列Aの日付が将来(明日)その人にリマインダーメールを送信します。
誰かが何らかのフィードバックを提供してくれると思いますか?私は複数のforsとifがそれを遅くしているが、それを改善する方法を知るのに十分な経験がないという感覚を持っています。
ありがとうございました!時には少しのヒントでさえ、私自身で解決策を見つけ出すのに十分な情報を得ることができます。
try:
for i in os.listdir(os.chdir(thisdir)):
if i.endswith(".xlsx"):
workbook = load_workbook(i, data_only=True)
try:
ws = workbook[wsvar]
cell_range = ws['j3':'j110']
for row in cell_range: # This is iterating through rows 1-7
for cell in row: # This iterates through the columns(cells) in that row
if cell.value:
if cell.offset(row=0, column =-9).value.date() == (datetime.now().date() + timedelta(days=1)):
for name, email in sublist.items():
#send the emails
if cell.value == name:
email = sublist[cell.value]
datconv = str(cell.offset(row=0, column=-9).value.date().strftime("%m/%d/%Y"))
program = cell.offset(row=0, column=-7).value
#if there are hours in the "hours worked column, use those"
if cell.offset(row=0, column=-5).value:
hours = cell.offset(row=0, column=-5).value
#else, pick up the scheudled hours
else:
hours = cell.offset(row=0, column=-6).value
#SMTP code for email goes here, but it doesn't seem to be the culprit
ネスティングのレベル(4つのループ!)は、リファクタリングする必要があることを示唆しています。これは、ホットスポットの特定に役立ちます。 107行ではなく7行をループしています。 –
@CharlieClarkはい、私は同意します。私はちょうどそれが必要 "ああ - ハ!"瞬間だが、私はa)pythonとb)openpyxlで十分な経験がない。そして、はい、私は大きな範囲をループし始めたときに私のコメントを更新するのを忘れました。 –
これをメーリングリストに移すことをお勧めします。リファクタリングを手助けするのは簡単でしょう。本当に問題にならないセルの数については、重複したコールがたくさんあります。 –