Question

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()
Was it helpful?

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()

OTHER TIPS

Sorry this is not an answer but didn't fit in a comment:

Can you try the following, and post in which case you still have problem:

  1. If you just remove the UsedRange.Copy() line
  2. If you just remove the PasteSpecial line
  3. If you remove both the UsedRange.Copy() line and the PasteSpecial line
  4. If you remove lines from excel_workbook1.Worksheets(Sheet).UsedRange.Copy() to del excel_workbook2
  5. If you remove all the lines from excel_app = win32com... to end of Copy(), and remove the import of win32com

Update:

Based on results by user3231222, two more tests:

  1. Confirm that if comment out everything between (but not including) the lines UsedRange.Copy() and excel_app.Quit() that error still occurs.
  2. If it does, check whether you get same error if you replace the spreadsheet loaded with one that has say just a couple of cells with text in them (no formulas or formatting etc)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top