[Tutor] Looping, and Win32com.client

Eduardo Vieira eduardo.susan at gmail.com
Fri Dec 19 16:54:45 CET 2008


Hello, this is my first post in this list and I'm not a programmer,
but am enjoying learning python.
I'm trying to make a script to add worksheets to excel files:
I managed to implement this code:

import os
folder = 'C:\\Personal\\oldxlsformat\\'
from win32com.client import Dispatch
xl = Dispatch('Excel.Application')
xl.Application.AskToUpdateLinks = "False"
for ficheiro in os.listdir(folder):
    if ficheiro.endswith('.xls'):
        wb = xl.Workbooks.Open(ficheiro, 2)
        wb.Worksheets.Add().Name = "BVTest"
        wb.Close(SaveChanges = 1)


xl.Quit()

but then I get this error:
Traceback (most recent call last):
  File "C:\Personal\exceltests4.py", line 8, in <module>
    wb = xl.Workbooks.Open(ficheiro, 2)
  File "<COMObject <unknown>>", line 8, in Open
com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft Office
Excel', u"'AVERY GRAPHICS INK JET.xls' could not be found. Check the
spelling of the file name, and verify that the file location is
correct.\n\nIf you are trying to open the file from your list of most
recently used files, make sure that the file has not been renamed,
moved, or deleted.", u'C:\\Program Files\\Microsoft
Office\\Office12\\1033\\XLMAIN11.CHM', 0, -2146827284), None)

The files I have as a test are these:

C:\Personal\oldxlsformat\AVERY GRAPHICS INK JET.xls
C:\Personal\oldxlsformat\CUTTING MATS & RULERS.xls
C:\Personal\oldxlsformat\Oracal - Ontario Stocking Order.xls
C:\Personal\oldxlsformat\ORACAL 8100-8500.xls


More information about the Tutor mailing list