1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
|
.. SPDX-License-Identifier: BSD-2-Clause
Copyright 2013-2023, John McNamara, jmcnamara@cpan.org
.. _exceptions:
The Exceptions Class
====================
The Exception class contains the various exceptions that can be raised by
XlsxWriter. In general XlsxWriter only raised exceptions for un-recoverable
errors or for errors that would lead to file corruption such as creating two
worksheets with the same name.
The hierarchy of exceptions in XlsxWriter is:
* ``XlsxWriterException(Exception)``
* ``XlsxFileError(XlsxWriterException)``
* ``FileCreateError(XlsxFileError)``
* ``UndefinedImageSize(XlsxFileError)``
* ``UndefinedImageSize(XlsxFileError)``
* ``FileSizeError(XlsxFileError)``
* ``XlsxInputError(XlsxWriterException)``
* ``DuplicateTableName(XlsxInputError)``
* ``InvalidWorksheetName(XlsxInputError)``
* ``DuplicateWorksheetName(XlsxInputError)``
* ``OverlappingRange(XlsxInputError)``
Exception: XlsxWriterException
------------------------------
.. py:exception:: XlsxWriterException
Base exception for XlsxWriter.
Exception: XlsxFileError
------------------------
.. py:exception:: XlsxFileError
Base exception for all file related errors.
Exception: XlsxInputError
-------------------------
.. py:exception:: XlsxInputError
Base exception for all input data related errors.
Exception: FileCreateError
--------------------------
.. py:exception:: FileCreateError
This exception is raised if there is a file permission, or IO error, when
writing the xlsx file to disk. This can be caused by an non-existent directory
or (in Windows) if the file is already open in Excel::
import xlsxwriter
workbook = xlsxwriter.Workbook('exception.xlsx')
worksheet = workbook.add_worksheet()
# The file exception.xlsx is already open in Excel.
workbook.close()
Raises::
xlsxwriter.exceptions.FileCreateError:
[Errno 13] Permission denied: 'exception.xlsx'
This exception can be caught in a ``try`` block where you can instruct the
user to close the open file before overwriting it::
while True:
try:
workbook.close()
except xlsxwriter.exceptions.FileCreateError as e:
decision = input("Exception caught in workbook.close(): %s\n"
"Please close the file if it is open in Excel.\n"
"Try to write file again? [Y/n]: " % e)
if decision != 'n':
continue
break
See also :ref:`ex_check_close`.
Exception: UndefinedImageSize
-----------------------------
.. py:exception:: UndefinedImageSize
This exception is raised if an image added via :func:`insert_image()` doesn't
contain height or width information. The exception is raised during Workbook
:func:`close()`::
import xlsxwriter
workbook = xlsxwriter.Workbook('exception.xlsx')
worksheet = workbook.add_worksheet()
worksheet.insert_image('A1', 'logo.png')
workbook.close()
Raises::
xlsxwriter.exceptions.UndefinedImageSize:
logo.png: no size data found in image file.
.. note::
This is a relatively rare error that is most commonly caused by XlsxWriter
failing to parse the dimensions of the image rather than the image not
containing the information. In these cases you should raise a GitHub issue
with the image attached, or provided via a link.
Exception: UnsupportedImageFormat
---------------------------------
.. py:exception:: UnsupportedImageFormat
This exception is raised if if an image added via :func:`insert_image()` isn't
one of the supported file formats: PNG, JPEG, GIF, BMP, WMF or EMF. The exception
is raised during Workbook :func:`close()`::
import xlsxwriter
workbook = xlsxwriter.Workbook('exception.xlsx')
worksheet = workbook.add_worksheet()
worksheet.insert_image('A1', 'logo.xyz')
workbook.close()
Raises::
xlsxwriter.exceptions.UnsupportedImageFormat:
logo.xyz: Unknown or unsupported image file format.
.. note::
If the image type is one of the supported types, and you are sure that the
file format is correct, then the exception may be caused by XlsxWriter
failing to parse the type of the image correctly. In these cases you should
raise a GitHub issue with the image attached, or provided via a link.
Exception: FileSizeError
------------------------
.. py:exception:: FileSizeError
This exception is raised if one of the XML files that is part of the xlsx file, or the xlsx file itself, exceeds 4GB in size::
import xlsxwriter
workbook = xlsxwriter.Workbook('exception.xlsx')
worksheet = workbook.add_worksheet()
# Write lots of data to create a very big file.
workbook.close()
Raises::
xlsxwriter.exceptions.FileSizeError:
Filesize would require ZIP64 extensions. Use workbook.use_zip64().
As noted in the exception message, files larger than 4GB can be created by
turning on the zipfile.py ZIP64 extensions using the :func:`use_zip64` method.
Exception: EmptyChartSeries
---------------------------
.. py:exception:: EmptyChartSeries
This exception is raised if a chart is added to a worksheet without a data
series. The exception is raised during Workbook :func:`close()`::
import xlsxwriter
workbook = xlsxwriter.Workbook('exception.xlsx')
worksheet = workbook.add_worksheet()
chart = workbook.add_chart({'type': 'column'})
worksheet.insert_chart('A7', chart)
workbook.close()
Raises::
xlsxwriter.exceptions.EmptyChartSeries:
Chart1 must contain at least one data series. See chart.add_series().
Exception: DuplicateTableName
-----------------------------
.. py:exception:: DuplicateTableName
This exception is raised if a duplicate worksheet table name in used via
:func:`add_table()`. The exception is raised during Workbook :func:`close()`::
import xlsxwriter
workbook = xlsxwriter.Workbook('exception.xlsx')
worksheet = workbook.add_worksheet()
worksheet.add_table('B1:F3', {'name': 'SalesData'})
worksheet.add_table('B4:F7', {'name': 'SalesData'})
workbook.close()
Raises::
xlsxwriter.exceptions.DuplicateTableName:
Duplicate name 'SalesData' used in worksheet.add_table().
Exception: InvalidWorksheetName
-------------------------------
.. py:exception:: InvalidWorksheetName
This exception is raised during Workbook :func:`add_worksheet()` if a
worksheet name is too long or contains restricted characters.
For example with a 32 character worksheet name::
import xlsxwriter
workbook = xlsxwriter.Workbook('exception.xlsx')
name = 'name_that_is_longer_than_thirty_one_characters'
worksheet = workbook.add_worksheet(name)
workbook.close()
Raises::
xlsxwriter.exceptions.InvalidWorksheetName:
Excel worksheet name 'name_that_is_longer_than_thirty_one_characters'
must be <= 31 chars.
Or for a worksheet name containing one of the Excel restricted characters,
i.e. ``[ ] : * ? / \``::
import xlsxwriter
workbook = xlsxwriter.Workbook('exception.xlsx')
worksheet = workbook.add_worksheet('Data[Jan]')
workbook.close()
Raises::
xlsxwriter.exceptions.InvalidWorksheetName:
Invalid Excel character '[]:*?/\' in sheetname 'Data[Jan]'.
Or for a worksheet name start or ends with an apostrophe::
import xlsxwriter
workbook = xlsxwriter.Workbook('exception.xlsx')
worksheet = workbook.add_worksheet("'Sheet1'")
workbook.close()
Raises::
xlsxwriter.exceptions.InvalidWorksheetName:
Sheet name cannot start or end with an apostrophe "'Sheet1'".
Exception: DuplicateWorksheetName
---------------------------------
.. py:exception:: DuplicateWorksheetName
This exception is raised during Workbook :func:`add_worksheet()` if a
worksheet name has already been used. As with Excel the check is case
insensitive::
import xlsxwriter
workbook = xlsxwriter.Workbook('exception.xlsx')
worksheet1 = workbook.add_worksheet('Sheet1')
worksheet2 = workbook.add_worksheet('sheet1')
workbook.close()
Raises::
xlsxwriter.exceptions.DuplicateWorksheetName:
Sheetname 'sheet1', with case ignored, is already in use.
Exception: OverlappingRange
---------------------------------
.. py:exception:: OverlappingRange
This exception is raised during Worksheet :func:`add_table()` or
:func:`merge_range()` if the range overlaps an existing worksheet table or merge
range. This is a file corruption error in Excel::
import xlsxwriter
workbook = xlsxwriter.Workbook('exception.xlsx')
worksheet = workbook.add_worksheet()
worksheet.merge_range('A1:G10', 'Range 1')
worksheet.merge_range('G10:K20', 'Range 2')
workbook.close()
Raises::
xlsxwriter.exceptions.OverlappingRange:
Merge range 'G10:K20' overlaps previous merge range 'A1:G10'.
|