Python数据有效性DataValidation模块

openpyxl模块操作excell表格

openpyxl官方文档

from openpyxl  import Workbook
from openpyxl  import load_workbook
from openpyxl.worksheet.datavalidation import DataValidation

if __name__ == '__main__':

    # Create the workbook and worksheet we'll be working with
    wb = load_workbook(r'H:\AlarmAnalysis\测试.xlsx')
    print(wb.sheetnames)  
    ws = wb.active
    print('打开文件')
    # Create a data-validation object with list validation
    dv = DataValidation(type="list", formula1='"Dog,Cat,Bat"', allow_blank=True)

    # Optionally set a custom error message
    dv.error ='Your entry is not in the list'
    dv.errorTitle = 'Invalid Entry'
 
    # Optionally set a custom prompt message
    dv.prompt = 'Please select from the list'
    dv.promptTitle = 'List Selection'
    print('设置数据有效性')
    #设置验证的单元格范围
    c1=ws['A1']
    dv.add(c1)
    # Add the data-validation object to the worksheet
    ws.add_data_validation(dv)
    wb.save(r'H:\AlarmAnalysis\测试.xlsx')
    print('设置成功')
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页