1 回答
TA贡献1815条经验 获得超6个赞
我无法Pandas在下拉列表中添加,但我能够读回文件,更新它,然后像这样写回:
from openpyxl.worksheet.datavalidation import DataValidation
from openpyxl import load_workbook
def add_drop_down(file_path, file_name, row_total):
print("adding drop down")
wb = load_workbook("".join([file_path, file_name]))
ws = wb['Sheet1']
ws['S2'] = 'Yes'
ws['S3'] = 'No'
ws['S4'] = 'Maybe'
ws['S5'] = 'OK'
ws['S6'] = 'Not OK'
ws['S7'] = 'Check'
ws['T2'] = 'What1'
ws['T3'] = 'What2'
ws['T4'] = 'What3'
current_row = 2
while current_row < row_total + 2:
data_val_results = DataValidation(type="list",formula1='=S2:S7')
data_val_status = DataValidation(type="list",formula1='=T2:T4')
ws.add_data_validation(data_val_results)
ws.add_data_validation(data_val_status)
row_results = "".join(["O", str(current_row)])
row_status = "".join(["P", str(current_row)])
data_val_results.add(ws[row_results])
data_val_status.add(ws[row_status])
current_row += 1
wb.save("".join([file_path, file_name]))
可能有一种更聪明的方法来做到这一点,但对于一次性出口和运输,这很好用!在本例中,下拉菜单一直向下延伸到指定的行号。我还在这个例子中放置了两个不同的下拉菜单。感谢您为我指出正确的方向!
添加回答
举报