我尝试读取单元格值,例如 Excel 工作表中的 E5 和 E5 包含一个公式 '=(A29 - A2)'。我使用以下代码,它返回 0.00 而不是实际值1.440408。有办法解决这个问题吗?我想打印正确的值。请在这件事上给予我帮助。谢谢。
book = xlrd.open_workbook('Test.xlsx')
first_sheet = book.sheet_by_index(0)
particular_cell_value = (first_sheet.cell_value(4,4))
print(particular_cell_value)
Excel 文件独立存储公式和值。如果文件是由 Excel 直接保存的,那么 Excel 会同时写入这两个文件。但是,其他软件(例如 xlwt、OpenPyXL 和 XlsxWriter)生成的文件,只写公式。这是因为无法保证第三方包能够以与 Excel 完全相同的方式评估 Excel 的内置函数。因此,建议(实际上来自微软本身)是让第三方软件包编写公式,将值设置为零,并将全局“自动重新计算”标志设置为打开(因此,如果在 Excel 中打开文件,Excel 将继续计算
所有公式都会自动计算结果)。
XlsxWriter 在其文档中详细说明了write_formula() http://xlsxwriter.readthedocs.org/worksheet.html#write_formula method:
XlsxWriter doesn’t calculate the value 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. This is the method recommended in the Excel documentation and in general it works fine with spreadsheet applications. However, applications that don’t have a facility to calculate formulas, such as Excel Viewer, or some mobile applications will only display the 0 results.
然后,如果您确实想在那里输入值,它会继续告诉您如何将值和公式写在一起。该值可以是您选择的任何值,这不一定与 Excel 计算的值相同。截至撰写本文时,xlwt 和 OpenPyXL 都不支持此功能。
所以,说了这么多,当谈到reading一个Excel文件,自然xlrd会取0,除非结果也同时写入。您会注意到,当 Excel 最后一次保存文件时,xlrd 总是会选取正确的结果。它还会获取结果(可能正确也可能不正确)如果你写了一个使用 XlsxWriter。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)