Question

The python code iterates over the paragraphs of a simple document (7 lines "Line 1", "Line 2" etc). I expect that each paragraph-object is unique, but actually I get duplicates, in this case 0x25173368. What happens? It is completely against my intuition.

The code:

import win32com.client as win32

word = win32.GetObject(Class="Word.Application")
doc = word.Documents.Open("test.docx")

for para in doc.Paragraphs:
  print repr(para)

The output:

<win32com.gen_py.Microsoft Word 12.0 Object Library.Paragraph instance at 0x25173368>
<win32com.gen_py.Microsoft Word 12.0 Object Library.Paragraph instance at 0x25427240>
<win32com.gen_py.Microsoft Word 12.0 Object Library.Paragraph instance at 0x25173368>
<win32com.gen_py.Microsoft Word 12.0 Object Library.Paragraph instance at 0x25427280>
<win32com.gen_py.Microsoft Word 12.0 Object Library.Paragraph instance at 0x25173368>
<win32com.gen_py.Microsoft Word 12.0 Object Library.Paragraph instance at 0x25427320>
<win32com.gen_py.Microsoft Word 12.0 Object Library.Paragraph instance at 0x25173368>
Était-ce utile?

La solution

Iteration over doc.Paragraphs probably creates a Python wrapper object to each underlying COM object dynamically. Since you do not store para anywhere, the wrapper object is destroyed when it goes out of scope, and the next iteration may create a new object at the same memory address.

Try this instead and see if the result is different:

paras = list(doc.Paragraphs)
for para in paras:
    print repr(para)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top