xml2xlsx icon indicating copy to clipboard operation
xml2xlsx copied to clipboard

TypeError is raised when cell with date type is empty

Open exer2k opened this issue 6 years ago • 1 comments

  • XML2XLSX version: 1.0.1
  • Python interpreter version: 3.7.3
  • Platform: Darwin-18.6.0-x86_64-i386-64bit

Assume that there is a cell with type attribute set to date, which has no value. During the conversion, TypeError gets thrown by the strptime() function, as the value of passed argument self._cell.value is None, rather than a string, which is expected.

Here is the traceback:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/case.py", line 59, in testPartExecutor
    yield
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/case.py", line 615, in run
    testMethod()
  File "/Users/exer2k/code/xml2xlsx/tests/__init__.py", line 169, in test_cell_type_date
    sheet = io.BytesIO(xml2xlsx(template))
  File "/Users/exer2k/code/xml2xlsx/xml2xlsx/__init__.py", line 237, in xml2xlsx
    return etree.XML(xml, parser, )
  File "src/lxml/etree.pyx", line 3213, in lxml.etree.XML
  File "src/lxml/parser.pxi", line 1876, in lxml.etree._parseMemoryDocument
  File "src/lxml/parser.pxi", line 1757, in lxml.etree._parseDoc
  File "src/lxml/parser.pxi", line 1068, in lxml.etree._BaseParser._parseUnicodeDoc
  File "src/lxml/parsertarget.pxi", line 192, in lxml.etree._TargetParserContext._handleParseResultDoc
  File "src/lxml/parsertarget.pxi", line 180, in lxml.etree._TargetParserContext._handleParseResultDoc
  File "src/lxml/etree.pyx", line 318, in lxml.etree._ExceptionContext._raise_if_stored
  File "src/lxml/saxparser.pxi", line 485, in lxml.etree._handleSaxEnd
  File "src/lxml/parsertarget.pxi", line 99, in lxml.etree._PythonSaxParserTarget._handleSaxEnd
  File "/Users/exer2k/code/xml2xlsx/xml2xlsx/__init__.py", line 216, in end
    self._cell.value, self._cell_date_format).date()
TypeError: strptime() argument 1 must be str, not None

This can be asserted with the following unit test:

import unittest
from xml2xlsx import xml2xlsx

class CellTest(unittest.TestCase):
    def test_empty_cell_type_date(self):
        template = """
        <sheet title="test">
            <row><cell type="date" date-fmt="%d.%m.%Y"></cell></row>
        </sheet>
        """
        self.assertRaises(TypeError, xml2xlsx, template)

Instead, the script should catch this, leave the cell empty, and continue with the execution. It is a simple fix, and I can provide a pull request if you accept them.

exer2k avatar Jul 29 '19 20:07 exer2k

Waiting for pull request then. Will be happy to merge it.

pkaczynski avatar Jul 29 '19 20:07 pkaczynski