当前位置:   article > 正文

Python数据有效性DataValidation模块_python datavalidation

python datavalidation

openpyxl模块操作excell表格

openpyxl官方文档

  1. from openpyxl import Workbook
  2. from openpyxl import load_workbook
  3. from openpyxl.worksheet.datavalidation import DataValidation
  4. if __name__ == '__main__':
  5. # Create the workbook and worksheet we'll be working with
  6. wb = load_workbook(r'H:\AlarmAnalysis\测试.xlsx')
  7. print(wb.sheetnames)
  8. ws = wb.active
  9. print('打开文件')
  10. # Create a data-validation object with list validation
  11. dv = DataValidation(type="list", formula1='"Dog,Cat,Bat"', allow_blank=True)
  12. # Optionally set a custom error message
  13. dv.error ='Your entry is not in the list'
  14. dv.errorTitle = 'Invalid Entry'
  15. # Optionally set a custom prompt message
  16. dv.prompt = 'Please select from the list'
  17. dv.promptTitle = 'List Selection'
  18. print('设置数据有效性')
  19. #设置验证的单元格范围
  20. c1=ws['A1']
  21. dv.add(c1)
  22. # Add the data-validation object to the worksheet
  23. ws.add_data_validation(dv)
  24. wb.save(r'H:\AlarmAnalysis\测试.xlsx')
  25. print('设置成功')
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/907510
推荐阅读
相关标签
  

闽ICP备14008679号