Search code examples
pythonclipboardcopy-pastegladewin32com

Error by doing Copy Paste with win32com (Glade GTK Python)


I have a simple function "Copy", for copy and paste with win32com. It runs several times without problems.

But if i use a button(GUI GTK Glade) to trigger the function "Copy()", it runs only once. The second time i get the following Error:

Gdk-CRITICAL (recursed) **: inner_clipboard_window_procedure:
assertion `success' failed
aborting...

Can you help me ?

CopyPaste:

import os, sys
import win32com.client

def Copy():
    # Pfad zum Template
    path_to_temp = r"C:\004_Python_Workspace\Persek\Template.xls"

    # Pfad zum Testpaket
    xlsPath = r"C:\004_Python_Workspace\Testfolder\Testanweisung_LK_ASL.xls" 

    # Blattname im Template und im Testpaket
    Sheet = 'ECU_Config'

    excel_app = win32com.client.dynamic.Dispatch('Excel.Application') 

    ###### Kopiere ECU_Config aus Template #####
    excel_workbook1 = excel_app.Workbooks.Open(path_to_temp)

    excel_workbook1.Worksheets(Sheet).UsedRange.Copy()

    ###### Fuege in das neue Testpaket ein #####      
    excel_workbook2 = excel_app.Workbooks.Open(xlsPath)
    excel_workbook2.Worksheets(Sheet).Range('A1').PasteSpecial()
    excel_workbook2.worksheets(Sheet).Columns('A:B').AutoFit()
    excel_workbook2.Close(SaveChanges=True)
    del excel_workbook2

    excel_workbook1.Close()
    del excel_workbook1
    excel_app.Quit()     

Glade GtK Trigger with a button:

def on_debug_clicked(self, object, data=None):
    CopyPaste_Error.Copy()

Update 1a: Until "UsedRange.Copy()" line is all the same.

    excel_workbook1.Worksheets(Sheet).UsedRange.Copy()
    excel_app.Quit()

Solution

  • Now i found the solution, but i dont know why. I include "win32clipboard"

    import os, sys
    import win32com.client
    import win32clipboard
    
    def Copy():
    
        win32clipboard.OpenClipboard()
    
        # Pfad zum Template
        path_to_temp = r"C:\004_Python_Workspace\Persek\Template.xls"
    
        # Pfad zum Testpaket
        xlsPath = r"C:\004_Python_Workspace\Testfolder\Testanweisung_LK_ASL.xls" 
    
        # Blattname im Template und im Testpaket
        Sheet = 'ECU_Config'
    
        excel_app = win32com.client.dynamic.Dispatch('Excel.Application') 
    
        ###### Kopiere ECU_Config aus Template #####
        excel_workbook1 = excel_app.Workbooks.Open(path_to_temp)
    
        excel_workbook1.Worksheets(Sheet).UsedRange.Copy()
    
        ###### Fuege in das neue Testpaket ein #####      
        excel_workbook2 = excel_app.Workbooks.Open(xlsPath)
        excel_workbook2.Worksheets(Sheet).Range('A1').PasteSpecial()
        excel_workbook2.worksheets(Sheet).Columns('A:B').AutoFit()
        excel_workbook2.Close(SaveChanges=True)
        del excel_workbook2
    
        excel_workbook1.Close()
        del excel_workbook1
        excel_app.Quit()
    
        win32clipboard.CloseClipboard()