[Tutor] Looping, and Win32com.client

Andreas Kostyrka andreas at kostyrka.org
Fri Dec 19 18:29:18 CET 2008


Am Fri, 19 Dec 2008 15:57:49 +0000
schrieb Tim Golden <mail at timgolden.me.uk>:

> Eduardo Vieira wrote:
> > 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):
> 
> 
> os.listdir returns a list of files relative to
> the directory in question. (Try it at the
> interpreter and see). Excel requires, pretty
> much, a list of absolute filenames. So try
> using something like os.path.join (folder, ficheiro)
> when opening in Excel.

The above should be enough, but if problems persists, it's often
helpful in Win32 OSes to use the 8.3 filename:

shortname = win32api.GetShortPathName(os.path.join(folder, ficheiro))

Another alternative would be to use glob:

import glob

...

for ficheiro in glob.glob(os.path.join(folder, "*.xls")):


glob.glob returns the full path that was found.

Andreas

> 
> TJG
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list