[英]Python openpyxl loop through excel files in folder
I have working code, but it takes forever to loop through about 35 .xlsx files, read values in column J (including comparing cell values to a dictionary) and then do some comparisons. 我有有效的代码,但是要遍历约35个.xlsx文件,读取J列中的值(包括将单元格值与字典进行比较)和进行一些比较,将花费很多时间。
Basically, it's an email notification system that a) finds a person's name in a cell somewhere in column J, then examines its offset date in column A. If the date in column A is one day in the future (tomorrow) it sends that person a reminder email. 基本上,这是一个电子邮件通知系统,它是a)在J列某处的某个单元格中找到一个人的名字,然后在A列中检查其偏移日期。如果A列中的日期是未来一天(明天),它将发送该人提醒电子邮件。
Wondering if anyone would be willing to provide some feedback! 想知道是否有人愿意提供一些反馈! I have a sense that the multiple fors and if's are slowing it down, but not experienced enough to know how to improve it.
我感觉到多个因素和假设正在使它放慢速度,但经验不足以知道如何改进它。
Thanks for any input! 感谢您的输入! Sometimes even a little hint usually gives me enough info to work out a solution on my own.
有时,即使是一点点提示也通常会给我足够的信息,以便我自己制定解决方案。
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
I don't know if this can help, it works for me in a similar situation, first i red and appended all files i had (3600) to a List, then i loop them through the list, it ran faster. 我不知道这是否能帮上忙,它在类似的情况下对我有用,首先我将所有我拥有的文件(3600)附加到列表中,然后循环遍历列表,运行速度更快。
Good luck 祝好运
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.