AttributeError:“工作簿”对象没有属性“ iter_cols”

时间:2019-10-30 16:33:56

标签: python openpyxl

我是openpyxl的新手,他编写了脚本来遍历某些列并删除多个名称下的列。每次我尝试使用wb.iter_cols时都会说:

workbook object has no attribute 'iter_cols'

我检查了openpyxl版本(3.0),并尝试向iter_cols添加参数,但无济于事。

import openpyxl
from openpyxl import Workbook, load_workbook

wb = openpyxl.load_workbook('test.xlsx')
wb.iter_cols()
DelCols()

def DelCols():
    for col in wb.iter_cols():
        if col == "StartDate" or col == "EndDate" or col == "Status" or col == "IPAddress"\
        or col == "Progress" or col == "Duration (in seconds)" or col == "Finished"\
        or col == "RecordedDate" or "ResponseId" or col == "RecipientLastName"\
        or col == "RecipientFirstName" or col == "RecipientEmail" or col == "ExternalReference"\
        or col == "LocationLatitude" or col == "LocationLongitude" or col == "DistributionChannel":
            wb.delete_cols(col)

1 个答案:

答案 0 :(得分:0)

如果其他人遇到与我相同的问题,则不要进行此操作。

我必须将代码更改为:

wb = openpyxl.load_workbook(DocFileLocation)
ws = wb.active

def DelCols():
    for col in ws.iter_cols():
        if col == "StartDate" or col == "EndDate" or col == "Status" or col == "IPAddress"\
        or col == "Progress" or col == "Duration (in seconds)" or col == "Finished"\
        or col == "RecordedDate" or "ResponseId" or col == "RecipientLastName"\
        or col == "RecipientFirstName" or col == "RecipientEmail" or col == "ExternalReference"\
        or col == "LocationLatitude" or col == "LocationLongitude" or col == "DistributionChannel":
            ws.delete_cols(col)