[英]dynamically read and/or overwrite an excel file with python without the overwrite alert appearing
出于某种原因,以下代码运行良好,但即使我设置了 xl.EnableEvents = False,文件覆盖警报仍然出现,除非我手动单击覆盖文件弹出窗口,否则代码不会进一步执行。 有谁知道如何解决这一问题?
代码打开一个 excel 文件,其中包含一个字符串,该字符串允许 excel 文件连接到bloomberg api,我在这里使用了这个解决方案来让它工作。 只要文件打开足够长的时间,数据就会被拉入文件,然后保存并退出。 获取数据大约需要 35 秒,pandas 表开始显示我请求的内容
问题是弹窗! - 我需要查看字符串 '#N/A Requesting Data...' 何时不再出现在文件中,并且如果不定期保存文件就看不到执行此操作的方法。 一个允许我动态查看文件内容而无需保存的解决方案会很棒。
这里的解决方案对我停止弹出窗口不起作用,我可能每次都可以创建一个新文件,然后在最后将它们全部删除,但这似乎有点笨拙。 如果有人想在更完整的上下文中查看代码和问题,则此问题将在此处扩展此问题。
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 库的经验非常有限。
经过几个小时的挖掘,我找到了如何动态解决这个问题,而无需每次迭代都保存文件。 如果其他人遇到这个问题,大部分解决方案都可以在这里找到。 非常感谢 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.