excel - openpyxl: (column) grouping - outline level not working & retaining only last one - Stack Overflow

I am trying to group columns at multiple levels and in this process only the last outline level is reta

I am trying to group columns at multiple levels and in this process only the last outline level is retained, though the outline number area in spreadhseet shows all outlines. I referred to this past post and it worked initially and it stopped working for the below code:

Here is my code:

from openpyxl.utils.cell import get_column_letter

def group_xlsx(output_worksheet):

    start_row=1 #start row
    start_column=2 #start column
    total_columns=48 #total columns
    columns_number=3 #columns in each group
    
    #outer group
    first_column=get_column_letter(start_column)
    last_column=get_column_letter(total_columns+2)
    output_worksheet.column_dimensions.group(first_column,last_column,hidden=True,outline_level=1)
    
    print('Outer group columns are {} & {}'.format(first_column,last_column))

    #inner group
    for index in range(start_column,total_columns+1,columns_number+1):
        first_column=get_column_letter(index)
        last_column=get_column_letter(index+2)
        output_worksheet.column_dimensions.group(first_column,last_column,hidden=True,outline_level=2)

    print('Last inner group columns are {} & {}'.format(first_column,last_column))

    return

This is how it looks in spreadheet

Not sure where it is going wrong. Any suggestions are greatly appreciated.

I am trying to group columns at multiple levels and in this process only the last outline level is retained, though the outline number area in spreadhseet shows all outlines. I referred to this past post and it worked initially and it stopped working for the below code:

Here is my code:

from openpyxl.utils.cell import get_column_letter

def group_xlsx(output_worksheet):

    start_row=1 #start row
    start_column=2 #start column
    total_columns=48 #total columns
    columns_number=3 #columns in each group
    
    #outer group
    first_column=get_column_letter(start_column)
    last_column=get_column_letter(total_columns+2)
    output_worksheet.column_dimensions.group(first_column,last_column,hidden=True,outline_level=1)
    
    print('Outer group columns are {} & {}'.format(first_column,last_column))

    #inner group
    for index in range(start_column,total_columns+1,columns_number+1):
        first_column=get_column_letter(index)
        last_column=get_column_letter(index+2)
        output_worksheet.column_dimensions.group(first_column,last_column,hidden=True,outline_level=2)

    print('Last inner group columns are {} & {}'.format(first_column,last_column))

    return

This is how it looks in spreadheet

Not sure where it is going wrong. Any suggestions are greatly appreciated.

Share Improve this question asked Mar 13 at 15:16 Rajagopal PolisettiRajagopal Polisetti 354 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

With the way it is formatted in Excel, you'd want to do something like the following to achieve what I believe you are attempting;

from openpyxl.utils.cell import get_column_letter
import openpyxl


def group_xlsx(output_worksheet):
    
    # outline_level 1
    for i in range(5, 50, 4):
        output_worksheet.column_dimensions.group(get_column_letter(i), hidden=True, outline_level=1)
        print(f"Setting group for separation column: {get_column_letter(i)}")

    # outline_level 2
    for j in range(2, 50, 4):
        output_worksheet.column_dimensions.group(get_column_letter(j), get_column_letter(j+2), hidden=True, outline_level=2)
        print(f"Setting group for {get_column_letter(j)}:{get_column_letter(j+2)}")

    return

filename = 'grouping.xlsx'

# Create Excel
wb = openpyxl.Workbook()
ws = wb.active

# Call function
group_xlsx(ws)

# Save workbook
wb.save(filename)

The Sheet should look like the following if I read what your trying to do in your code correctly.
Obviously with the groups expanded

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744693922a4588375.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信