ARTICLE - Why the MS Office file formats is so complicated

Kent Johnson kent37 at tds.net
Thu Feb 21 13:19:35 EST 2008


Shawn O'Shea wrote:

Putting on my python-tutor hat...

> import win32com.client
> import os
> 
> docsdir = "C:\Documents and Settings\shawn\My Documents\wordtest"
> outdir = "C:\Documents and Settings\shawn\My Documents\converted"

Raw strings or / or doubled \\ would be prudent here.

> docslist = os.listdir(docsdir)
> 
> app=win32com.client.Dispatch("Word.Application")
> app.Visible=1
> 
> for file in docslist:

'file' is a poor choice of name, it shadows the built-in file().

>     print os.path.abspath(docsdir+"\\"+file)

os.path.join(docsdir, file) is more idiomatic. You might want to check 
for directories here too, or check the extension.

>     doc=app.Documents.Open(os.path.abspath(docsdir+"\\"+file))
>     doc.SaveAs(outdir+"\\"+file.replace(file[-3:],"rtf"),6)

os.path.splitext(file)[0] + '.rtf' is more robust; file[:-3]+'rtf' is 
simpler if you are sure of the extension.

Kent

>     doc.Close()
> 
> app.Quit()



More information about the gnhlug-discuss mailing list