Cannot write to worksheet with numpy.float types

Issue #1187 duplicate
Mark Brown created an issue

In older versions the _bind_value() method in cell.py uses instance(value, NUMERIC_TYPES), so a value of type numpy.float64 was correctly recognized as numpy.floating. In the latest version, type(value) is used, numpy.float64 is then compared to numpy.floating, which is not equal.

So it can no longer write numpy.float* into a cell.

Thanks for looking into this and provide a patch.

An example is below:

import numpy                                                                                                                                                                                             
import openpyxl                                                                                                                                                                                          
from openpyxl import Workbook                                                                                                                                                                            
print(openpyxl.__version__)                                                                                                                                                                              

wb = Workbook()                                                                                                                                                                                          
dest_filename = 'empty_book.xlsx'                                                                                                                                                                        
ws1 = wb.active                                                                                                                                                                                          
val=numpy.random.random(10)[0]                                                                                                                                                                           
print("Before: ", isinstance(val, numpy.floating), "After: ", type(val) in (numpy.floating,))                                                                                                            
ws1.cell(column=1, row=1, value=val) 

Produce output error message:

2.6.0
Before: True After: False
Traceback (most recent call last):
File "./t.py", line 12, in <module>
ws1.cell(column=1, row=1, value=val)
File "/usr/local/lib/python3.6/site-packages/openpyxl/worksheet/worksheet.py", line 240, in cell
cell.value = value
File "/usr/local/lib/python3.6/site-packages/openpyxl/cell/cell.py", line 252, in value
self._bind_value(value)
File "/usr/local/lib/python3.6/site-packages/openpyxl/cell/cell.py", line 218, in _bind_value
raise ValueError("Cannot convert {0!r} to Excel".format(value))
ValueError: Cannot convert 0.5908632902416832 to Excel

Comments (1)

  1. Log in to comment