如何使用python刷新Excel [英] How to refresh excel using python
问题描述
在使用xlsx writer或openpyxl将功能写入excel之后,我需要使用python在excel中评估该功能.我需要将由该函数生成的数字拉回到python中,以便在公式中使用.我无法直接在python中计算此数字,因为它使用了自定义的excel函数来与其他程序进行交互.如果在将函数写入单元格后打开excel文件,则函数将求值.
I need to evaluate a function within excel using python right after writing that function into excel with xlsx writer or openpyxl. I need to pull the number generated by this function back into python to use in a formula. I cannot calculate this number straight in python because it uses an excel function that was custom made to interact with another program. If I open the excel file after the function is written to a cell, the function evaluates.
我尝试使用win32com,但似乎无法正常工作.还有其他人知道不使用win32com即可完成此操作的方法吗?
I've tried using win32com but does not seem to work. Does anyone else know a way to accomplish this without using win32com?
推荐答案
我知道了.我之前没有意识到您也可以使用win32com在excel中进行编写.
I figured it out. I didn't realize before that you can use win32com to write in excel as well.
import win32com.client as w3c
ex_file = w3c.DispatchEx('Excel.Application')
wb = ex_file.Workbooks.Add()
ws = wb.Worksheets.Add()
ws.Name = 'Test'
ws.Range('A1:A1') = <function>
wb.RefreshAll()
cell_val = ws.Range('A1:A1').value
print(cell_val)
wb.SaveAs(<file name.xlsx>)
ex_file.Quit()
这篇关于如何使用python刷新Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!