how to get formula result in excel using xlwings

时间:2016-12-02 05:07:28

标签: python-2.7 xlwings

What i want to do is 1)get a folmula result in excel and 2)update the values to the existing excel file. [output_result 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]

1 个答案:

答案 0 :(得分:0)

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

为了获得你可以做的价值:

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

另请注意,您可以使用例如

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