Python COM -- What is the name of the file?

gbreed at cix.compulink.co.uk gbreed at cix.compulink.co.uk
Tue Feb 26 11:35:08 EST 2002


Mike Brenner wrote:

> Does anybody know how to get the name of the file an Excel Spreadsheet 
> is in?
> 
> For example, to do a file-save-as in tab-delimited format, 
> it is necessary to change "C:\data\stuff.xls"
> to "C:\data\stuff.txt", but I don't know where to
> get the "C:\data\stuff.xls" from in any given spreadsheet.

Do you have the "Microsoft Excel Visual Basic Reference" installed?  It 
should be in your help topics list.  If not, it's an option when you 
install Office, and it's what you need to answer questions like this.

Anyway, for an object called xl, it looks like these are what you want:

>>> xl.Workbooks[0].Path
u'C:\\Documents and Settings\\grahamb\\My Documents'
>>> xl.Workbooks[0].Name
u'Book1.xls'
>>> xl.Workbooks[0].FullName
u'C:\\Documents and Settings\\grahamb\\My Documents\\Book1.xls'

> It would even be okay, if I could just get the name "stuff" from some 
> part of the COM object.
>
> (And, of course, hopefully the same technique will work in Microsoft 
> Project, etc.)

I think "Workbooks" is Excel specific.


                   Graham



More information about the Python-list mailing list