1

Possible Duplicate:
How to search and replace in MS Word Textbox using Python

How to manuplate textbox in MS Word using Python Hello , I’m a newbie to python. I want to do some automating work in hundreds of msword files. I need to do lots of search and replace work.

I got an example from internet. So I can replace original string AAA with BBB in hundreds of msword files. But however it can not replace the strings in textbox. Almost drive me crazy and it’s hard to find the solution.

  My Python version is 2.7 and I include the following libraries in my file. I still can NOT show every textbox’s text in word files

I tried the solution Python win32com - Automating Word - How to replace text in a text box? but it didn't work

canvas = word.ActiveDocument.Shapes[0]
for item in canvas.CanvasItems:
    print item.TextFrame.TextRange.Text

Even i can get canvas object, but in the above "FOR loop" , i'll get the error exception. it seems it didn't have those members.

Does anyone can give me some concrete examples , Or other popular frameworks or libraries in Windows to handle MS Word/Excel. Thank you in advance…thank you very much~

Belows are the errors , it seems i tried to access non-existed attribute.

  Traceback (most recent call last):
  File "a.py", line 108, in <module>
    main()
  File "a.py", line 86, in main
    AppWord = ReadWrod(docPath)
  File "a.py", line 51, in ReadWrod
    testTB(doc)
  File "a.py", line 22, in testTB
    print doc.Shapes[0].TextFrame.TestRange.Text
  File "C:\Python27\lib\site-packages\win32com\client\dynamic.py", line 522, in __getattr__
    raise AttributeError("%s.%s" % (self._username_, attr))
AttributeError: <unknown>.TestRange
Community
  • 1
  • 1
newBike
  • 14,385
  • 29
  • 109
  • 192
  • Is there a reason you're using python for this over VBA? – kreativitea Nov 13 '12 at 00:22
  • Don't just say "i'll get the error exception", show us the exception and traceback. And ideally do at least the minimal first step toward debugging: e.g., inside the `for` loop, `print` something identifiable so you can tell which item is raising the exception. – abarnert Nov 13 '12 at 01:53
  • 1
    Also, it looks like there's a typo in the answer you've copied; what happens if you do `item.TextFrame.TextRange.Find.Text` instead of `item.TextFrame.TextRange.Text`? – abarnert Nov 13 '12 at 01:54
  • Excuse me, is there anyway to print out what attribute does a object have. – newBike Nov 13 '12 at 02:39
  • eg : item has a member called "TextFrame" but how can i know?? there is no api document for win32py. thank you all! – newBike Nov 13 '12 at 02:40

0 Answers0