Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

openpyxl: no attribute "column_dimensions" #11706

Closed
jimmyppi opened this issue Apr 2, 2024 · 3 comments · Fixed by #11718
Closed

openpyxl: no attribute "column_dimensions" #11706

jimmyppi opened this issue Apr 2, 2024 · 3 comments · Fixed by #11718
Labels
priority: regression Something that worked before doesn't work anymore

Comments

@jimmyppi
Copy link

jimmyppi commented Apr 2, 2024

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
@srittau srittau added the priority: regression Something that worked before doesn't work anymore label Apr 2, 2024
@srittau
Copy link
Collaborator

srittau commented Apr 2, 2024

Cc @Avasam

@Avasam
Copy link
Collaborator

Avasam commented Apr 2, 2024

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 )

@jimmyppi
Copy link
Author

jimmyppi commented Apr 3, 2024

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
Labels
priority: regression Something that worked before doesn't work anymore
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants