繁体   English   中英

Python openpyxl遍历文件夹中的excel文件

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM