You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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:
importopenpyxlfromopenpyxl.worksheet.worksheetimportWorksheetworkbook=openpyxl.open(filename)
sheet=workbook [worksheet_name]
assertisinstance(sheet, Worksheet) # sheet is a Worksheetsheet.column_dimensions
or
importopenpyxlfromopenpyxl.worksheet.worksheetimportWorksheetworkbook=openpyxl.open(filename)
sheet=workbook[worksheet_name]
ifisinstance(sheet, Worksheet): # sheet is a Worksheetsheet.column_dimensions
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 )
Activity
srittau commentedon Apr 2, 2024
Cc @Avasam
Avasam commentedon 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. OnlyWorkSheetcontainscolumn_dimensions.Chartsheet,WriteOnlyWorksheet,ReadOnlyWorksheetall 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:
or
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
ChartsheetI have mentioned), I wouldn't mind changing the returns toAny(complex return type) short term. Or even back toWorksheet, whilst "incorrect and incomplete", gettingWorksheetis the most common use case (and I'd just have to focus on completing #9940 :P )jimmyppi commentedon Apr 3, 2024
Thanks @Avasam, I'll use one of the workarounds for now