_bind_value + Django problem: either use __str__ for the object or__repr__ in the error message

Issue #747 resolved
d33 tah created an issue

Hi! Thanks for making this library! :)

I recently had a bug that boiled down to openpyxl saying "Cannot convert <random string> to Excel". It turned out that my project was passing a Django object to openpyxl and the error message contained its __str__. Could you either use __repr__ in the error message so it's easier to debug or - preferably - try calling __str__ on an unknown object instead of crashing?

Cheers, d33tah

Comments (5)

  1. CharlieC

    Thanks for the report and glad you like the library. A PR would be nice or failing that a test case.

  2. d33 tah reporter


    How about this one?

      /tmp cat a.py 
    class C:
        def __str__(self):
            return "hi"
    from openpyxl import Workbook
    wb = Workbook()
    ws = wb.active
    ws['A1'] = C()
      /tmp python a.py
    Traceback (most recent call last):
      File "a.py", line 8, in <module>
        ws['A1'] = C()
      File "/home/d/virtualenv/lib/python2.7/site-packages/openpyxl/worksheet/worksheet.py", line 403, in __setitem__
        self[key].value = value
      File "/home/d/virtualenv/lib/python2.7/site-packages/openpyxl/cell/cell.py", line 313, in value
      File "/home/d/virtualenv/lib/python2.7/site-packages/openpyxl/cell/cell.py", line 217, in _bind_value
        raise ValueError("Cannot convert {0} to Excel".format(value))
    ValueError: Cannot convert hi to Excel
  3. Log in to comment