[英]How to read excel files in a for loop with openpyxl?
这对我来说似乎很棘手。 假设我有一个嵌套在目录树中的excel文件,其中包含一些非空列。 我想使用openpyxl
获取位于F列中的所有值的总和:
file1.xlsx
A B C D E F
5
7
11
17
20
29
34
我的看法如下,但这是错误的:
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
运行此程序时,我得到NameError: name 'cell' is not defined
。 如何解决这个问题?
当前最主要的错误是您仅遍历行,而不是遍历该行中的列(单元)。
在代码末尾,您可以执行此操作(替换代码的两行结束):
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
您发现自己认为不是每个excel文件都在运行。 这本来很容易调试的。 之后删除所有代码
ws=wb.active
并添加
print(name + ' : ' + ws)
这样就可以打印出所有excel文件名及其活动工作表。 如果输出的结果超过1,则显然是在抓取并抓取excel文件...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.