0

I try to group somes columns using xlswriter but it doesn't work. I tried the same with 'hidden' function and it's work.

Works ( hide columns from the 90th to the 130 th)

worksheet.set_column(90, 130, None, None,  {'hidden': 1})

Doesn't work

worksheet.set_column(90, 130, None, None,  {'collapsed': 1})
jmcnamara
  • 38,196
  • 6
  • 90
  • 108
Malou
  • 141
  • 8

1 Answers1

1

Rows and columns can be collapsed by setting the hidden flag for the hidden rows/columns and setting the collapsed flag for the row/column that has the collapsed '+' symbol.

xlsxwriter documentation

Let's take a simple example adapted from here :

Create the worksheet

import xlsxwriter

# Create a new workbook and add some worksheets
workbook = xlsxwriter.Workbook('outline_collapsed.xlsx')
worksheet = workbook.add_worksheet('Collapsed Columns')

# Add a general format
bold = workbook.add_format({'bold': 1})
data = [
    ['Month', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Total'],
    ['North', 50, 20, 15, 25, 65, 80, '=SUM(B2:G2)'],
    ['South', 10, 20, 30, 50, 50, 50, '=SUM(B3:G3)'],
    ['East', 45, 75, 50, 15, 75, 100, '=SUM(B4:G4)'],
    ['West', 15, 15, 55, 35, 20, 50, '=SUM(B5:G5)']]

# Write the data and a formula.
for row, data_row in enumerate(data):
    worksheet.write_row(row, 0, data_row)
worksheet.set_row(0, None, bold)

enter image description here

To collapse columns B to G

  • Set the hidden flag for columns B-G
  • Set the collapsed flag for the column H which will have the collapsed '+' symbol.
# Collapsing
worksheet.set_column('B:G', 5, None, {'level': 1, 'hidden': True})
worksheet.set_column('H:H', 10, None, {'collapsed': True})

enter image description here

enter image description here

Bunny
  • 1,180
  • 8
  • 22