将Excel Workbook加载到Python中的COM对象中时,数据无法正确刷新/加载

因此,我正在使用Win32加载Excel电子表格。然后,我将电子表格切成8-4000行和1-65列。

列1-16均为静态数据。此后的所有列都是公式(例如,执行除法)或使用彭博的Excel API(例如= BDP(E9&“ corp”,“ CRNCY”))从彭博(Bloomberg)提取数据。

现在我的问题是Excel将不会更新任何17-65列?当我手动加载它时,它运行完美。 也许您可以通过以下方式查看错误...谢谢。

(我已经标记了VBA,因为python包装器必须传递类似VBA的代码;否则,我可以理解。因此,如果有任何VBA编码人员对此有所了解,那就太好了

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)).Formula


    # This content will be loaded in DataFrame
    content = xlws.Range(xlws.Cells(8,1),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..
collinmao 回答:将Excel Workbook加载到Python中的COM对象中时,数据无法正确刷新/加载

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3073727.html

大家都在问