初学者7 发表于 2021-8-5 13:36:41

新学的一段代码,运用总有问题

这是我的部分代码
for file in files:
    file_path = path+file
    wb = load_workbook(file_path)
    ws = wb.active
    row_num = ws.max_row
    for row in ws.iter_cols(min_row=row_num-8, max_row=row_num,values_only=True):
      new_ws.append(row)


运行后出现这种反馈
Traceback (most recent call last):
File "c:/Users/Administrator/Desktop/编程文件/数据汇总.py", line 19, in <module>
    for row in ws.iter_cols(min_row=row_num-8, max_row=row_num,values_only=True):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\openpyxl\worksheet\worksheet.py", line 518, in _cells_by_col
    yield tuple(cell.value for cell in cells)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\openpyxl\worksheet\worksheet.py", line 518, in <genexpr>
    yield tuple(cell.value for cell in cells)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\openpyxl\worksheet\worksheet.py", line 515, in <genexpr>
    cells = (self.cell(row=row, column=column)
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\openpyxl\worksheet\worksheet.py", line 238, in cell
    raise ValueError("Row or column values must be at least 1")
ValueError: Row or column values must be at least 1

求大佬指点一下{:5_92:}

大马强 发表于 2021-8-5 13:39:16

把代码全发出来下吧

初学者7 发表于 2021-8-5 13:40:06

大马强 发表于 2021-8-5 13:39
把代码全发出来下吧

import os
from openpyxl import load_workbook
from openpyxl import Workbook

# 设置文件夹路径
path = 'C:/Users/Administrator/Desktop/数据汇总/'
# 获取文件夹内所有文件名和文件夹名
files = os.listdir(path)

new_wb = Workbook()
new_ws = new_wb.active

# 循环打开文件夹下所有工作簿和其工作表
for file in files:
    file_path = path+file
    wb = load_workbook(file_path)
    ws = wb.active
    row_num = ws.max_row
    for row in ws.iter_cols(min_row=row_num-8, max_row=row_num,values_only=True):
      new_ws.append(row)

new_wb.save('C:/Users/Administrator/Desktop/数据汇总/L7113数据汇总.xlsx')

大马强 发表于 2021-8-5 13:48:19

抱歉,知识盲区,等大佬来看吧
但我刚刚看查了一下,你没有设置列

大马强 发表于 2021-8-5 13:48:57

for row in ws.iter_cols(min_row=row_num-8, max_row=row_num,values_only=True):
这里你只是设置了行,没有列
页: [1]
查看完整版本: 新学的一段代码,运用总有问题