“工作表上找不到”范围的整数变量

2024-06-28 11:32:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我的代码仍然存在问题(我是自学成才的,所以我远不是Python或XLwings方面的专家)。我的方法如下:

  1. 我将一组4列数据(长度也不同)复制到一个2d数组中
  2. 然后将数据与新找到的数据集进行比较,并从复制的数组中删除匹配项
  3. 然后,将清除指定给原始二维阵列的列的内容
  4. 相同的数组(已删除匹配项)将在与上一个数组相同的起始单元格中替换

简而言之,我复制列,删除匹配项,清除原始单元格,然后将新内容粘贴到与原始列相同的起始单元格。(由于列的长度不是固定的,所以我将此单元格行分配给变量“sht2Row”。)

但是,大约有10%的时间,我会得到以下错误:

    104         sht2.range((5,3),(sht2Row,10)).clear_contents()
    105         time.sleep(0.5)
--> 106         sht2.range((5,12),(sht2Row,16)).clear_contents()

    107         time.sleep(0.5)
    108         sht2.range("C5").value=BoughtData

~\anaconda3\lib\site-packages\xlwings\main.py in range(self, cell1, cell2)

    862                 raise ValueError("Second range is not on this sheet")
    863             cell2 = cell2.impl
--> 864         return Range(impl=self.impl.range(cell1, cell2))

    865 
    866     @property

~\anaconda3\lib\site-packages\xlwings\_xlwindows.py in range(self, arg1, arg2)

    635             xl2 = self.xl.Range(arg2)

    636 

--> 637         return Range(xl=self.xl.Range(xl1, xl2))

    638 
    639     @property

~\anaconda3\lib\site-packages\xlwings\_xlwindows.py in __call__(self, *args, **kwargs)
     64         while True:

     65             try:


---> 66                 v = self.__method(*args, **kwargs)

     67                 if isinstance(v, (CDispatch, CoClassBaseClass, DispatchBaseClass)):

     68                     return COMRetryObjectWrapper(v)

~\AppData\Local\Temp\gen_py\3.8\00020813-0000-0000-C000-000000000046x0x1x9.py in Range(self, Cell1, Cell2)

  47370         # The method Range is actually a property, but must be used as a method to correctly pass the arguments

  47371         def Range(self, Cell1=defaultNamedNotOptArg, Cell2=defaultNamedOptArg):

> 47372         ret = self._oleobj_.InvokeTypes(197, LCID, 2, (9, 0), ((12, 1), (12, 17)),Cell1 , Cell2)

  47374                 if ret is not None:

com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2146827284), None)

有人知道为什么会发生这种情况吗?在过去,我偶尔会收到此错误,因为我正在编写与这些销售人员对话的代码,但最近大部分时间,我甚至不会在电脑前,但仍会收到此错误。我不明白。如果这是sht2Row的某个问题,那么为什么在我第一次使用该变量时没有发生呢?不管怎样,当sht2Row只是一个整数时,怎么可能出现“第二个范围不在此工作表上”错误?我真的不知道为什么这只是偶尔发生,为什么它会说范围不在表上

这是我第一次使用这个网站。我通常会自己找出错误,但这一次让我难堪


Tags: 数据inpyselfnonelib错误site