0

I have an array and intend to write it to a cell in Excel file. This is what I have written:

from openpyxl import Workbook
wb = Workbook()
ws = wb.active
ws.cell(row =1, column=2,value = array)

However, this is what appears:

raise ValueError("Cannot convert {0!r} to Excel".format(value))
ValueError: Cannot convert array([ 0.05636739, -1.0995235 ,  1.5621883 ,  0.23029989, -1.0048712 ,
        0.2968822 , -0.70565003,  0.980125  , -0.8437103 ,  0.61934054,
        2.0164294 ,  1.1749456 , -0.5825496 ,  0.0223734 , -0.3009992 ,
       -1.4909168 , -0.31345168, -0.2767412 , -0.8971678 , -1.1039274 ,
       -0.85181683, -0.16889095, -3.1210947 ,  0.02482696,  0.12225098,
       -0.6535463 , -0.44520944,  1.1683434 ,  0.45652604,  1.036608  ,
        0.10676127,  0.14357172, -1.1680435 ,  0.9394648 ,  0.86175704,
       -0.9481152 , -0.61173934,  0.06488147,  0.97073036,  0.24283405,
       -2.0439208 , -1.698466  ,  2.4012387 ,  0.45818126, -0.43852893,
       -0.9148734 ,  0.72079426,  0.7851535 , -0.7290141 ,  0.6462927 ,
       -0.02162193, -0.13589399,  1.159382  ,  0.6197715 ,  0.6868322 ,
       -0.26407108,  0.5959033 ,  0.6035499 , -0.36849284,  1.1357089 ,
        0.58538085, -0.88665897,  1.1236175 , -0.4649693 , -0.37097642,
        0.49457297,  0.8635137 , -0.50415665,  0.7048566 ,  0.95307064,
        0.92260027, -0.94126046,  0.664093  ,  0.73268044,  1.5148633 ,
       -0.35993278,  1.6198764 ,  0.7880315 ,  0.32650006,  1.1038091 ,
       -0.9126403 , -1.6570944 , -1.3555399 , -1.9593365 , -1.1097077 ,
       -0.41850403, -0.1392701 ,  1.5849863 , -0.19829401,  0.22036958,
       -0.12666762, -0.61043185,  0.05952802,  2.5408616 ,  1.5259178 ,
       -0.3422841 , -0.40555838, -0.6363599 , -0.8635243 , -0.00665558,
        0.15131243,  0.18074986, -0.51952434,  1.1674523 ,  0.2348943 ,
       -0.16456501,  0.12222623, -0.8483792 ,  0.63505876,  0.59282553,
       -1.0182037 , -0.98936766,  2.5243523 ,  1.27921   ,  1.1834651 ,
        0.35722128,  0.67636305,  0.08398098, -0.09863412, -1.8910272 ,
        0.6978843 ,  0.75396585,  0.12672533, -0.35542056, -1.0139618 ,
       -2.023965  ,  0.8246543 , -0.33283824, -0.99150425,  0.8465382 ,
       -1.3770554 ,  1.0512654 ,  0.3017207 , -0.01521344,  0.2868532 ,
        0.5822116 ,  0.8829187 ,  0.62899995, -2.2099032 ,  0.9080436 ,
        0.2942579 ,  1.5142432 ,  1.1774076 ,  0.88153124, -1.1418657 ,
        0.9071858 , -0.37036875, -0.7320364 ,  0.13388583,  1.3081983 ,
        2.4556623 ,  1.2906868 ,  0.5216429 , -1.2663802 , -0.62655646,
       -0.3464324 ,  0.35414752, -0.19352001,  2.7272477 ,  0.7015851 ,
       -1.2175066 ,  0.35944152,  0.00731494,  0.16970608,  1.3208112 ,
       -0.24665654, -1.0809184 ,  0.3556201 , -0.34895262,  0.22785911,
        0.57528704, -0.82995737, -0.75545216,  1.4501175 ,  0.8180947 ,
        1.5672855 , -1.066282  , -1.6521634 , -0.84497553,  0.0742275 ,
        2.4019604 ,  1.9604492 ,  0.84073836, -0.7566013 , -2.4600828 ,
        0.7296744 ,  0.83801734, -1.4829856 , -0.37807947, -1.7043909 ,
        0.43703625, -0.26007292,  1.5664383 , -0.51522386,  1.6523901 ,
       -0.04317267,  0.15829101,  0.5215144 ,  0.5581539 ,  0.49906728],
      dtype=float32) to Excel

How can I right the whole array to a cell? How can I solve this problem?

4
  • Do you want to write all values to 1 cell or each value to separate cell? Commented Feb 27, 2021 at 12:00
  • I want to write all values to one cell so that the cell contains the whole array. Commented Feb 27, 2021 at 12:01
  • 1
    A cell can't hold an array because, in Excel, an array is spread over many cells. That's the essence of Excel. Therefore, if you wish to write the value of an array to a single cell you must choose a data format other than array. For example, you might convert the array to a string and, later, convert the string back to an array. The question is why you would want to do that while you can more conveniently write an array to a range and read back the range into an array without any transformation. Commented Feb 27, 2021 at 12:17
  • The exception tells you what the problem is. Commented Feb 27, 2021 at 13:11

1 Answer 1

1

Convert list to string:

ws.cell(row=1, column=2, value=str(array))
Sign up to request clarification or add additional context in comments.

2 Comments

Then, how can I later read its content as an array?
Read cell as a string and convert to array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.