“工作表”对象没有属性“ max_col”

时间:2019-05-29 04:03:53

标签: python-3.x openpyxl

我在其他项目中多次使用过max_col属性,但始终收到错误'Worksheet' object has no attribute 'max_col'

我特别困惑,因为我在其上方使用了max_row,没有错误。我检查了文档,并且max_col似乎仍然正确吗?

#!/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 个答案:

答案 0 :(得分:1)

该属性为max_column而不是max_colofficial documentation