Conflict between group and width

Issue #839 invalid
Daniel OHara created an issue

When setting group I have found:

  1. Setting column width after group set remove column from group

  2. Setting group changes all column widths to the width of first column

  3. Setting second group spanning another removes the first group

This code illustrates the issues.

import openpyxl

def fill_sheet(ws):
    ws['A1'] = 'Column A'
    ws['B1'] = 'Column B'
    ws['C1'] = 'Column C'
    ws['D1'] = 'Column D'
    ws['E1'] = 'Column E'
    ws['A2'] = 'Value A'
    ws['B2'] = 'Value B'
    ws['C2'] = 'Value C'
    ws['D2'] = 'Value D'
    ws['E2'] = 'Value E'

wb = openpyxl.Workbook()

# Sheet1: setting column width after group set remove column from group
ws1 = wb.active
fill_sheet(ws1)
ws1.column_dimensions.group('B', 'D')
ws1.column_dimensions['C'].width = 10

# Sheet2: setting group changes all column widths to the width of first column
ws2 = wb.create_sheet(title="Sheet2")
fill_sheet(ws2)
ws2.column_dimensions['B'].width = 5
ws2.column_dimensions['C'].width = 10
ws2.column_dimensions['D'].width = 10
ws2.column_dimensions.group('B', 'D')

# Sheet3: setting second group spanning another removes the first group
ws2 = wb.create_sheet(title="Sheet3")
fill_sheet(ws2)
ws2.column_dimensions.group('C', 'C')
ws2.column_dimensions.group('B', 'D')

wb.save("GroupTest.xlsx")

Comments (4)

  1. Daniel OHara reporter

    This is not the behavior I get in Excel via front end or running a Macro. This is my macro equivalent of the python.

    Sub fill_Sheet()
        Range("A1").Value = "Column A"
        Range("B1").Value = "Column B"
        Range("C1").Value = "Column C"
        Range("D1").Value = "Column D"
        Range("E1").Value = "Column E"
        Range("A2").Value = "Value A"
        Range("B2").Value = "Value B"
        Range("C2").Value = "Value C"
        Range("D2").Value = "Value D"
        Range("E2").Value = "Value E"
    End Sub
    Sub GroupTest()
        ' Sheet1: setting column width after group set leaves column in group
        Sheets("Sheet1").Select
        Call fill_Sheet
        Columns("B:D").Group
        Columns("C:C").ColumnWidth = 10
    
        ' Sheet2: setting group leaves column widths as they are
        Sheets("Sheet2").Select
        Call fill_Sheet
        Columns("B:B").ColumnWidth = 5
        Columns("C:C").ColumnWidth = 10
        Columns("D:D").ColumnWidth = 10
        Columns("B:D").Group
    
        ' Sheet3: setting second group spanning another add another level of grouping group
        Sheets("Sheet3").Select
        Call fill_Sheet
        Columns("C:C").Group
        Columns("B:D").Group
    End Sub
    

    python Sheet

    pythonSheet1.JPG

    MacrSheet1

    MacroSheet1.JPG

    python Sheet2

    pythonSheet2.JPG

    Macro Sheet2

    MacroSheet2.JPG

    python Sheet3

    pythonSheet3.JPG

    Macro Sheets

    MacroSheet3.JPG

  2. CharlieC

    According to the OOXML specification the col tag "Defines column width and column formatting for one or more columns of the worksheet."

    This is what openpyxl implements. Excel might be overloading the definition for its own purposes but looks to me to be in breach of the specification.

  3. Log in to comment