简体   繁体   English

使用 Openpyxl 隐藏列

[英]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:当我尝试隐藏 Excel 工作表的列时,出现此错误:

"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:尝试通过列字母( AB等)引用列,如下所示:

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() ) (您试图调用ws.column_dimensions.group()但看来您的意思是sheet.column_dimensions.group()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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