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('设置成功')
发布了76 篇原创文章 · 获赞 5 · 访问量 6985
App 阅读领勋章
微信扫码 下载APP
阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览