本文介绍了使用xlsxwriter关闭并使用xlrd读取excel文件后,无法读取公式的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在fisrt,我使用xlsxwriter在一个单元格中编写一个简单的公式,然后关闭* .xlsx文件.然后,我尝试读取该单元格的值,但仅在同一程序中使用xlrd从该单元格中读取"0".

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.

如果我在一个单元格中读取了一些固定值(例如字符串或常数),它会很好地工作.如果我在* .xlsx文件中手动创建公式单元格,则xlrd也可以获取公式的值.

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.

似乎只有当我在* .xlsx中写一个forumla然后在同一程序中读取它时,我才能读取公式单元格的值.

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.3xlrd:1.0.0xlsxwriter:0.9.3

Python: 3.4.3xlrd: 1.0.0xlsxwriter: 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))

结果是:2.00.0

The result is :2.00.0

但是我希望它是:2.03.0

But I expect it to be:2.03.0

我该如何解决?非常感谢!

How can I solve it? Thanks a lot!

推荐答案

XlsxWriter不会将公式的值写入Excel.请参阅文档的公式结果部分.

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

这篇关于使用xlsxwriter关闭并使用xlrd读取excel文件后,无法读取公式的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 03:51