Skip to content

openpyxl: no attribute "column_dimensions" #11706

Closed

Description

@jimmyppi

Got this error after today's release:

error: Item "WriteOnlyWorksheet" of "Union[Union[Worksheet, WriteOnlyWorksheet, ReadOnlyWorksheet], Chartsheet]" has no attribute "column_dimensions"  [union-attr]

error: Item "ReadOnlyWorksheet" of "Union[Union[Worksheet, WriteOnlyWorksheet, ReadOnlyWorksheet], Chartsheet]" has no attribute "column_dimensions"  [union-attr]

error: Item "Chartsheet" of "Union[Union[Worksheet, WriteOnlyWorksheet, ReadOnlyWorksheet], Chartsheet]" has no attribute "column_dimensions"  [union-attr]

Code example:

import openpyxl
op = openpyxl.open(filename)
sheet = op[worksheet_name] # sheet is a WorkSheet
sheet.column_dimensions

Activity

  1. srittau commented on Apr 2, 2024

    @srittau
    Collaborator
  2. Avasam commented on Apr 2, 2024

    @Avasam
    SponsorCollaborator

    The issue here is raised by not knowing statically whether your workbook is read_only, write_only, or "regular", and whether it contains Chartsheets or not. Only WorkSheet contains column_dimensions. Chartsheet, WriteOnlyWorksheet, ReadOnlyWorksheet all don't.

    Related to #9940 , where a generic workbook would help with knowing whether worksheets, and cells, are write_only or read_only, but that still wouldn't help with Chartsheet, maybe we could fudge around that one for __getitem__ as a common use-case.
    Another thing I need to validate, is whether a regular workbook could have read_only or write_only worksheets. It's "possible" code-wise, just not sure if it's an edge case that should/could happen with actual workbooks.

    With the last stub update, you can do:

    import openpyxl
    from openpyxl.worksheet.worksheet import Worksheet
    
    workbook = openpyxl.open(filename)
    sheet = workbook [worksheet_name]
    assert isinstance(sheet, Worksheet)  # sheet is a Worksheet
    sheet.column_dimensions

    or

    import openpyxl
    from openpyxl.worksheet.worksheet import Worksheet
    
    workbook = openpyxl.open(filename)
    sheet = workbook[worksheet_name]
    if isinstance(sheet, Worksheet):  # sheet is a Worksheet
        sheet.column_dimensions

    This is yet another case for python/typing#566

    That being said, with #9940 being an open issue with a potential better long-term solution (with the fudging of Chartsheet I have mentioned), I wouldn't mind changing the returns to Any (complex return type) short term. Or even back to Worksheet, whilst "incorrect and incomplete", getting Worksheet is the most common use case (and I'd just have to focus on completing #9940 :P )

  3. jimmyppi commented on Apr 3, 2024

    @jimmyppi
    Author

    Thanks @Avasam, I'll use one of the workarounds for now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority: regressionSomething that worked before doesn't work anymore

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions