分页: 1 / 1

openpyxl 读公式值,并写回文件

发表于 : 2022-10-05 16:42
百草谷居士
使用 openpyxl 操作 xlsx 文件。
如果想读取公式值,需要在打开文件时,指定 data_only=True ,即: load_workbook(fname, data_only=True)
但在这种模式下,写回文件 save(fname)后,表格里所有的公式都不见了,全变成了值。

有什么好的方法,可以既读取公式值,又可以在写回文件时,保留原来的公式?

Re: openpyxl 读公式值,并写回文件

发表于 : 2022-10-05 18:51
lilydjwg
使用 win32com 来操作 Microsoft Excel 处理吧。

Re: openpyxl 读公式值,并写回文件

发表于 : 2022-10-06 6:40
百草谷居士
lilydjwg 写了: 2022-10-05 18:51 使用 win32com 来操作 Microsoft Excel 处理吧。
一看名字就是不支持linux的样子

Re: openpyxl 读公式值,并写回文件

发表于 : 2022-10-06 11:04
lilydjwg
Excel 本来就不支持 Linux 呀。

Re: openpyxl 读公式值,并写回文件

发表于 : 2022-10-06 13:53
astolia
百草谷居士 写了: 2022-10-05 16:42 如果想读取公式值,需要在打开文件时,指定 data_only=True ,即: load_workbook(fname, data_only=True)
一看名字就觉得奇怪,一看文档果然如此。
openpyxl.reader.excel.load_workbook(filename, read_only=False, keep_vba=False, data_only=False, keep_links=True)
data_only (bool) – controls whether cells with formulae have either the formula (default) or the value stored the last time Excel read the sheet