Python openpyxl loop through excel files in folder
<p>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. </p>

<p>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.</p>

<p>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.</p>

<p>Thanks for any input!  Sometimes even a little hint usually gives me enough info to work out a solution on my own.</p>

<pre><code>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
</code></pre>

<p>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.</p>

<p>Good luck</p>

python excel openpyxl smtplib