动态读取和/或覆盖与Python Excel文件没有出现在覆盖警报

问题描述 投票:1回答:1

出于某种原因,下面的代码运行正常,但文件覆盖警报保持上来,即使我已经设置xl.EnableEvents =假,代码将不会进一步执行,除非我手动点击覆盖文件弹出。有谁知道如何解决这一问题?

该代码打开它包含一个字符串,它允许Excel文件连接到彭博API一个Excel文件,我用这个解决方案here来得到这个工作。只要文件被打开足够长的时间的数据被拉入该文件,然后保存并退出。它需要大约〜35秒,以获取数据和大熊猫表开始显示我请求的内容

问题是弹出窗口! - 我需要看的时候串“#N / A请求数据...”不再是在文件中并不能看到一个办法做到这一点无需定期保存文件。一个解决方案,让我看到动态的文件内容,而无需保存将是巨大的。

here没有为我工作,以阻止弹出窗口的解决方案,我可能使一个新的文件,每次,然后删除它们全都在结尾处,但是这似乎有点笨重。这个问题如果有人希望看到一个更完整的上下文的代码和问题扩展了这个问题here

WB = 'C:/path/to/my/file.xlsx'
location = "EGLL"

def run_VWA(WB, location):
    """open the excel file, allow enough time to pull the data, then close and save"""

    bb = 'C:/blp/API/Office Tools/BloombergUI.xla'
    xl=win32com.client.DispatchEx("Excel.Application")  
    xl.Workbooks.Open(bb)
    xl.AddIns("Bloomberg Excel Tools").Installed = True

    wb = xl.Workbooks.Open(Filename=WB) #opens workbook in readonly mode.

    xl.Visible = False
    xl.EnableEvents = False
    xl.DisplayAlerts = False

    total=0
    colstring='#N/A Requesting Data...'
    while total < 40:
        wb.Save()   
        df = df_from_excel(WB, location)
        if colstring not in df:
            break
        time.sleep(3)
        total+=3


    wb.Close(SaveChanges=1)
    xl.DisplayAlerts = True
    xl.Quit()
    #Cleanup the com reference. 
    del xl   

    return

任何帮助,这是非常赞赏,我与win32com库的经验非常有限。

python excel win32com bloomberg
1个回答
1
投票

经过好几个小时的挖掘,我发现如何,而不需要保存每一次迭代的文件中动态解决这个问题。如果任何人碰到这个问题大部分解决方案被发现here。非常感谢assylias一些有用的线索。

def run_VWA(WB, location):
    """open the excel file, allow enough time to pull the data, then close and save"""

    bb = 'C:/blp/API/Office Tools/BloombergUI.xla'
    xl=win32com.client.DispatchEx("Excel.Application")  
    xl.Workbooks.Open(bb)
    xl.AddIns("Bloomberg Excel Tools").Installed = True

    wb = xl.Workbooks.Open(Filename=WB) #opens workbook in readonly mode.

    xl.Visible = False
    xl.EnableEvents = False
    xl.DisplayAlerts = False

    count=0
    while True:
        readData = wb.Worksheets(location)
        allData = readData.UsedRange
        if allData.Rows.Count > 1 or allData.Columns.Count > 1:
            print('rows: {}'.format(allData.Rows.Count))
            print('cols: {}'.format(allData.Columns.Count))
            break
        print(wb)
        print(count)
        time.sleep(3)
        count+=3


    wb.Close(SaveChanges=1)
    xl.DisplayAlerts = True
    xl.Quit()
    #Cleanup the com reference. 
    del xl   

    return
© www.soinside.com 2019 - 2024. All rights reserved.