你可以使用
openpyxl
来提取下拉列表的信息:它存储在给定工作表的 data_validations 中。例如(为了易于阅读,插入了换行符):
>>> wb = openpyxl.load_workbook("dropdown.xlsx")
>>> ws = wb["Sheet1"]
>>> ws.data_validations
<openpyxl.worksheet.datavalidation.DataValidationList object>
Parameters:
disablePrompts=None, xWindow=None, yWindow=None, count=1,
dataValidation=[<openpyxl.worksheet.datavalidation.DataValidation object>
Parameters:
sqref=<MultiCellRange [E1]>, showErrorMessage=True, showDropDown=None, showInputMessage=True,
allowBlank=False, errorTitle=None, error=None, promptTitle=None, prompt=None,
type='list', errorStyle=None, imeMode=None, operator=None, formula1='$L$4:$L$7', formula2=None]
我不会处理所有可能的情况,所以这只是一些你可能要做的事情的示例,但是类似于这样的内容:
def read_with_dropdown(book_name, sheet_name, range_str):
wb = openpyxl.load_workbook(book_name)
ws = wb[sheet_name]
data = [[cell.value for cell in row] for row in ws[range_str]]
validations = ws.data_validations.dataValidation
for validation in validations:
ranges = validation.sqref.ranges
if len(ranges) != 1:
raise NotImplementedError
if validation.type == 'list':
list_cells = ws[validation.formula1]
values = [cell.value for cell_row in list_cells for cell in cell_row]
else:
raise NotImplementedError
bounds = ranges[0].bounds
try:
data[bounds[1]-1][bounds[0]-1] = values
except IndexError:
pass
return data
给我(再次插入换行符):
>>> data = read_with_dropdown("dropdown.xlsx", "Sheet1", "A1:E5")
>>> data
[['Column 1', 'Column 2', 'Column 3', 'Column 4', ['yes', 'no', 'yes1', 'no1']],
[None, None, None, None, None],
[None, None, None, None, None],
[None, None, None, None, None],
[None, None, None, None, None]]