Broken examples
norseman
norseman at hughes.net
Tue Aug 12 13:48:15 EDT 2008
Tim Roberts wrote:
> 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.
>
==============
The wb.Close( False) works as predicted.
The excel = None leaves Excel open.
excel.Visible=0 does close it so I'm OK.
excel.Quit() is probably the better choice.
(learned via a comment above + Trial & Error)
For the next person that might have need of this:
>>>>>>>>>> tested/working final snippet
#!/---
#
import os
import win32com.client
excel = win32com.client.Dispatch( 'Excel.Application' )
excel.Visible=0 #=1 shows the spreadsheet (can be handy)
xlCSV = 6
# substituted 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$/Win-DOS won't auto overwrite
os.unlink(csv)
wb = excel.Workbooks.Open( nm )
wb.SaveAs( csv, xlCSV )
wb.Close( False )
#
excel.Quit() # doing it right
#
print "Do a dir *.csv check to see if this worked."
# end of file
>>>>>>>>>>>>>
Using the Python supplied CSV module now makes handling a variety of
.xls files a real snap. "Variety" of real spread sheet types, yes. The
butchered type that should have been done in Word - probably never.
Thanks again!
Steve
norseman at hughes.net
More information about the Python-list
mailing list