can't open word document after string replacements

Jon Clements joncle at googlemail.com
Tue Oct 24 04:50:40 EDT 2006


Antoine De Groote wrote:

> Hi there,
>
> I have a word document containing pictures and text. This documents
> holds several 'ABCDEF' strings which serve as a placeholder for names.
> Now I want to replace these occurences with names in a list (members). I
> open both input and output file in binary mode and do the
> transformation. However, I can't open the resulting file, Word just
> telling that there was an error. Does anybody what I am doing wrong?
>
> Oh, and is this approach pythonic anyway? (I have a strong Java background.)
>
> Regards,
> antoine
>
>
> import os
>
> members = somelist
>
> os.chdir(somefolder)
>
> doc = file('ttt.doc', 'rb')
> docout = file('ttt1.doc', 'wb')
>
> counter = 0
>
> for line in doc:
>      while line.find('ABCDEF') > -1:
>          try:
>              line = line.replace('ABCDEF', members[counter], 1)
>              docout.write(line)
>              counter += 1
>          except:
>              docout.write(line.replace('ABCDEF', '', 1))
>      else:
>          docout.write(line)
>
> doc.close()
> docout.close()

Errr.... I wouldn't even attempt to do this; how do you know each
'line' isn't going to be split arbitarily, and that 'ABCDEF' doesn't
happen to be part of an image. As you've noted, this is binary data so
you can't assume anything about it. Doing it this way is a Bad Idea
(tm).

If you want to do something like this, why not use templated HTML, or
possibly templated PDFs? Or heaven forbid, Word's mail-merge facility?


(I think MS Office documents are effectively self-contained file
systems, so there is probably some module out there which can
read/write them).

Jon.




More information about the Python-list mailing list