如何使用xlwings在excel中获得公式结果 [英] how to get formula result in excel using xlwings

查看:68
本文介绍了如何使用xlwings在excel中获得公式结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是 1) 在 excel 中获得一个公式结果 2) 将值更新到现有的 excel 文件中.[ 我使用xlsxwriter"创建并编写了公式.但是当我尝试openpyxl(或pandas)来检索folmula结果时,它返回0.我想使用xlwings"来解决这个问题,但不知道该怎么做.有人可以帮忙吗?

What i want to do is 1)get a folmula result in excel and 2)update the values to the existing excel file. [ I created and wrote the folmula using "xlsxwriter". But when I tried openpyxl (or pandas) to retrieve the folmula result, it returns 0. I want to use "xlwings" to solve this problem, but no idea how to do it. can anyone help?

#openpyx
wb = openpyxl.load_workbook(filename=xlsx_name,data_only=True)
ws = wb.get_sheet_by_name("sheet1")
print "venn_value",(ws.cell('X2').value)
#pandas 
fold_merge_data=pd.read_excel(xlsx_name,sheetname=1)
print fold_merge_data['Venn diagram'][:10]

推荐答案

是的,xlwings 可以为您解决这个问题,因为它使用 pywin32 对象与 Excel 进行交互,而不仅仅是读/写 xlsx或 csv 文件,如 openpyxl 和 pandas.这样,Excel实际执行公式,xlwings抓取结果.

Yes, xlwings can solve this problem for you because it 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.

为了获得价值,你可以这样做:

In order to get the value you can do:

import xlwings as xw
sheet = xw.sheets.active # if the document is open
#otherwise use sheet = xw.Book(r'C:/path/to/file.xlsx').sheets['sheetname']
result = sheet['X2'].value

另外,请注意您可以使用例如设置公式

Also, note that you can set the formula using, for example

sheet['A1'].value = '=1+1' # or ='B1*2' if you want to reference other cells

这篇关于如何使用xlwings在excel中获得公式结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆