1

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

2 Answers2

1

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

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)
Oliver
  • 27,510
  • 9
  • 72
  • 103
  • 1. Error because Paste without Copy before is not possible 2. Error: inner_clipboard_window_procedure 3. no Error 4. no Error 5. no Error – user3231222 Feb 25 '14 at 08:12
  • Update: 1a, Comment out everything between these stated also workbook1.close and del workbook1 --> I get the error in the first run – user3231222 Feb 26 '14 at 08:13
  • Update: 1b, Comment out everything between UsedRange.Copy() and excel_workbook1.Close() --> I get the same error in the second run – user3231222 Feb 26 '14 at 08:15
  • Update: 2. I get the same Error – user3231222 Feb 26 '14 at 08:54
  • update 1a is strange! can you run again? Can you extend your question to include the script that causes error on first try? and verify it is repeatable (always on first try). – Oliver Feb 26 '14 at 19:42
  • update 1a: Yes it is repeatable. The last two lines are "excel_workbook1.Worksheets(Sheet).UsedRange.Copy()" and "excel_app.Quit()" – user3231222 Feb 27 '14 at 07:13