0

I am trying to loop through a bunch of excel files and copy a specific sheet from each of them into another excel file:

for ii in ll_files:
  excel = win32.gencache.EnsureDispatch('Excel.Application')
  wb_1 = excel.Workbooks.Open(ii)
  ws_1 = wb_1.Worksheets('Q1')
  wb_2 = excel.Workbooks.Open(r'filepath')
  ws_2 = wb_2.Worksheets('answers')

  ws_1.Copy(ws_2)

  wb_1.Save()
  wb_2.Save()

  wb_1.Close(True)
  wb_2.Close(True)

  excel.Application.Quit()
  print ii
  time.sleep(1)

Works on about half the files I'm looping through, while the other half fail with this error:

com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft Excel', u'Copy method of Worksheet class failed', u'xlmain11.chm', 0, -2146827284), None)

All of the files have a 'Q1' tab

David Yang
  • 1,807
  • 6
  • 23
  • 38
  • Are you trying to open a workbook without explicitly specifying the full path? That might explain the iffy behavior; e.g. sometimes the default folder is correct and sometimes it is not. –  Sep 09 '15 at 19:40
  • Ah, good call but I don't believe that's the problem, I check the ws_1 and wb_1 when the code fails and they seem correct. – David Yang Sep 09 '15 at 20:03
  • So others don't have to translate the error codes: `-2147352567 => "Exception Occurred"` and `-2146827284 => "Unknown error (0x800a03ec)"`. The inner exception [looks](http://stackoverflow.com/a/7108420/5240004) [promising](http://stackoverflow.com/a/14364664/5240004). Admittedly, `Unknown Error` could be a lot of other things too. – theB Sep 09 '15 at 20:36

0 Answers0