使用python处理excel时出错

时间:2021-07-21 21:50:13

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

当我的脚本同时更新一个excel的时候,如果我要手动处理另一个excel错误,我将使用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)

我的错误是这样的(com_error(-2147418111),调用被callee拒绝。,没有,没有)

Is there is any way to overcome it ..can i update another excel without geting error..

有没有办法克服它?我可以更新另一个excel而不出错吗

3 个解决方案

#1


4  

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.

发生此错误是因为您正在调用的COM对象将拒绝外部调用,如果它已经在处理另一个操作。没有对调用进行异步处理,并且行为看起来可能是随机的。

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.

根据操作的不同,您将看到pythoncom.com_error或pywintypes.com_error。解决这个问题的一种简单(如果不太优雅)方法是将您的调用封装到带有try-except的COM对象中,如果您收到这些访问错误之一,请重试您的调用。

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).

有关一些背景知识,请参阅Mark Hammond和Andy Robinson (O'Reilly 2000)在Win32上从Python编程中摘录的第12章“错误处理”一节。

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".

在Siew Kam Onn的博客文章“Python编程与Excel,如何克服makepy生成的Python文件的COM_error”中,还有一些有用的信息。

#2


0  

steven may be my variable names are not good but there is no gramatical mistake in code its workes fine my only concern is that com error

steven可能是我的变量名不好,但是在代码中没有语法错误,很好,我唯一关心的是com错误

my script runs without error if i have not started another excel

如果我没有启动另一个excel,我的脚本就可以正常运行

my quetion is anyone knows how to overcome it with using dispatch OR is there any other excel module for python where i can deal with this problem?

我的问题是,有人知道如何通过使用dispatch来解决这个问题吗?或者有其他针对python的excel模块可以解决这个问题吗?

excel= Dispatch('Excel.Application')

excel =调度(“Excel.Application”)

excel.Visible = True

excel。可见= True

file_name = "E:\Report.xls"

file_name = " E:\ Report.xls "

workbook = excel.Workbooks.Open(file_name)

工作簿= excel.Workbooks.Open(file_name)

sheet=workbook.Sheets(1)

表= workbook.Sheets(1)

consider i am writing somethin in excel

sheet.Cells(1,1).Value = 'test1'

sheet.Cells(1,1)。值= ' test1 '

sheet.Cells(1,2).Value = 'test2'

sheet.Cells(1、2)。值= ' test2 '

sheet.Cells(1,3).Value = 'test3'

sheet.Cells(1、3)。值= ' test3 '

sheet.Cells(1,4).Value = 'test4'

sheet.Cells(1,4)。值= ' test4 '

sheet.Cells(1,5).Value = 'test5'

sheet.Cells(1、5)。值= ' test5 '

sheet.Cells(1,6).Value = 'test6'

sheet.Cells(6)。值= ' test6 '

sheet.Cells(1,7).Value = 'test7'

sheet.Cells(7)。值= ' test7 '

sheet.Cells(1,8).Value = 'test8'

sheet.Cells(8)。值= ' test8 '

#3


0  

I encountered this same issue recently. While it sounds like there can be multiple root causes, my situation was occurring because Python was making subsequent calls too quickly for Excel to keep up, particularly with external query refreshes. I resolved this intermittent "Call was rejected by callee" error by inserting time.sleep() between most of my calls and increasing the sleep argument for any calls that are particularly lengthy (usually between 7-15 seconds). This allows Excel the time to complete each command before Python issued additional commands.

我最近遇到了同样的问题。虽然听起来可能有多个根原因,但我的情况发生了,因为Python对Excel进行后续调用的速度太快,无法跟上,特别是在外部查询刷新时。我解决了这个间歇性的“调用被callee拒绝”错误,方法是在大多数调用之间插入time.sleep(),并为任何特别长(通常在7-15秒之间)的调用增加睡眠参数。这允许Excel在Python发出附加命令之前完成每个命令。

#1


4  

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.

发生此错误是因为您正在调用的COM对象将拒绝外部调用,如果它已经在处理另一个操作。没有对调用进行异步处理,并且行为看起来可能是随机的。

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.

根据操作的不同,您将看到pythoncom.com_error或pywintypes.com_error。解决这个问题的一种简单(如果不太优雅)方法是将您的调用封装到带有try-except的COM对象中,如果您收到这些访问错误之一,请重试您的调用。

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).

有关一些背景知识,请参阅Mark Hammond和Andy Robinson (O'Reilly 2000)在Win32上从Python编程中摘录的第12章“错误处理”一节。

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".

在Siew Kam Onn的博客文章“Python编程与Excel,如何克服makepy生成的Python文件的COM_error”中,还有一些有用的信息。

#2


0  

steven may be my variable names are not good but there is no gramatical mistake in code its workes fine my only concern is that com error

steven可能是我的变量名不好,但是在代码中没有语法错误,很好,我唯一关心的是com错误

my script runs without error if i have not started another excel

如果我没有启动另一个excel,我的脚本就可以正常运行

my quetion is anyone knows how to overcome it with using dispatch OR is there any other excel module for python where i can deal with this problem?

我的问题是,有人知道如何通过使用dispatch来解决这个问题吗?或者有其他针对python的excel模块可以解决这个问题吗?

excel= Dispatch('Excel.Application')

excel =调度(“Excel.Application”)

excel.Visible = True

excel。可见= True

file_name = "E:\Report.xls"

file_name = " E:\ Report.xls "

workbook = excel.Workbooks.Open(file_name)

工作簿= excel.Workbooks.Open(file_name)

sheet=workbook.Sheets(1)

表= workbook.Sheets(1)

consider i am writing somethin in excel

sheet.Cells(1,1).Value = 'test1'

sheet.Cells(1,1)。值= ' test1 '

sheet.Cells(1,2).Value = 'test2'

sheet.Cells(1、2)。值= ' test2 '

sheet.Cells(1,3).Value = 'test3'

sheet.Cells(1、3)。值= ' test3 '

sheet.Cells(1,4).Value = 'test4'

sheet.Cells(1,4)。值= ' test4 '

sheet.Cells(1,5).Value = 'test5'

sheet.Cells(1、5)。值= ' test5 '

sheet.Cells(1,6).Value = 'test6'

sheet.Cells(6)。值= ' test6 '

sheet.Cells(1,7).Value = 'test7'

sheet.Cells(7)。值= ' test7 '

sheet.Cells(1,8).Value = 'test8'

sheet.Cells(8)。值= ' test8 '

#3


0  

I encountered this same issue recently. While it sounds like there can be multiple root causes, my situation was occurring because Python was making subsequent calls too quickly for Excel to keep up, particularly with external query refreshes. I resolved this intermittent "Call was rejected by callee" error by inserting time.sleep() between most of my calls and increasing the sleep argument for any calls that are particularly lengthy (usually between 7-15 seconds). This allows Excel the time to complete each command before Python issued additional commands.

我最近遇到了同样的问题。虽然听起来可能有多个根原因,但我的情况发生了,因为Python对Excel进行后续调用的速度太快,无法跟上,特别是在外部查询刷新时。我解决了这个间歇性的“调用被callee拒绝”错误,方法是在大多数调用之间插入time.sleep(),并为任何特别长(通常在7-15秒之间)的调用增加睡眠参数。这允许Excel在Python发出附加命令之前完成每个命令。