出于某种原因,以下代码运行良好,但即使我设置了 xl.EnableEvents = False,文件覆盖警报仍然出现,代码将不会进一步执行,除非我手动单击覆盖文件弹出窗口。有谁知道如何解决这一问题?
代码打开一个excel文件,其中包含一个允许excel文件连接到bloomberg api的字符串,我使用了这个解决方案here让它发挥作用。只要文件打开足够的时间,数据就会被提取到文件中,然后保存并退出。获取数据大约需要 35 秒,pandas 表开始显示我请求的内容
问题是弹出窗口! - 我需要查看字符串“#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 库的经验非常有限。
最佳答案
经过几个小时的挖掘,我找到了如何动态解决这个问题,而无需每次迭代都保存文件。如果其他人遇到这个问题,大部分解决方案都已找到 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
关于python - 使用 python 动态读取和/或覆盖 Excel 文件,而不会出现覆盖警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54590994/