Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XlsxWriter write_formula() not working always show Zero (0)

I have work with xlsxwriter and I try to print simple formula with a print of formula into the sheet

import xlsxwriter
workbook = xlsxwriter.Workbook('filename1.xlsx')
format_val=workbook.add_format()
worksheet = workbook.add_worksheet()
worksheet.write(0,1,5)
worksheet.write(1,1,2)
worksheet.write_formula(3,0, '=SUM(B1:B2)')
workbook.close()
csvf = StringIO(import_file.read().decode())

Here the Image of how to show and when I press = than output is print 7

But output also will be Zero (0). I know that XlsxWriter doesn’t calculate the result of a formula and instead stores the value 0 as the formula result. It then sets a global flag in the XLSX file to say that all formulas and functions should be recalculated when the file is opened.

I have tried this. It is working for me but it's not a proper thing when I try with Upper Case is not working: ~

num_formate=format_val.set_num_format('0')
worksheet.write_formula(5,1, '=2+1',num_formate,4)

But when it's open How to show calculate value?

like image 693
Harshit Trivedi Avatar asked Oct 18 '25 22:10

Harshit Trivedi


1 Answers

Passing empty string to value arg works for me.

# # xlsxwriter/worksheet.py
#
# class Worksheet(xmlwriter.XMLwriter):
# ...
#     def write_formula(self, row, col, formula, cell_format=None, value=0)

worksheet.write_formula(3, 0, '=SUM(B1:B2)', None, '')
like image 173
toryano0820 Avatar answered Oct 20 '25 12:10

toryano0820



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!