简体   繁体   中英

Using Openpyxl to hide the columns

from openpyxl import Workbook
    
wb = Workbook()
sheet = wb.active
    
data = [
    ['Item', 'Colour'],
    ['pen', 'brown'],
    ['book', 'black'],
    ['plate', 'white'],
    ['chair', 'brown'],
    ['coin', 'gold'],
    ['bed', 'brown'],
    ['notebook', 'white'],
]
    
for r in data:
    sheet.append(r)
    
ws.column_dimensions.group('Item','Colour', hidden=True)
wb.save('filtered.xlsx')

When I try to hide columns for the Excel sheet, I got this error:

"Item is not a valid column name"

Is there any way to fix the error?

Try referring to the columns by their column letters ( A , B , etc.), like this:

sheet.column_dimensions.group('A', 'B', hidden=True)

(you were trying to call ws.column_dimensions.group() but it appears you meant sheet.column_dimensions.group() )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM