Broken examples
Tim Roberts
timr at probo.com
Mon Aug 11 20:58:51 EDT 2008
norseman wrote:
> Tim;
> Finally got a chance to test your snippet. Thank you for it!
>
> I took the liberty of flushing it out a bit.
> ================
> #!/---
> #
> import os
> import win32com.client
>
> excel = win32com.client.Dispatch( 'Excel.Application' )
> excel.Visible=1 # shows the spreadsheet (can be handy)
> xlCSV = 6
> # substitute for list generation
> list = ["c:\\temp\\test.xls", "c:\\temp\\test1.xls"]
>
> for nm in list:
> csv = os.path.splitext( nm )[0] + '.csv'
> print "Procesing file:", csv
> if os.path.exists(csv): # window$ won't auto overwrite
> os.unlink(csv)
> wb = excel.Workbooks.Open( nm )
> wb.SaveAs( csv, xlCSV )
> print wb.Close() # requires manual verification
> # eliminating unattended runs
>
> #wb.Exit() & wb.Quit() # generate ERROR(s) - no such thing(s)
> # leaving Excel open
>
> excel.Visible=0 # actually closes Excel if no sheet open
> # verified via task manager
>
> print "Check to see if this worked."
>
> # end of file
> =================
> The above does in fact work.
> Using the above I have a few questions.
> Documentation on xlCSV came from where?
> " on .Workbooks.Open(file.xls) came from ???
> " on .SaveAs( filename, xlCSV ) came from ???
> " on .Close() came from ???
MSDN has rather extensive documentation on the Excel object model,
although it is somewhat spread out. Search for "Excel object model".
I cheated on xlCSV. That's the name of the symbol, and I believe it
will even be available as win32com.client.constants.xlCSV after you
instantiate the application, but I just searched the web for "xlCSV" and
hard-coded the 6.
> Tell me about the lack of the .Exit() and/or .Quit(), please.
Workbooks don't exit or quit. Workbooks can be closed, but exit is
something that applies to the application (excel, in your case). You
should also be able to say
excel = None
to get rid of the app.
> I ran out of time today. Is it possible to send something like an
> <enter> keystroke to a Window (<yes><no>) box if it accepts same from
> actual keyboard? May not work anyway since .Close() hangs (in
> interactive) awaiting it's answer.
The Workbook.Close method accepts parameters; you should be able to get
it to skip the dialog. I believe that wb.Close( False ) should do this.
--
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.
More information about the Python-list
mailing list