简体   繁体   English

使用python动态读取和/或覆盖excel文件而不会出现覆盖警报

[英]dynamically read and/or overwrite an excel file with python without the overwrite alert appearing

For some reason the following code runs fine but the file overwrite alert keeps coming up even though I have set xl.EnableEvents = False, the code won't execute further unless I manually click the overwrite file popup.出于某种原因,以下代码运行良好,但即使我设置了 xl.EnableEvents = False,文件覆盖警报仍然出现,除非我手动单击覆盖文件弹出窗口,否则代码不会进一步执行。 Does anyone know how to fix this?有谁知道如何解决这一问题?

The code opens an excel file which contains a string which allows the excel file to connect to the bloomberg api, I used this solution here to get this to work.代码打开一个 excel 文件,其中包含一个字符串,该字符串允许 excel 文件连接到bloomberg api,我在这里使用了这个解决方案让它工作。 As long as the file is open for enough time the data is pulled into the file and then saves and exits.只要文件打开足够长的时间,数据就会被拉入文件,然后保存并退出。 It takes around ~35 seconds to get the data and the pandas table starts displaying the content I'm requesting获取数据大约需要 35 秒,pandas 表开始显示我请求的内容

The problem is the popups!问题是弹窗! - I need to see when the string '#N/A Requesting Data...' is no longer in the file and can't see a way to do it without periodically saving the file. - 我需要查看字符串 '#N/A Requesting Data...' 何时不再出现在文件中,并且如果不定期保存文件就看不到执行此操作的方法。 A solution that allows me to see the file contents dynamically without having to save would be great.一个允许我动态查看文件内容而无需保存的解决方案会很棒。

The solution here didn't work for me to stop the popups, I could probably make a new file each time and then delete them all at the end but this seems a bit clunky. 这里的解决方案对我停止弹出窗口不起作用,我可能每次都可以创建一个新文件,然后在最后将它们全部删除,但这似乎有点笨拙。 This question extends this problem here if anyone wants to see the code and the problem in a more full context.如果有人想在更完整的上下文中查看代码和问题,则此问题将在此处扩展此问题。

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

Any help with this is much appreciated, I have very limited experience with the win32com library.非常感谢您对此的任何帮助,我对 win32com 库的经验非常有限。

After a good few hours digging I've found how to solve this dynamically without the need for saving the file each iteration.经过几个小时的挖掘,我找到了如何动态解决这个问题,而无需每次迭代都保存文件。 If anyone else comes up against this problem most of the solution was found here .如果其他人遇到这个问题,大部分解决方案都可以在这里找到。 Many thanks assylias for some useful pointers.非常感谢 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM