[英]Python openpyxl loop through excel files in folder
我有有效的代碼,但是要遍歷約35個.xlsx文件,讀取J列中的值(包括將單元格值與字典進行比較)和進行一些比較,將花費很多時間。
基本上,這是一個電子郵件通知系統,它是a)在J列某處的某個單元格中找到一個人的名字,然后在A列中檢查其偏移日期。如果A列中的日期是未來一天(明天),它將發送該人提醒電子郵件。
想知道是否有人願意提供一些反饋! 我感覺到多個因素和假設正在使它放慢速度,但經驗不足以知道如何改進它。
感謝您的輸入! 有時,即使是一點點提示也通常會給我足夠的信息,以便我自己制定解決方案。
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
我不知道這是否能幫上忙,它在類似的情況下對我有用,首先我將所有我擁有的文件(3600)附加到列表中,然后循環遍歷列表,運行速度更快。
祝好運
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.