使用python处理excel时出错 [英] Error while working with excel using python
问题描述
当我的脚本正在更新一个excel同一时间,如果我要做另外的工作手动的另一个excel错误发生我正在使用调度
from win32com.client import Dispatch
/ pre>
excel = Dispatch('Excel.Application')
excel.Visible = True
file_name =file_name .xls
workbook = excel.Workbooks.Open(file_name)
workBook = excel.ActiveWorkbook
sheet = workbook.Sheets(sheetno)
我像这样
(,com_error(-2147418111,'被拒绝被呼叫者的呼叫)',无,无)
有没有办法克服它..可以更新另一个excel没有geting错误..
解决方案发生此错误是因为您正在调用的COM对象如果已经在处理另一个操作,将拒绝外部调用。没有异步处理调用,并且行为可能看起来是随机的。
$ bDepend在操作上你会看到pythoncom.com_error或pywintypes.com_error。解决这个问题的一个简单(如果不合适)的方法是将您的调用包装到COM对象中,并且尝试除外,如果您收到以下访问错误之一,请重试您的呼叫。
对于某些背景,请参阅第12章摘录中的错误处理部分em> Win32上的Python编程 由Mark Hammond& Andy Robinson(O'Reilly 2000)。
在 Siew Kam Onn的博文Python编程与Excel,如何从makepy生成的python文件克服COM_error。
while my script is updating one excel same time if i am going to do any other work manually with another excel error occurs i am using dispatch
from win32com.client import Dispatch excel = Dispatch('Excel.Application') excel.Visible = True file_name="file_name.xls" workbook = excel.Workbooks.Open(file_name) workBook = excel.ActiveWorkbook sheet=workbook.Sheets(sheetno)
I am geting error like this (, com_error(-2147418111, 'Call was rejected by callee.', None, None)
Is there is any way to overcome it ..can i update another excel without geting error..
解决方案This error occurs because the COM object you're calling will reject an external call if it's already handling another operation. There is no asynchronous handling of calls and the behavior can seem random.
Depending on the operation you'll see either pythoncom.com_error or pywintypes.com_error. A simple (if inelegant) way to work around this is to wrap your calls into the COM object with try-except and, if you get one of these access errors, retry your call.
For some background see the "Error Handling" section of the chapter 12 excerpt from Python Programming on Win32 by Mark Hammond & Andy Robinson (O'Reilly 2000).
There's also some useful info specifically about Excel in Siew Kam Onn's blog post "Python programming with Excel, how to overcome COM_error from the makepy generated python file".
这篇关于使用python处理excel时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!