简体   繁体   English

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

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

相关问题 对于循环 Python OpenPyXL 在 Excel - For loop Python OpenPyXL in Excel 如何使用openpyxl在for循环中读取Excel文件? - How to read excel files in a for loop with openpyxl? 我使用 python openpyxl 在 excel 中使用 for 循环 - I use python openpyxl for working with for loop in excel Python使用Openpyxl遍历Excel行,根据事务ID更新单元格 - Python loop through Excel rows using Openpyxl, updating cells based on a transaction ID Python Pandas - 循环浏览 Excel 文件的文件夹,将每个 ZC1D81AF583580.x 文件的数据导出到它们自己的文件“DED8FDCZ”中 - Python Pandas - loop through folder of Excel files, export data from each Excel file's sheet into their own .xlsx file openpyxl:用python编写大型excel文件 - openpyxl: writing large excel files with python Python Openpyxl - Append 许多 excel 文件合并为 1 个文件 - Python Openpyxl - Append many excel files into 1 file python 中的 Openpyxl 进行循环以将许多 excel 文件中的特定行合并为一个 - Openpyxl in python making for loop to merge specific row from many excel files into one Python Pandas - 遍历文件夹 of.xlsx 文件,仅使用正则表达式从 Excel 选项卡中添加数据,名称中带有 xx.xx - Python Pandas - loop through folder of .xlsx files, only add data from Excel tabs with xx.xx in the name using regex 使用 Openpyxl 打开文件夹中的文件 - Opening files in a folder with Openpyxl
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM