赞
踩
- 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('设置成功')
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。