[Tutor] Edit a Word document programmatically CORRECTION Open not open
Marc Tompkins
marc.tompkins at gmail.com
Tue Oct 14 19:07:30 CEST 2008
On Tue, Oct 14, 2008 at 9:23 AM, URBAN LANDREMAN <ulandreman at msn.com> wrote:
> Thanks for the suggestion.
>
> When I ran the slightly modified code:
> import win32com.client
> app = win32com.client.Dispatch('Word.Application')
> docOrig =
> app.Documents.Open('n:\\MetroPlanners\\2006\\MCHResearchProject.doc')
> range = docOrig.Content()
> range.Find.Text = "e"
> range.Find.Replacement.Text = "*"
> range.Find.Execute()
> docEditted = doc
> docEditted.SaveAs('n:\\MetroPlanners\\2006\\MCHResearchProjectV2.doc')
> app.Quit()
>
>
> I got:
>
> Traceback (most recent call last):
> File "N:/SPSS/Python/Examples/MSWord/WordFindAndReplaceV2", line 5, in
> <module>
> range.Find.Text = "e"
> AttributeError: 'unicode' object has no attribute 'Find'
>
docOrig.Content() returns you a Unicode object, which isn't the same as a
Word document at all. You're then trying to use methods on that object
which aren't supported. I can't give you specifics without doing a bit of
Googling myself, but you probably want a different call on line 4 to give
you an object you can work with.
--
www.fsrtechnologies.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20081014/bfe820a5/attachment.htm>
More information about the Tutor
mailing list