如何在Openpyxl中隐藏列?

时间:2016-07-22 13:29:01

标签: python openpyxl

我在Excel工作表中隐藏了一堆列。我收到此错误:AttributeError: can't set attribute来自此行worksheet.column_dimensions['B'].visible = False

很抱歉,这是一个非常简单的问题。我刚刚更新到Openpyxl / Pandas的新版本,所以我现在必须通过我的代码并进行更改以适应新版本的文档。

    worksheet.column_dimensions['B'].visible = False
    worksheet.column_dimensions['D'].visible = False
    worksheet.column_dimensions['E'].visible = False
    worksheet.column_dimensions['F'].visible = False
    worksheet.column_dimensions['G'].visible = False
    worksheet.column_dimensions['H'].visible = False
    worksheet.column_dimensions['I'].visible = False
    worksheet.column_dimensions['K'].visible = False
    worksheet.column_dimensions['L'].visible = False
    worksheet.column_dimensions['M'].visible = False
    worksheet.column_dimensions['N'].visible = False
    worksheet.column_dimensions['O'].visible = False
    worksheet.column_dimensions['P'].visible = False
    worksheet.column_dimensions['Q'].visible = False
    worksheet.column_dimensions['R'].visible = False
    worksheet.column_dimensions['S'].visible = False
    worksheet.column_dimensions['T'].visible = False
    worksheet.column_dimensions['U'].visible = False
    worksheet.column_dimensions['V'].visible = False
    worksheet.column_dimensions['W'].visible = False
    worksheet.column_dimensions['X'].visible = False
    worksheet.column_dimensions['Y'].visible = False
    worksheet.column_dimensions['Z'].visible = False
    worksheet.column_dimensions['AA'].visible = False
    worksheet.column_dimensions['AB'].visible = False
    worksheet.column_dimensions['AC'].visible = False
    worksheet.column_dimensions['AD'].visible = False
    worksheet.column_dimensions['AE'].visible = False
    worksheet.column_dimensions['AF'].visible = False
    worksheet.column_dimensions['AG'].visible = False
    worksheet.column_dimensions['AH'].visible = False
    worksheet.column_dimensions['AI'].visible = False
    worksheet.column_dimensions['AJ'].visible = False
    worksheet.column_dimensions['AK'].visible = False
    worksheet.column_dimensions['AM'].visible = False
    worksheet.column_dimensions['AN'].visible = False
    worksheet.column_dimensions['AP'].visible = False
    worksheet.column_dimensions['AQ'].visible = False
    worksheet.column_dimensions['AR'].visible = False
    worksheet.column_dimensions['AS'].visible = False
    worksheet.column_dimensions['AT'].visible = False
    worksheet.column_dimensions['AU'].visible = False
    worksheet.column_dimensions['AV'].visible = False
    worksheet.column_dimensions['AW'].visible = False
    worksheet.column_dimensions['AX'].visible = False
    worksheet.column_dimensions['AY'].visible = False
    worksheet.column_dimensions['AZ'].visible = False
    worksheet.column_dimensions['BA'].visible = False
    worksheet.column_dimensions['BB'].visible = False
    worksheet.column_dimensions['BC'].visible = False
    worksheet.column_dimensions['BD'].visible = False
    worksheet.column_dimensions['BE'].visible = False
    worksheet.column_dimensions['BF'].visible = False
    worksheet.column_dimensions['BH'].visible = False
    worksheet.column_dimensions['BI'].visible = False
    worksheet.column_dimensions['BJ'].visible = False
    worksheet.column_dimensions['BK'].visible = False
    worksheet.column_dimensions['BL'].visible = False
    worksheet.column_dimensions['BM'].visible = False
    worksheet.column_dimensions['BN'].visible = False
    worksheet.column_dimensions['BO'].visible = False
    worksheet.column_dimensions['BP'].visible = False
    worksheet.column_dimensions['BQ'].visible = False
    worksheet.column_dimensions['BR'].visible = False
    worksheet.column_dimensions['BS'].visible = False
    worksheet.column_dimensions['BT'].visible = False
    worksheet.column_dimensions['BU'].visible = False
    worksheet.column_dimensions['BV'].visible = False
    worksheet.column_dimensions['BW'].visible = False
    worksheet.column_dimensions['BX'].visible = False
    worksheet.column_dimensions['BY'].visible = False
    worksheet.column_dimensions['BZ'].visible = False
    worksheet.column_dimensions['CA'].visible = False
    worksheet.column_dimensions['CB'].visible = False
    worksheet.column_dimensions['CC'].visible = False
    worksheet.column_dimensions['CD'].visible = False
    worksheet.column_dimensions['CE'].visible = False
    worksheet.column_dimensions['CF'].visible = False
    worksheet.column_dimensions['CG'].visible = False
    worksheet.column_dimensions['CH'].visible = False
    worksheet.column_dimensions['CI'].visible = False
    worksheet.column_dimensions['CJ'].visible = False
    worksheet.column_dimensions['CK'].visible = False
    worksheet.column_dimensions['CL'].visible = False
    worksheet.column_dimensions['CM'].visible = False
    worksheet.column_dimensions['CN'].visible = False
    worksheet.column_dimensions['CO'].visible = False
    worksheet.column_dimensions['CP'].visible = False
    worksheet.column_dimensions['CQ'].visible = False
    worksheet.column_dimensions['CR'].visible = False
    worksheet.column_dimensions['CS'].visible = False
    worksheet.column_dimensions['CU'].visible = False

此外,如果有人可以告诉我是否有更有效的方法来隐藏列,我确定可能存在,这将是很棒的。

3 个答案:

答案 0 :(得分:10)

您应该将hidden属性设置为True

 worksheet.column_dimensions['A'].hidden= True

为了隐藏多个列:

for col in ['A', 'B', 'C']:
    worksheet.column_dimensions[col].hidden= True

答案 1 :(得分:3)

列可以分组:

ws.column_dimensions.group(start='B', end='CU', hidden=True)

答案 2 :(得分:0)

您可以为已定义的工作簿wb使用循环。 在此示例中,我有10列带有数据的列,并希望隐藏所有剩余的列 16385是最后一个excel列XFD +1的索引。

import openpyxl as op

worksheet = wb['Sheet1']

max_column =ws.max_column

last_column = op.utils.cell.column_index_from_string('XFD')

for idx in range(max_column+1, last_column+1):
    ws.column_dimensions[op.utils.get_column_letter(idx)].hidden = True

如果您知道列的位置,那么会很容易