Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError is raised when cell with date type is empty #3

Open
exer2k opened this issue Jul 29, 2019 · 1 comment · May be fixed by #4
Open

TypeError is raised when cell with date type is empty #3

exer2k opened this issue Jul 29, 2019 · 1 comment · May be fixed by #4

Comments

@exer2k
Copy link

exer2k commented Jul 29, 2019

  • 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.

@pkaczynski
Copy link
Owner

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

@exer2k exer2k linked a pull request Jul 29, 2019 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants