如何使用 python 在 excel 中的单元格范围内的单元格范围内添加数据验证

How to add data validation in a range of cell in a range of cell in excel using python

heading_list= ['apple','type','Action','method']
for j, t in enumerate(heading_list):
            worksheet.write(row + 1, col + j, t,header_format)
            if t == 'Action':
                worksheet.data_validation(row+1,3,col+j,3 ,{'validate': 'list', 'source': ['Insert', 'Update']})

我想将验证添加到特定行..我该怎么做?worksheet.data_validation(row+1,3,col+j,3 ,{'validate': 'list', 'source': ['Insert', 'Update']}) 没有相应地工作

代码有效。我认为问题在于 (row, col) 坐标错误,并且数据验证不在您期望的单元格中。

固定(行,列)坐标应该给出预期的结果:

import xlsxwriter

workbook = xlsxwriter.Workbook('test.xlsx')
worksheet = workbook.add_worksheet()

header_format = workbook.add_format({'border': 1, 'bg_color': '#C6EFCE'})

row = 0
col = 0

heading_list = ['apple', 'type', 'action', 'method']
for j, t in enumerate(heading_list):
    worksheet.write(row, col + j, t, header_format)
    if t == 'action':
        worksheet.data_validation(row + 1, col + j, row + 1, col + j,
                                  {'validate': 'list',
                                   'source': ['Insert', 'Update']})


workbook.close()

输出: