简体   繁体   English

是否可以插入excel公式值而不是公式

[英]is it possible to insert a excel formula value not formula

i am trying to insert a value using excel formula which is happening successfully but i want to save the value not formula ,here is the piece of code i am trying so far.我正在尝试使用成功发生的 excel 公式插入一个值,但我想保存该值而不是公式,这是我目前正在尝试的一段代码。

print("Adding formula to " + filename)         


for i,cellObj in enumerate(sheet_formula['P'],1):
        cellObj.value='=IF(AND(OR(A{0}="g_m",A{0}="s_m"),ISNUMBER(SEARCH("A", E{0}))), "A", VLOOKUP(A{0},\'i ma\'!A:B, 2, FALSE))'.format(i)
        sheet_formula.cell(row=1, column=16).value = 'C'

this piece of is able to insert formula but i want to save the value not formula这段可以插入公式,但我想保存值而不是公式

"The basic reasons for abandoning openpyxl are: (1) XLS file processing is not supported; (2) the bug of testing current version style preservation is not solved; If you encounter the above two problems, give up openpyxl and embrace xlwings. There is no way out." “放弃openpyxl的基本原因是:(1)不支持XLS文件处理;(2)测试当前版本样式保存的bug没有解决;如果遇到以上两个问题,放弃openpyxl,拥抱xlwings。有没有出路。” Grabbed from here .这里抢过来


"It's possible using xlwings which uses pywin32 objects to interact with Excel, rather than just reading/writing xlsx or csv documents like openpyxl and pandas. This way, Excel actually executes the formula, and xlwings grabs the result." “可以使用使用 pywin32 对象的xlwings与 Excel 交互,而不仅仅是读取/写入 xlsx 或 csv 文档(如 openpyxl 和 pandas)。这样,Excel 实际上执行公式,而 xlwings 获取结果。” Grabbed from here .这里抢过来


So while it's not possible (so it seems) using just Openpyxl , or any other library that does not support xls file processing, as a Python library, it is possible using xlwings .因此,尽管这是不可能的(看起来是),只使用Openpyxl ,或不支持xls文件处理任何其他库,作为一个Python库,可以使用xlwings I have added a simple sample below.我在下面添加了一个简单的示例。 I simply opened a fresh workbook, added a formula and transformed the formula to it's calculated value.我只是打开了一个新的工作簿,添加了一个公式并将公式转换为它的计算值。

import xlwings as xw
app = xw.App(visible=False, add_book=False)
wb = app.books.add()
ws = wb.sheets.active
ws['A1'].value = '=3+5'
ws['A1'].value = ws['A1'].value
wb.save(r'C:\Users\...\test.xlsx')
wb.close()
app.quit()
exit()

Hopefully the above helps.希望以上内容有所帮助。 Please keep in mind;铭记于心; I'm a Python beginner!我是 Python 初学者!


For those who are interested, some good explaination about the difference between Openpyxl and xlWings can be found here .对于那些感兴趣的人,可以在此处找到有关OpenpyxlxlWings之间区别的一些很好的解释。 And a somewhat similar problem with some answers can be found here可以在这里找到一些答案的类似问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM