Created
September 16, 2025 06:22
-
-
Save aspose-com-gists/18e3d73d16359537f279bab7e0b43656 to your computer and use it in GitHub Desktop.
How to Create a Dropdown List in Excel using Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from aspose.cells import Workbook, CellArea, ValidationType, OperatorType, ValidationAlertType | |
| # Create a new workbook | |
| wb = Workbook() | |
| ws = wb.worksheets[0] | |
| ws.name = "InlineDemo" | |
| # Define cell area (A1) | |
| area_inline = CellArea() | |
| area_inline.start_row = 0 | |
| area_inline.start_column = 0 | |
| area_inline.end_row = 0 | |
| area_inline.end_column = 0 | |
| # Add validation | |
| v_index_inline = ws.validations.add(area_inline) | |
| v_inline = ws.validations[v_index_inline] | |
| v_inline.type = ValidationType.LIST | |
| v_inline.operator = OperatorType.NONE | |
| v_inline.in_cell_drop_down = True | |
| # Provide inline items | |
| v_inline.formula1 = '"Apple,Banana,Cherry"' | |
| # Optional input and error messages | |
| v_inline.input_message = "Pick a fruit from the list" | |
| v_inline.input_title = "Fruit" | |
| v_inline.error_message = "Please choose one of Apple, Banana, or Cherry" | |
| v_inline.error_title = "Invalid choice" | |
| v_inline.alert_style = ValidationAlertType.STOP | |
| v_inline.show_input = True | |
| v_inline.show_error = True | |
| # Save workbook | |
| wb.save("inline_dropdown.xlsx") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from aspose.cells import Workbook, CellArea, ValidationType, OperatorType | |
| # Create a new workbook | |
| wb = Workbook() | |
| ws = wb.worksheets[0] | |
| ws.name = "RangeDemo" | |
| # Create helper sheet for list values | |
| src = wb.worksheets.add("ListSource") | |
| src.cells.get(0, 0).put_value("Mango") | |
| src.cells.get(1, 0).put_value("Orange") | |
| src.cells.get(2, 0).put_value("Peach") | |
| # Create a named range | |
| rng = src.cells.create_range("A1:A3") | |
| rng.name = "FruitOptions" | |
| # Define cell area (B1) | |
| area_range = CellArea() | |
| area_range.start_row = 0 | |
| area_range.start_column = 1 | |
| area_range.end_row = 0 | |
| area_range.end_column = 1 | |
| # Add validation | |
| v_index_range = ws.validations.add(area_range) | |
| v_range = ws.validations[v_index_range] | |
| v_range.type = ValidationType.LIST | |
| v_range.operator = OperatorType.NONE | |
| v_range.in_cell_drop_down = True | |
| # Reference named range | |
| v_range.formula1 = "=FruitOptions" | |
| # Save workbook | |
| wb.save("range_dropdown.xlsx") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment