将Excel工作簿加载到Python中的COM对象时,数据未正确刷新/加载
因此,我正在使用win32加载excel电子表格。然后,我将电子表格分成第8-4000行和第1-65列 第1-16列都是静态数据。之后的列是所有公式(例如执行除法)或使用彭博Excel API从彭博获取数据(例如=BDP(E9&“corp”,“CRNCY”)) 现在我的问题是excel不会更新17-65列中的任何一列?当我手动加载它时,它工作得非常好。。 也许你可以从下面我的方式中看到错误。。。多谢各位 (我已经标记了VBA,因为python包装器必须传递类似VBA的代码;或者我是这么理解的。因此,如果任何VBA程序员对此有深入了解,那就太好了)将Excel工作簿加载到Python中的COM对象时,数据未正确刷新/加载,python,excel,python-3.x,vba,com,Python,Excel,Python 3.x,Vba,Com,因此,我正在使用win32加载excel电子表格。然后,我将电子表格分成第8-4000行和第1-65列 第1-16列都是静态数据。之后的列是所有公式(例如执行除法)或使用彭博Excel API从彭博获取数据(例如=BDP(E9&“corp”,“CRNCY”)) 现在我的问题是excel不会更新17-65列中的任何一列?当我手动加载它时,它工作得非常好。。 也许你可以从下面我的方式中看到错误。。。多谢各位 (我已经标记了VBA,因为python包装器必须传递类似VBA的代码;或者我是这么理解的。因
也许上帝会工作,它正在重新设计整个excel应用程序。似乎不工作;我认为这可能是一个Bloomerbg.xla(外加)的问题。当我使用Python控制台加载Excel文件时(这样我就可以动态编码),Bloomberg选项卡Refresh All按钮返回一个错误。。。现在,我已经使用xlapp.AddIns验证了它确实已安装等。因此,我将对此进行一些研究,然后再与您联系。谢谢
import win32com.client as win32
def open_workbook(xlapp, xlfile, password=None):
try:
xlwb = xlapp.Workbooks("LAP.xlsm")
except Exception as e:
print("No 1. Error: ", e)
try:
xlwb = xlapp.Workbooks.Open(xlfile, Password=password, UpdateLinks=0)
except Exception as e:
print(e)
xlwb = None
return xlwb
def main(file, password=None):
xlapp = win32.gencache.EnsureDispatch("Excel.Application")
xlapp.DisplayAlerts = True
xlapp.Visible = True
print(check.check_file(file, xlapp))
if not check.check_file(file, xlapp):
xlwb = open_workbook(xlapp, xlfile=file, password=password)
else:
print("Error") # TODO what would we do if we can't open the file, try again, prompt user?...
# The resources sheet contains Currency Exchange Rates used for formula in 'Data' Sheet
res = xlwb.Sheets('Resources')
# The Calculate() stuff is my attempt at fixing the problem
res.EnableCalculation = False
res.EnableCalculation = True
res.Calculate()
# This is the Main Sheet of interest - it is later loaded into a Panda's Dataframe
xlws = xlwb.Sheets('Data')
last_col = xlws.UsedRange.Columns.Count
last_row = xlws.UsedRange.Rows.Count
#All of this Calcualte() stuff is my attempt at fixing the problem - to no prevail
xlws.EnableCalculation = False
xlws.EnableCalculation = True
xlws.Calculate()
xlws.Columns(17).Calculate()
xlws.Columns(16).Calculate()
xlws.Columns(18).Calculate()
xlws.Range(xlws.Cells(8,17), xlws.Cells(last_row,17)).Formula = \
xlws.Range(xlws.Cells(8,17),xlws.Cells(last_row,17)).Formula
# This content will be loaded in DataFrame
content = xlws.Range(xlws.Cells(8,1), xlws.Cells(last_row, last_col)).Value
print(len(content), type(content))
print(content[:10])
# The final print statement reveals that many of the formula driven columns are 0,N/A etc..