Fails to read the value of formular after using xlsxwriter to close and then usinging xlrd to read the excel file

眉间皱痕 提交于 2019-12-24 17:18:00

问题


At fisrt, I use xlsxwriter to write a simple formula in one cell and close the *.xlsx file. Then I try to read the value of the cell but only read '0' from the cell using xlrd at the same program.

If I read some fixed values (like string or constant number) in a cell, it works well. If I create a formula cell manually in a *.xlsx file, the xlrd can also get the value of the formula.

It seems that I cannot read the value of formula cell only when I write a forumla to *.xlsx and then read it in the same program.

Python: 3.4.3 xlrd: 1.0.0 xlsxwriter: 0.9.3

import xlrd
import xlsxwriter

name = 'abc.xlsx'
sheet_name = 'sheet1'
# write a formula in 'D1' as '=A1+C1'
out_book = xlsxwriter.Workbook(name, {'im_memory': True})
out_sheet = out_book.add_worksheet(sheet_name)
out_sheet.write('A1', 1)
out_sheet.write('C1', 2)
out_sheet.write_formula(0, 3, '=A1+C1')
out_book.close()

#After closing excel, read the cell 
tmp = xlrd.open_workbook(name)
sheet = tmp.sheet_by_name(sheet_name)
#show '2.0'
print (sheet.cell_value(0, 2))
#show '0.0'
print (sheet.cell_value(0, 3))

The result is : 2.0 0.0

But I expect it to be: 2.0 3.0

How can I solve it? Thanks a lot!


回答1:


XlsxWriter doesn't write the value of a formula to Excel. See the Formula Result section of the docs.



来源:https://stackoverflow.com/questions/38915228/fails-to-read-the-value-of-formular-after-using-xlsxwriter-to-close-and-then-usi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!