Data validation¶
Validation turns a column into a dropdown or constrains what can be typed into it. Excel enforces the rule on input only; it does not re-check values already in the cells, including the ones xlsxturbo wrote.
Data Validation¶
Add dropdowns and input constraints. Unknown keys raise errors (see Header Styling).
import xlsxturbo
import pandas as pd
df = pd.DataFrame({
'status': ['Open', 'Closed'],
'score': [85, 92],
'price': [19.99, 29.99],
'code': ['ABC', 'XYZ']
})
xlsxturbo.df_to_xlsx(df, "validated.xlsx",
validations={
# Dropdown list
'status': {
'type': 'list',
'values': ['Open', 'Closed', 'Pending', 'Review']
},
# Whole number range (0-100)
'score': {
'type': 'whole_number',
'min': 0,
'max': 100,
'error_title': 'Invalid Score',
'error_message': 'Score must be between 0 and 100'
},
# Decimal range
'price': {
'type': 'decimal',
'min': 0.0,
'max': 999.99
},
# Text length constraint
'code': {
'type': 'text_length',
'min': 3,
'max': 10
}
}
)
Validation types:
| Type | Aliases | Description | Options |
|---|---|---|---|
list |
- | Dropdown menu | values (non-empty list of strings without commas, max 255 chars total) |
whole_number |
whole, integer |
Integer range | min, max |
decimal |
number |
Decimal range | min, max |
text_length |
textlength, length |
Character count | min, max |
Optional message options:
- input_title, input_message: Prompt shown when cell is selected
- error_title, error_message: Message shown when invalid data is entered
Titles and messages can be supplied independently; every supplied value must be a string.
Notes:
- Validations apply to the data rows of the specified column
- Column patterns work: 'score_*': {...} matches all columns starting with score_
- With both min and max the rule is "between"; with only min it is "greater than or equal to", and with only max "less than or equal to"
- With neither bound, whole_number accepts any whole number from -2147483648 to 2147483647 and text_length any length; decimal needs at least one bound and raises ConfigurationError without one
- whole_number min/max are bounded to the i32 range (-2147483648 to 2147483647); a value outside that range raises ValueError naming the field and range
- decimal bounds must be finite numbers; NaN and infinity are refused
- A decimal bound may be at most 9.99999999999999E+307 either side of zero, Excel's largest number. Excel cannot open a workbook whose validation holds a larger one, such as 1e308 or sys.float_info.max, so such a bound raises ConfigurationError; to leave a side open, leave the bound out
- min above max is accepted and written as given; Excel opens the file, and the rule then rejects every entry
- Booleans are not numeric bounds, including for whole_number
- List validation values are limited to 255 total characters (Excel limitation)
- A list value may not contain a comma: Excel splits the list at commas, so "Smith, John" would become two entries. Such a value, and an empty values list, raise ConfigurationError
- On a DataFrame with no data rows nothing is written, but the validations are still checked, including that each pattern matches a column
- Works with both df_to_xlsx and dfs_to_xlsx (global or per-sheet)
- Not available in constant memory mode