Search code examples
pythonexcelpython-3.xpython-modulexlsxwriter

set default value from source using validate as list in data_validation() method of xlsxwriter in python?


I have created an excel sheet using xlsxwriter module in python. I am trying to set a default value for the drop down list created with data_validation() method of xlsxwriter.
However, according to the documentation of xlsxwriter, it has no default value if we use validate as list.

sheet.data_validation('G5', {'validate': 'list',
                             'source': ['Completed',
                                        'Pending',
                                        'Script Error']})

Can anyone suggest me some work around to have a default value and drop down list in the same cell?


Solution

  • As far as I know there isn't an option in Excel to have a default value in a drop-down data validation. Hence it isn't supported by XlsxWriter.

    However, you can just write the default value to the same cell as the data validation in XlsxWriter. For example:

    import xlsxwriter
    
    workbook = xlsxwriter.Workbook('data_validate.xlsx')
    worksheet = workbook.add_worksheet()
    
    worksheet.data_validation('B3', {'validate': 'list',
                                     'source': ['Completed',
                                                'Pending',
                                                'Script Error']})
    
    worksheet.write('B3', 'Pending')                                        
    
    
    workbook.close()
    

    Output:

    enter image description here