Home > Software engineering >  AttributeError: 'workbook' object has no attribute 'max_row'
AttributeError: 'workbook' object has no attribute 'max_row'

Time:03-02

I already referred this post here but it has no response.

Am using a public github package here to copy all the formatting from one excel file to another excel file.

By formatting, I mean the color, font, freeze panes etc.

style_sheet = load_workbook(filename = 'sty1.xlsx')
data_sheet = load_workbook(filename = 'dat1.xlsx')
copy_styles(style_sheet, data_sheet) # the error happens in this line 

The error producing line within copy_styles function is given below

def copy_styles(style_sheet, data_sheet):
    max_matched_row = min(style_sheet.max_row, data_sheet.max_row)
    max_matched_col = min(style_sheet.max_column, data_sheet.max_column)

The full copy_styles function can be found in this github link here

The error that I encounter is given below

AttributeError: 'workbook' object has no attribute 'max_row'

If you want a sample file to test, it can be found in this github issue here

CodePudding user response:

Assuming you want to copy the style of the first sheet in the workbook, you should do:

copy_styles(style_sheet.sheet_by_index(0), data_sheet.sheet_by_index(0))

If you want to copy the style of all worksheets (assuming that they match), just loop over them:

style_wb = load_workbook(filename = 'sty1.xlsx')
data_wb = load_workbook(filename = 'dat1.xlsx')

for sheet_from, sheet_to in zip(style_wb.sheets(), data_wb.sheets()):
    copy_styles(sheet_from, sheet_to)

I changed the variable names on the second example to make it clear that they are workbooks, not sheets.

  • Related