[Python] Try to open Excel.xlsm file using xlwings results in Com-Error

Discussão em 'Python' iniciado por Stack, Agosto 26, 2025.

  1. Stack

    Stack Membro Participativo

    I'm currently working on a project where I need to open an .xlsm Excel file through my code. However, I keep running into the following error:

    Traceback (most recent call last):
    File "C:\.\Python311\site-packages\win32com\client\dynamic.py", line 81, in _GetGoodDispatch
    IDispatch = pythoncom.connect(IDispatch)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    pywintypes.com_error: (-2147221021, 'Vorgang nicht verfügbar.', None, None)

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
    File "C:\.\excel_service.py", line 48, in <module>
    ExcelService()
    File "C:\.\excel_service.py", line 25, in __init__
    File = win32com.client.Dispatch("Excel.Application")
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\.\Python311\site-packages\win32com\client\__init__.py", line 116, in Dispatch
    dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch, userName, clsctx)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\.\Python311\site-packages\win32com\client\dynamic.py", line 101, in _GetGoodDispatchAndUserName
    return (_GetGoodDispatch(IDispatch, clsctx), userName)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\.\Python311\site-packages\win32com\client\dynamic.py", line 83, in _GetGoodDispatch
    IDispatch = pythoncom.CoCreateInstance(
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
    pywintypes.com_error: (-2146959355, 'Starten des Servers fehlgeschlagen', None, None)

    • Operating System: Windows 11 Pro x64
    • Excel version: 365
    • Python version: 3.11.9
    • xlwings version: 0.33.15

    Here are the attempts I've made so far:

    import xlwings as xl
    import os
    import win32com

    class ExcelService:

    BASE_PATH = os.curdir
    EXCEL_SYS_PATH = "240401_Gesamtplanung vsSYS_Budget26_V4.xlsm"
    EXCEL_SW_PATH = "240401_Gesamtplanung vsSW_Budget26_V2.xlsm"

    def __init__(self):
    self.sys_full_path = os.path.join(self.BASE_PATH, self.EXCEL_SYS_PATH)
    self.sw_full_path = os.path.join(self.BASE_PATH, self.EXCEL_SW_PATH)

    if xl.apps:
    print("aktive")
    app = xl.apps.active
    if app.books:
    wb = app.books[0]
    else:
    wb = xl.Book(self.sw_full_path)
    else:
    print("öffnen")
    File = win32com.client.Dispatch("Excel.Application")
    File.Visible = 1
    wb = File.Workbooks.open(self.sw_full_path)

    # with xl.App(visible=False) as app:
    # wb = xl.Book(os.path.abspath(self.sw_full_path))
    # app.visible = True
    # print(wb.sheets.active)
    # print("Arbeitsmappe:", wb.name)
    # print("Pfad:", wb.fullname)
    # input("Drücke Enter zum Beenden…") # Damit das Fenster offen bleibt

    # app = xl.App(visible=True)
    # wb = app.books.open(self.sw_full_path)
    # wb = xl.Book(self.sw_full_path)
    # app = wb.app

    # print(wb.sheets.active)
    # print("Arbeitsmappe:", wb.name)
    # print("Pfad:", wb.fullname)
    # input("Drücke Enter zum Beenden…") # Damit das Fenster offen bleibt

    if __name__ == "__main__":
    ExcelService()

    Continue reading...

Compartilhe esta Página