Error: ValueError
An error occurred with the code and the program raised a ValueError.
Error Description
The error is caused when attempting to create a sheet with a name that already exists, while the setting “if_sheet_exists” is set to “error”.
Solution
To solve this error, you have a few options:
- Change the value of the “if_sheet_exists” parameter to “overwrite”. This will overwrite the existing sheet if it already exists. Example:
- Rename the sheet you are trying to create with a different name that doesn’t already exist. Example:
- If you want to raise an error when a sheet with the same name already exists, handle the ValueError exception and display a custom error message. Example:
workbook.create_sheet('Sheet1', if_sheet_exists='overwrite')
workbook.create_sheet('NewSheet')
try:
workbook.create_sheet('Sheet1', if_sheet_exists='error')
except ValueError:
print("Sheet 'Sheet1' already exists. Please choose a different name.")