سؤال

a new Python Programmer here. I have a problem and hopefully you could help me. What I am doing is that I am tagging cells in an Excel Sheet through Python. And suppose I define a name for a huge no of cells at a time I am required to use Union. So it will be a Union of ranges. Now I was looking at the VBA code and translating them to Python syntax using win32com. If the Range is a few cells say 50 or so, I dont need Union but for large no of cells I would. Here's where the problem lies. I have come up with the following code, but i end up getting the

"TypeError: The Python instance can not be converted to a COM object " error.

from traits.api import HasTraits, Str, Int, Button,CStr, List
from win32com.client import gencache
import win32com.client,sys,os

class cellTagging(HasTraits):

    wb = "" 
    Data = CStr
    parameter = Str
    read_Write = Str
    sheet = Str
    cell_No = List
    cell_Value = Str
    excel = win32com.client.gencache.EnsureDispatch('Excel.Application')


    def _cell_No_changed(self):
        path=os.getcwd()
        fName = os.path.join(path, sys.argv[1])
        self.wb = cellTagging.excel.Workbooks.Open(fName)
        she = self.wb.ActiveSheet
        length = len(self.cell_No)
        r = []
        for i in self.cell_No:
            r.append(she.Range(",".join(i)))
        print r
        print(r[0])
        print r[1]
        print r[2]
        h = "%s " * len(r) % tuple(r)
        string = h[:-1]
        cellTagging.excel.Union(h).Name = "Test1"
        self.wb.Close(SaveChanges = 1)

    def Run_Macro(self,Macro):
        path=os.getcwd()
        fName = os.path.join(path, sys.argv[1])
        self.wb = cellTagging.excel.Workbooks.Open(fName)
        cellTagging.excel.Application.Run(Macro)
        self.wb.Close(SaveChanges = 1)

if __name__=='__main__':
    cellTagging( parameter = 'demo', read_Write = 'Read', sheet  = 'Testing1', cell_No =        [['A86', 'A87', 'A88', 'A89', 'A90', 'A91', 'A92', 'A93', 'A94', 'A95', 'A96', 'A97', 'A98', 'A99', 'A100', 'A101', 'A102', 'A103', 'A104', 'A105', 'A4', 'B4', 'C4', 'D4', 'E4', 'F4', 'G4', 'H4', 'I4', 'J4', 'K4', 'L4', 'M4', 'N4', 'O4', 'P4', 'Q4', 'R4', 'S4', 'T4', 'U4', 'V4', 'W4', 'X4', 'Y4', 'Z4', 'AA4', 'AB4', 'AC4', 'AD4'], ['AE4', 'AF4', 'AG4', 'AH4', 'AI4', 'AJ4', 'AK4', 'AL4', 'AM4', 'AN4', 'AO4', 'AP4', 'AQ4', 'AR4', 'AS4', 'AT4', 'AU4', 'AV4', 'AW4', 'AX4', 'AY4', 'AZ4', 'BA4', 'BB4', 'BC4', 'BD4', 'BE4', 'BF4', 'BG4', 'BH4', 'BI4', 'BJ4', 'BK4', 'BL4', 'BM4', 'BN4', 'BO4', 'BP4', 'BQ4', 'BR4', 'BS4', 'BT4', 'BU4', 'BV4', 'BW4', 'BX4', 'BY4', 'BZ4', 'CA4', 'CB4'], ['CC4', 'CD4', 'CE4', 'CF4', 'CG4', 'CH4', 'CI4', 'CJ4', 'CK4', 'CL4', 'CM4', 'CN4', 'CO4', 'CP4', 'CQ4', 'CR4', 'CS4', 'CT4', 'CU4', 'CV4', 'CW4', 'CX4', 'CY4', 'CZ4', 'DA4', 'DB4']])

The line hello = cellTagging.excel.Union(h).Name = "Test1" gives me the "TypeError: The Python instance can not be converted to a COM object" error

PS: But when I put the list elements(Ranges) directly as hello = cellTagging.excel.Union(r[0],r[1],r[2]).Name = "Test1"

it works. I am confused any insights would be appreciated!Thank you!

هل كانت مفيدة؟

المحلول

According to Union Method, the union method looks like:

 expression.Union(Arg1, Arg2, ...)

But your code will pass a single string.

h = "%s " * len(r) % tuple(r)    
...
hello = cellTagging.excel.Union(h).Name = "Test1"

Whereas:

hello = cellTagging.excel.Union(r[0],r[1],r[2]).Name = "Test1" 

Will pass in a three parameters.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top