How to set formula value in openpyxl

I am trying to write a simple tool to work with .csv and .xlsx files. The idea is to read the data as .csv, add some changes and export to .xlsx with the changes added. Everything works for me, but when I try to write a .xlsx file (using openpyxl in Python) I have problems. When I open a document in LibreOffice Calc or Gnumeric, some fields are not displayed. The fields I'm having trouble with include formulas. When I click on them, the formulas are entered correctly in the field, but nothing appears in the field. If I retype the character in the formula it evaluated and the value appears.

I'm having a problem in the underlying .xml of a sheet and the cells obviously don't have a cache. But I also noticed that it doesn't always have the correct data type. I tried to fix this by explicitly setting the type to the formula, but no luck.

What I am using now to set a cell:

cell = ws.cell(column=i,row=j)
cell.set_explicit_value(value=val,data_type=cell.TYPE_FORMULA)

      

+3


source to share





All Articles