简体   繁体   English

如何使用openpyxl在for循环中读取Excel文件?

[英]How to read excel files in a for loop with openpyxl?

This seems tricky for me. 这对我来说似乎很棘手。 Let's say I have, nested in a directory tree, an excel file with a few non-empty columns. 假设我有一个嵌套在目录树中的excel文件,其中包含一些非空列。 I want to get the sum of all values located in column F with openpyxl : 我想使用openpyxl获取位于F列中的所有值的总和:

file1.xlsx
A  B  C  D  E  F
               5
               7
               11
               17
               20
               29
               34

My take on it would be as follows, but it is wrong: 我的看法如下,但这是错误的:

import os
from openpyxl import load_workbook

directoryPath=r'C:\Users\MyName\Desktop\MyFolder' #The main folder
os.chdir(directoryPath)
folder_list=os.listdir(directoryPath)
for folders, sub_folders, file in os.walk(directoryPath): #Traversing the sub folders
    for name in file:
        if name.endswith(".xlsx"):
            filename = os.path.join(folders, name)
            wb=load_workbook(filename, data_only=True)
            ws=wb.active
            cell_range = ws['F1':'F7'] #Selecting the slice of interest
            sumup=0
            for row in cell_range:
                sumup=sumup+cell.value

While running this I get NameError: name 'cell' is not defined . 运行此程序时,我得到NameError: name 'cell' is not defined How to work around this? 如何解决这个问题?

The main thing currently wrong is that you are only iterating through the rows, not the columns(cells) within that row. 当前最主要的错误是您仅遍历行,而不是遍历该行中的列(单元)。

At the end of your code, you can do this (Replace the two end lines of your code): 在代码末尾,您可以执行此操作(替换代码的两行结束):

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
        value = cell.value
        sumup += value

You identified that you didn't think this was running through each of your excel files. 您发现自己认为不是每个excel文件都在运行。 This would have been very easy to debug. 这本来很容易调试的。 Remove all code after 之后删除所有代码

ws=wb.active

And add 并添加

print(name + ' : ' + ws)

This would have printed out all of the excel file names, and their active sheet. 这样就可以打印出所有excel文件名及其活动工作表。 If it prints out more than 1, then it's obviously crawling through and grabbing the excel files... 如果输出的结果超过1,则显然是在抓取并抓取excel文件...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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