Hi Mark,

thanks for spending time on my question. Sometimes when you are writing, you are already on the way to the solution ... Actually I haven't worked with an iterator object before an was unsure how to do it. But sometimes you must simply try. With an iterator for the Office Collection object wrappers like my wrapper for Applicaiton.Workbooks in addition to the overloaded __getitem__ it seems to work.

I plan to publish the package (alongside with a package with a COM object template class package for easening writing COM objects) as soon as I have done some test scripts ready. Probably it is of interest for you and the others here.

Best regards
Christoph


Am 19.04.2025 um 04:31 schrieb Mark Hammond:

I'm not sure what's going wrong here. Does fetching the list of workspaces as a simple list work? That would be a workaround, and feel free open an issue at https://github.com/mhammond/pywin32 since it shouldn't break that way.

Cheers

On 2025-04-18 8:33 p.m., dornech via python-win32 wrote:
Hi there,

I am working on an EXCEL wrapper to "phytonize" access to EXCEL.

I create a wrapper class for all objects classes derived from abasic wrapper class which mainly ensures that calls are snaked-case and pyhtonized calls work. I. e. workbokkobject.SaveAs, workbookobject.saveas and workbokkobject.save_as would all work. This is done by overloaded __getattr__ and __setattr__ methods.

The "real" EXCEL object is stored in the attribute "_xlWrapped".

The overloaded __getattr__ also ensures that when retrieving f. e. a workbook from the application wrapper, that the resulting EXCEL object is wrapped as well. I. e. let xlapp be my wrapped EXCEL then xlapp.active_workbook would return a reference to a wrapped Workbook object which contains the reference to the "real" EXCEL workbook object in the attribute _xlwrapped.

I also overload __getitem__ to allow xlapp[1] as short for xlapp.workbooks(1).

Some of these classes I do extend for example to check existence of a worksheet in a workbook.

This as very brief introduction.

Now I am struggeling with list comprehension and indexes.

    # access un-wrapped EXCEL
    print(xlapp._xlWrapped.Workbooks(1).Name)
    print(xlapp._xlWrapped.Workbooks(2).Name)
    for i, wb in enumerate(xlapp._xlWrapped.Workbooks):
        print(i, wb.Name)

    # access wrapped EXCEL
    print(xlapp.Workbooks(1).Name)
    print(xlapp.Workbooks(2).Name)
    for i, wb in enumerate(xlapp.Workbooks):
        print(i, wb.Name)

This prints
    WB1.xls
    WB2.xls
    0  WB1.xls
    1  WB2.xls
    WB1.xls
    WB2.xls
 and aborts with an windows/COM error:

 ret = self._oleobj_.InvokeTypes(170, LCID, 2, (13, 0), ((12, 1),),Index
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pywintypes.com_error: (-2147352567, 'Ausnahmefehler aufgetreten.', (0, None, None, None, 0, -2147352565), None)

Obviously when looping via the wrapper object the index is not automatically adjusted. However, I must not increase the index by on in __getitem__ because then the index would alway be increased.

It seems, püywin32 has some special support for list comprehension and index adjustment for COM objects?
I haven't worked with an iterator before but is this the solution?

Thanks in advance.

Best
Christoph





_______________________________________________
python-win32 mailing list
python-win32@python.org
https://mail.python.org/mailman/listinfo/python-win32

_______________________________________________
python-win32 mailing list
python-win32@python.org
https://mail.python.org/mailman/listinfo/python-win32

Reply via email to