'Worksheet' object has no attribute 'max_col'

瘦欲@ 提交于 2021-01-29 05:46:32

问题


I have used the max_col attribute numerous times in other projects, but keep getting the error 'Worksheet' object has no attribute 'max_col'

I'm especially confused because I use max_row right above it, with no error. I checked the documentation, and max_col still seems to be correct?

#!/usr/bin/python

# excelToCSV.py - Converts all excel files in a directory to CSV, one file
# per sheet

import openpyxl
import csv
import os

for excelFile in os.listdir('.'):
    #Skip non-xlsx files, load the workbook object.
    if excelFile.endswith('.xlsx'):
        wbA = openpyxl.load_workbook(excelFile)
        #Loop through each sheet in the workbook
        for sheet in wbA.worksheets:    #Note: changing wb to wb.worksheets
            sheetName = sheet.title
            sheetA = wbA.get_sheet_by_name(sheetName)
            # Create the CSV filename from the excel filename and sheet title
            excelFileStripped = excelFile.strip('.xlsx')
            csvFilename = excelFileStripped + '_' + sheetName + '.csv'
            # Create the csv.writer object for this csv file
            csvFile = open(csvFilename, 'w', newline='')
            csvWriter = csv.writer(csvFile)
            # Loop through every row in the sheet
            maxRow = sheetA.max_row
            maxCol = sheetA.max_col

回答1:


The attribute is max_column not max_col (official documentation)



来源:https://stackoverflow.com/questions/56352949/worksheet-object-has-no-attribute-max-col

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!